calling assembly functions from c

后端 未结 4 2097
不思量自难忘°
不思量自难忘° 2021-02-02 09:47

I\'m trying to use a function in assembly, invoked from a C project. This function is supposed to call a libc function let\'s say printf(), but I keep getting a se

4条回答
  •  耶瑟儿~
    2021-02-02 10:34

    After this:

    push printtext
    call printf
    

    You want:

    addl $4, %esp
    

    Further explanation:

    Because you're using x86 Linux I assume the calling convention requires the callee to cleanup the parameters. Because you pushed a pointer before calling printf, your stack is off by 4 after that function's ret instruction happened.

    Update:

    Yeah, OK, I was used to Intel syntax so I was getting the order of the arguments backward in my head. Actually the lack of the addl back to esp doesn't matter, because you're restoring esp correctly near your ret. My next guess is that the string you're passing to printf is lacking a null terminator... Let me see what gas does...

    Update 2:

    OK, gas null terminates strings for you, so I guess my second hunch was wrong. It looks like you found the issue so the point is moot.

提交回复
热议问题