I'm currently trying to link assembly functions to my C code driver for a college assignment. Upon executing the program, I get a seg fault error.
Below will include what's in my C file, ASM file, and the info from the GDB debugger.
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void add(char*, char*); //would extern be needed here maybe?
int main(){
int choice;
char num1[3];
char num2[3];
printf("Welcome to the back and forth program!\n\n");
do{
printf("What would you like to do?\n\n");
printf("1. Add two numbers together.\n");
printf("2. Find if a string is a palindrome. (ASM Version)\n");
printf("3. Find the factorial of a number.\n");
printf("4. Find if a string is a palindrome. (C Version)\n");
printf("5. Exit Program.\n\n");
printf("choose 1-5: ");
scanf("%d", &choice);
getchar();
while(choice < 1 || choice > 5){
printf("\nPlease choose an option between 1 and 5.\n");
scanf("%d", &choice);
getchar();
}
switch(choice){
case 1:
printf("\n*Add two numbers together*\n\n");
printf("Please enter a number: ");
fgets(num1, 1024, stdin);
num1[strlen(num1) - 1] = '\0';
printf("\nPlease enter a second number: ");
fgets(num2, 1024, stdin);
num2[strlen(num2) - 1] = '\0';
add(num1, num2);
printf("\nResult: %s\n", num2);
case 2:
case 3:
case 4:
case 5:
printf("\nThanks for using!\n");
break;
}
}while(choice != 5);
return 0;
}
One thing to note here, is that my professor is specifically stating I read in the two numbers as strings, and then use the atoi() function in assembly to convert from string to int.
Now, my ASM code:
BITS 32
GLOBAL add
EXTERN atoi
section .data
section .bss
section .text
add:
push ebp
mov ebp, esp
push eax
call atoi
push ebx
call atoi
mov eax, [ebp+8]
mov ebx, [ebp+12]
add eax, ebx
pop ebx
ret
Since I'm required to call atoi() from my Assembly function, I'd assume it's necessary to use a stack.
Finally, what the GDB debugger is saying:
Program received signal SIGSEGV, Segmentation fault. 0xffffcdbc in ?? ()
A note on the debugger error: when stepping through the program, it says this error once it reaches add(num1, num2).
For some other important information, I'm using the GCC compiler, NASM compiler, Intel Assembler i386, and am running Debian 10 x86_64 in a virtual machine via VirtualBox.
Any help on the matter would be greatly appreciated!
pop ebxis probably a typo forpop ebp?