为什么省略printf打印垃圾的参数?(Why does omitting an argument to printf print garbage?)

编程入门 行业动态 更新时间:2024-10-26 18:23:25
为什么省略printf打印垃圾的参数?(Why does omitting an argument to printf print garbage?)

我通过汇编代码使用printf。 我注意到在下面的例子中,如果我省略了期望的参数,垃圾被打印。

.386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg3fmt byte 0Ah,"%s",0Ah,"test output",0Ah,0 .code main proc INVOKE printf, ADDR msg3fmt ret main endp end

我的问题是为什么? 有没有一个设置的内存地址printf使用期望找到一个参数? 为什么打印任何东西,因为没有参数通过?

I am using printf via assembly code. I note that in the following example if I ommit the expected argument, garbage is printed.

.386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg3fmt byte 0Ah,"%s",0Ah,"test output",0Ah,0 .code main proc INVOKE printf, ADDR msg3fmt ret main endp end

My question is why? Is there a set memory address printf uses expecting to find an argument? Why is anything printed at all since no argument is passed?

最满意答案

原因是格式说明符告诉printf应该收到多少个参数。 Printf从堆栈获取数据; 如果你没有提供任何数据,那么它会将任何发生在堆栈上的事情都视为参数。

The reason is that the format specifiers tell printf how many arguments it should have received. Printf gets its data from the stack; if you don't provide any data for it then it will pull whatever happened to be on the stack and treat as an argument.

更多推荐

本文发布于:2023-07-26 19:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:垃圾   参数   printf   omitting   garbage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!