汇编代码

时间:2017-09-29 03:22:59

标签: c linux assembly x86

我只是学习用C代码调用汇编代码,如下所示:

caller.c:

#include <stdio.h>
#include <string.h>

void main(void)
{
    char buf[50];
    int a, b, res;
    char *mystr = "calculating...\n";
    char *emsg  = "Error in adding\n";

    a = 5; b = 10;

    mywrite(1, mystr, strlen(mystr));

    if (myadd(a, b, &res))
    {    
        sprintf(buf, "the result is %d\n", res);
        mywrite(1, buf, strlen(buf));
    }   
    else
    {        
        mywrite(1, emsg, strlen(emsg));
    }
} 

callee.s:

.code32

SYSWRITE = 4
.global mywrite, myadd

.text
mywrite:
    pushl %ebp
    movl  %esp, %ebp
    pushl %ebx
    movl  8(%ebp), %ebx
    movl  12(%ebp), %ecx
    movl  16(%ebp), %edx
    movl  $SYSWRITE, %eax
    int   $0x80
    popl   %ebx
    movl  %ebp, %esp
    popl  %ebp
    ret

myadd:
    pushl %ebp
    movl  %esp, %ebp
    movl  8(%ebp), %eax
    movl  12(%ebp), %edx
    xorl  %ecx, %ecx
    addl  %eax, %edx
    jo    1f
    movl  16(%ebp), %eax
    movl  %edx, (%eax)
    incl  %ecx
1:  movl  %ecx, %eax
    movl  %ebp, %esp
    popl  %ebp
    ret

然后我编译:

as -o callee.o callee.s
gcc -o caller caller.c callee.o

但是当运行来电者:./caller时,我得到了这个

  

分段错误(核心转储)

如何解决?

0 个答案:

没有答案