离开指令返回一个seg错误

时间:2022-09-03 02:48:07

I have a routine that perform calls to a routine where a value into ebp register is expected.I change it several times in the routine.So,I can't do: mov ebp,esp in the top of code.I tried by saving the esp address in another register and before leave instruction put such address into ebp but I still get a segmentaion fault.

我有一个例程,它执行对一个例程的调用,在这个例程中,ebp寄存器的值是预期的。我改变了好几次。所以,我不能这样做:mov ebp,esp在代码的顶部。我尝试将esp地址保存在另一个寄存器中,在离开指令之前将该地址放入ebp中,但是我仍然有一个segmentaion错误。

How to fix this?

如何解决这个问题?

the code is very huge(to post here,but I can post if needed),but the idea is something like this:

代码非常庞大(在这里发布,但如果需要的话我可以发布),但是想法是这样的:

;a lot of arguments are passed in stack
foo:
xor ebx,ebx
mov ecx,esp
loop0:
sub edx,1
jz end
;etc
mov eax,[esp+ebx]
mov ebp,eax
call routinex
;etc..
;...
mov ebp,edx
call printx
add ebx,4
jmp loop0
end:
mov ebp,ecx
leave
ret

I hope this code is enough for you get the idea.. I also like an explanation.

我希望这段代码能让你明白我的意思。我也喜欢一个解释。

1 个解决方案

#1


3  

What leave does is this:

休假的作用是:

mov esp, ebp
pop ebp

If you want to use it and have it work, you should write something like

如果你想要使用它并让它工作,你应该写一些类似的东西

push ebp
mov ebp, ecx
leave

At the end of your function. Of course, you could just dispense with using leave altogether if you do:

在函数的末尾。当然,如果你这样做,你完全可以省去休假:

mov esp, ecx
ret

since you are using ecx as your frame pointer.

因为您正在使用ecx作为您的帧指针。

#1


3  

What leave does is this:

休假的作用是:

mov esp, ebp
pop ebp

If you want to use it and have it work, you should write something like

如果你想要使用它并让它工作,你应该写一些类似的东西

push ebp
mov ebp, ecx
leave

At the end of your function. Of course, you could just dispense with using leave altogether if you do:

在函数的末尾。当然,如果你这样做,你完全可以省去休假:

mov esp, ecx
ret

since you are using ecx as your frame pointer.

因为您正在使用ecx作为您的帧指针。