简体   繁体   English

从汇编中调用C printf

[英]Calling C printf from assembly

I would like to call the printf function from C with two integers. 我想用两个整数从C调用printf函数。 My format string is: 我的格式字符串是:

LC0:
    db "odd bits: %d, even bits: %d", 10, 0

I have the integer ob and eb: 我有整数ob和eb:

ob: DD 0
eb: DD 0

and then I do at the end: 然后我在最后做:

push dword [ob]
push dword [eb]
push LC0
call printf
add esp,8

However, this gives me the result Odd bits: [ob], Even bits: [ob, repeated] then gives me a segmentation fault. 但是,这给了我结果Odd bits: [ob], Even bits: [ob, repeated]然后给了我一个分割错误。 Am I calling the printf function wrong? 我将printf函数称为错误吗?

EDIT: I added LC1 as db "even bits: %d", 10 0 , then redid: 编辑:我添加了LC1作为db "even bits: %d", 10 0 ,然后重做:

push dword [ob]
push LC0
call printf
push dword [eb]
push LC1
call printf
add esp, 8

This gives me a REVERSED result, giving the eb to the LC0 string, and ob to the LC1 string, and, it gives a segmentation fault at the end. 这给了我一个反转的结果,将eb赋给LC0字符串,将ob赋给LC1字符串,最后给出分段错误。 Any clue? 有什么线索吗?

You're not adjusting the stack pointer correctly. 您没有正确调整堆栈指针。

In your original code you were pushing 12 bytes, but only "popping" 8. 在原始代码中,您要推送12个字节,但只能“弹出” 8个字节。

In your updated code you're pushing 8 bytes twice, ie 16 bytes in total, but only "popping" 8 bytes once. 在更新的代码中,您要推送8个字节两次,即总共16个字节,但仅“弹出”一次8个字节。


As for the order in which the values are printed; 至于这些值的打印顺序; in your original code you had: 在原始代码中,您有:

push dword [ob]  
push dword [eb]
push LC0

You've declared LC0 as db "odd bits: %d, even bits: %d", 10, 0 , so clearly you intended ob to the printed first. 您已将LC0声明为db "odd bits: %d, even bits: %d", 10, 0 ob ,因此显然您打算将ob优先打印。 Arguments are push right-to-left, so you should push eb before ob . 参数是从右到左推,因此您应该在ob之前推eb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM