Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

c - Accessing function args from the stack relative to EBP while pushing/popping other registers?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your stack frame set up is wrong. push ebp; mov ebp, esp must be issued before any other stack movement takes place.

By pushing other stuff on the stack before setting up ebp you have set up ebp to point to a different place than usual, causing all the offsets to be different. To fix this, first set up the stack frame, then push the remaining registers:

global FindPattern
 section .text
FindPattern:
    push    ebp
    mov     ebp,esp

    push    esi
    push    edi
    push    ebx
    push    edx
    push    ecx
    
    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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...