1

我试图更好地理解 nasm 中的堆栈,所以我制作了这个程序来尝试将“参数”传递给 nasm 中的“函数”。我对这个大会很陌生。

section .data
v0s0msg0:       db 'Enter something',10
v1t0msg0L:      equ $-v0s0msg0

section .bss
v2i0inp0        resb 256
v3v0temp0   resb 256

section .text
global _start
_start:
;This is a nasm program to help me understand the stack better
mov eax,4
mov ebx,1
mov ecx,v0s0msg0
mov edx,v1t0msg0L
int 80h

mov eax,3
mov ebx,0
mov ecx,v2i0inp0
mov edx,256
int 80h

push dword v2i0inp0
call f0m0test0

mov eax,1
mov ebx,0
int 80h

f0m0test0:
pop dword[v3v0temp0]
mov eax,4
mov ebx,1
mov ecx,v3v0temp0
mov edx,256
int 80h
ret 4

我可以组装它,链接它,然后运行它就好了,但是在运行它时,在我输入输入后,它只是在两个“?”之后说分段错误。寻找字符。

我试过改变

pop dword[v3v0temp0]

类似于:

pop v3v0temp0

甚至:

mov v3v0temp0,dword[ebp]

和许多类似的事情,但它们最终都作为分段错误,或者作为汇编器中的错误说:操作码和操作数的无效组合我非常感谢帮助使这个程序工作,也请解释一下堆栈,使用前缀“dword”,以及“[]”字符的用途。我想解释一下如何将堆栈用于“参数”。我在 linux 操作系统上运行这个,Ubuntu 提前谢谢你

4

1 回答 1

2
f0m0test0:
pop dword[v3v0temp0]

This pops the return address off the stack, not the parameter.

mov eax,4
mov ebx,1
mov ecx,v3v0temp0
mov edx,256
int 80h
ret 4

Since you've already poped something (though not the intended parameter) off stack, ret 4 above is almost definitely wrong.

I think you want just:

f0m0test0:
mov eax,4
mov ebx,1
mov ecx,[esp+4]
mov edx,256
int 80h
ret 4

Alternatively, instead of the callee cleaning up the parameter with ret 4, have the caller do it (which, I believe, is the usual calling convention):

push dword v2i0inp0
call f0m0test0
add esp,4
于 2012-06-24T17:09:39.477 回答