为什么我们必须在这里取消引用stdout? [英] Why do we have to dereference stdout here?
问题描述
我正在尝试从程序集调用fputs(str, stdout);
.
I am trying to call fputs(str, stdout);
from assembly.
为什么我应该push dword [stdout]
而不是push stdout
?
由于在C语言中我们不执行fputs(str, *stdout)
,为什么我们需要在汇编中取消引用stdout
?
Since in C we don't do fputs(str, *stdout)
, why do we need to dereference stdout
in assembly?
完整代码:
extern fputs
extern stdout
section .data
hw: db "Hello World!", 10, 0
section .text
global main
main:
enter 0,0
push dword [stdout]
;push stdout
push hw
call fputs
leave
mov eax, 0
ret
推荐答案
您要取消引用asm标签stdout
,它等效于C中的&stdout
.它是内存中的静态位置,其中FILE*
值已存储.
You're dereferencing the asm label stdout
, which is equivalent to &stdout
in C. It's the static location in memory where the FILE*
value is stored.
只有C数组类型的行为类似于asm标签,其中C中的名称是地址,而不是内容.
Only C array types behave like asm labels, where the name in C is the address, not the contents.
另请参见 在NASM中,裸符号名称是静态地址.在C语言中,裸名是值.
(除了真正的C数组,裸名是第一个元素的地址.)
(Except for true C arrays, where the bare name is the address of the first element.)
在C语言中,具有自动存储类(即本地变量)的变量也可以具有名称,而不仅仅是静态名称.在asm中,符号只能在静态地址上使用. (C中的自动存储通常是x86 asm中的寄存器,或者如果需要溢出/重新加载,则为[ebp - 8]
之类的堆栈空间.堆栈地址不是链接时常数,因此不能有标签.您可以寻址相对于堆栈的地址到ESP或EBP.)
In C, variables with automatic storage class (i.e. local vars) can also have names, not just static. In asm, symbols can only go on static addresses. (Automatic storage in C is normally a register in x86 asm, or stack space like [ebp - 8]
if you need to spill/reload. Stack addresses aren't link-time constants, so can't have labels. You address the stack relative to ESP or EBP.)
具有动态存储的对象不能在C语言中具有名称,只能由已命名的指针指向.
Objects with dynamic storage can't have names in C, only be pointed to by named pointers.
这篇关于为什么我们必须在这里取消引用stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!