x86 32位汇编问题



我目前正在学习汇编,我正在玩if语句。我当前的代码如下:

write:
mov eax, 0x4     
sub esp, 4       
int 0x80         
main:
    ; The message has already been pushed to the stack
mov eax, 4
inc eax
cmp eax, 5
je write  

如果我把ret放在write:的末尾,那么我得到一个总线错误10,如果我不这样做,我得到一个无限循环,导致分割错误。我该怎么做才能实现这个目标?

call指令代替je指令进入writeret期望返回地址在堆栈上,但是如果您使用跳转到那里,它不会被推入!你也必须把esp放回你进入函数时的状态。下面是基于您的代码的最佳猜测示例:

write:
  mov eax, 0x4
  sub esp, 4       
  int 0x80
  add esp, 4
  ret
main:  ; The message has already been pushed to the stack
  mov eax, 4
  inc eax
  cmp eax, 5
  jne dontwrite  ; skip calling 'write' if eax != 5
  call write
dontwrite:
       ; the rest of the program goes here

试试这个。在您的示例中不需要调用过程。

main:  ; The message has already been pushed to the stack
  mov eax, 4
  inc eax
  cmp eax, 5
  jne dontwrite   ; Skip Write
  ; Write
  mov eax, 0x4
  sub esp, 4
  int 0x80
dontwrite:
       ; the rest of the program goes here

最新更新