Nasm程序分段错误

时间:2012-06-24 14:49:41

标签: linux stack segmentation-fault arguments nasm

我试图更好地理解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上运行它 提前谢谢

1 个答案:

答案 0 :(得分:2)

f0m0test0:
pop dword[v3v0temp0]

这会从堆栈中弹出返回地址,而不是参数。

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

由于您已经pop在堆栈上编辑了某些内容(虽然不是预期的参数),但上面的ret 4几乎肯定是错误的。

我想你只想:

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

或者,不是被调用者使用ret 4清理参数,而是让调用者执行此操作(我相信,这是通常的调用约定):

push dword v2i0inp0
call f0m0test0
add esp,4
相关问题