0

I have a 64 bit VM setup with Linux Mint, I'm trying to use a 32 bit program on this machine. This program has 3 parts.

  1. library.asm
  2. main.asm
  3. p2-main.c

I run these commands and 1 & 2 parts work together with expected results when I run Main.

nasm -f elf32 library.asm
nasm -f elf32 main.asm
gcc -m32 main.o library.o -o Main

Then I use this and run into issues with parts 1 & 3:

gcc -m32 p2-main.c library.o -o P2-Main

When I do this the first part of my .c program works, but when trying to pull the results from the original library I get a "Segmentation fault (core dumped)" error.

Am I doing something wrong with the commands?
Would I have to change something with the .c program to work on 64 bit?

I have confirmed with someone that has a 32 bit VM with Linux Mint that the code works. Any suggestions?

Here is my code for the 3 parts

library:

SECTION .DATA
        Pint:     db '  %d',0
        Spaces:   db '       ',0
        num:      db 'Number',0
        total:    db ' Runninig total',10,0
        newline:  db '',10,0

SECTION .text
global _sumAndPrintList        ;printf use global main 
global _replaceChar
extern printf
_replaceChar:
       mov eax,0
       mov edx,0
       mov edx,[esp+16]
       mov eax,[esp+12]
       mov edi,[esp+4]
       mov ecx,[esp+8]
       ;jmp exit
while:
       mov bl,[edi]
       cmp bl,al
       jne skip
       mov [edi],dl
skip:
       inc edi
       loop while
exit:       
       ret


_sumAndPrintList:
       push num
       call printf
       add esp,4
       push total
       call printf
       add esp,4

       mov esi,[esp+8]
       mov edi,[esp+4]
       mov ebx,0
while1:
       cmp esi,0
       jle break

       mov eax,[edi]
       add ebx,eax
       push eax
       push Pint
       call printf
       add esp,8

       push Spaces
       call printf
       add esp,4
       push ebx
       push Pint
       call printf
       add esp,8

       push newline
       call printf
       add esp,4
       add esi,-1
       add edi,4
       jmp while1
break:
        mov eax,ebx
        ret                     ; Return control

Main:

global main        ;printf use global main
extern _replaceChar
extern printf

Section .data 
Pint:     db '%d',10,0
stat1: db 'Enter a character to Serach for:',10,0 
stat2: db 'Enter a character to replace for:',10,0 
stat3:  db 'Enter lines of text:',10,0 

section .bss
buffer resb 100     ;100B buffer
repChar resb 2
searchChar resb 2

SECTION .text

            push stat1
            call printf
            add esp,4

            mov eax, 3            ; 'read' system call = 3
            mov ebx, 0            ; file descriptor 0 = STDOUT
            mov ecx, searchChar       ;adress of buffer to store input
            mov edx, 2           ;max number of bytes to read
            int 80h 

            push stat2
            call printf
            add esp,4

            mov eax, 3            ; 'read' system call = 3
            mov ebx, 0            ; file descriptor 0 = STDOUT
            mov ecx, repChar       ;adress of buffer to store input
            mov edx, 2            ;max number of bytes to read
            int 80h 

            push stat3
            call printf
            add esp,4

            mov eax, 3            ; 'read' system call = 3
            mov ebx, 0            ; file descriptor 0 = STDOUT
            mov ecx, buffer       ;adress of buffer to store input
            mov edx, 100            ;max number of bytes to read
            int 80h 
            mov ecx,0
            mov edx,0
            mov eax,buffer
            mov ebx,100
            mov esi,repChar
            mov edi,searchChar
            mov cl,[edi]
            mov dl,[esi]

            push edx
            push ecx
            push ebx
            push eax

            call _replaceChar
            add esp,16
            mov eax,4            ; 'write' system call = 4
        mov ebx,1            ; file descriptor 1 = STDOUT
        mov ecx,buffer         ; string to write
        mov edx,100            ; length of string to write
        int 80h
    exit:         
           ;Terminate  program
        mov eax, 0x1
        xor ebx, ebx
        int 0x80

p2-main.c

/* example.c */
#include <stdio.h>
#include <string.h>
int  _sumAndPrintList(int *list,int a);
int _replaceChar(char *text,int len,char ser,char rep);

int main(int argc, char *argv[]) {

        char s[]="Hello";
        int a=1;
        int list[1000];
        printf("This list will end on negative number input!\n");
        int i=0;
        while(a>0)
         {
          scanf("%d", &list[i]);
          getchar();
          a=list[i];
          i++;
          }
          int len=i-1;
     int b=_sumAndPrintList(list,len);
        printf("The function returned a sum of %d, does that match the last number in the running total column?\n",b);

        printf("------------------------------------------------------------\n");
        printf("Enter Lines of text:\n");

        char text[100];
        len=strlen(text);
        char serachChar=' ';
        char replaceChar='_';
        fgets(text,sizeof(text),stdin);
        _replaceChar(text,100,serachChar,replaceChar);
        printf("%s\n",text);
        return 0;

}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

0 Answers0