在汇编程序中调用函数(AT& T,IA-32)

时间:2013-03-22 08:47:44

标签: function assembly x86 call

我刚开始学习汇编程序,我一开始就陷入困境 - 我试图调用一个简单的函数 - 实际上它主要是从一本书中复制出来的,而且我一直在分段错误。也许更有经验的人可以指出这段代码有什么问题:

.code32
SYSEXIT = 1
.data
.text
.globl _start
_start:
        push $28  #just some random argument
        push $33 
        call myfunc
        mov $SYSEXIT, %eax
        #exit code is stored in ebx after calling  function
        int $0x80
.type myfunc, @function
myfunc:
        push %ebp #save old base pointer on a stack
        movl %esp, %ebp
        movl 8(%ebp), %ebx #first argument to ebx
        movl 12(%ebp), %ecx #second argument to ecx
        addl %ecx, %ebx  #add arguments together - store them in ebx
        movl %ebp, %esp
        pop %ebp
        ret

1 个答案:

答案 0 :(得分:2)

您的函数期望参数为long(32位),因此您应该使用pushl而不是push推送它们。

您还应该确保在函数返回后平衡堆栈。即类似的东西:

pushl $28
pushl $33
call myfunc
addl $8,%esp  # "removes" two 32-bit arguments off the stack