是的,您可以将任何 DWORD 值(在 32 位汇编程序中)压入堆栈。
第一个代码片段中的问题是,它printf期望第一个参数是格式字符串(在 C 中,你会写printf("%d\n", 10);)。所以像
section .data
fmt db "%d", 10, 0
...
push 10
push fmt
call printf
add esp, 8
将工作。
在第二个代码片段中,push [value]你应该写而不是写push dword [value],但如果你的value变量是单字节,那是不正确的。要么将其声明为 DWORD ( dd),要么执行
movsx eax, byte [value] ; if it's a signed integer; movzx for unsigned
push eax
还有一件事情。调用printf(或任何 C 库函数)时,请注意堆栈对齐。一些平台要求在函数调用时堆栈是 16 字节对齐的(这是正确执行优化的 CPU 指令(如 SSE)所必需的)。因此,要使堆栈对齐:
push ebp
mov ebp, esp
sub esp, 8 ; reserve 8 bytes for parameters
and esp, -16 ; align the stack (the reserved space can increase)
mov dword [esp], fmt ; put parameters into stack
mov dword [esp+4], 10
call printf
mov esp, ebp ; restore stack
pop ebp