I am writing an assembly program and a C program; the C program will call a function written in assembly. The environment is Ubuntu 18.04LTS x64.
It is designed for 32 bits x86 and will be compiled by NASM, but it can't pass correct parameters.
To simplify the problem, I just changed my function to get the sum of a and b.
extern int FindPattern(int a,int b);
int result;
result=FindPattern(1,1);
printf("%d
",result);
global FindPattern
section .text
FindPattern:
push ebp
push esi
push edi
push ebx
push edx
push ecx
mov ebp,esp
mov esi,[ebp+8] ; a
mov edi,[ebp+12] ; b
mov eax,0
add eax,esi
add eax,edi ; return a+b
pop ecx
pop edx
pop ebx
pop edi
pop esi
pop ebp
ret
The function just adds a and b, and returns the sum. The sum should be 2, however I got a random number like 1449041840. It seems the assembly didn't get correct parameters.
What's wrong with the code and how can I fix it?
# Makefile
cc=gcc
ASMBIN=nasm
all : asm cc link
asm:
$(ASMBIN) -o findpattern.o -f elf32 -l findpattern.lst findpattern.asm
cc :
$(cc) -m32 -c -g -O0 -fpack-struct graph_io.c
link :
$(cc) -m32 -o graph_io findpattern.o graph_io.o
clean:
rm *.o
rm graph_io
rm findpattern.lst
question from:
https://stackoverflow.com/questions/65905009/accessing-function-args-from-the-stack-relative-to-ebp-while-pushing-popping-oth 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…