程序集递归过程中的无限循环

时间:2019-06-10 09:39:46

标签: assembly x86 masm

在递归过程中添加WriteDec和CRLF调用时,它将进入无限循环。

  1. 我的动机是每次在右移后立即打印eax和crlf的值。
  2. 达到0时,似乎存在无限递归调用,因为它继续打印0。
  3. 我的印象是,由于ZF = 1第一次eax = 0,所以情况并非如此,因此它跳转到跳过标签的位置,从而结束了递归操作。

以下代码在EAX寄存器= 0时导致无限循环

 .code 
MAIN PROC

mov ecx, 10

L1:

push 10
call f1

call exitProcess
main ENDP

f1 PROC 

push ebp
mov ebp, esp
sub esp, 4
mov eax, [ebp+8]
shr eax, 1



call WriteDec
call CRLF

mov [ebp-4], eax
jz skip
call f1

skip:
mov eax, [ebp+8]
call WriteDec
call CRLF

; ** comment out ** mov ebp, [ebp]
mov esp, ebp
pop ebp
ret 4
f1 ENDP

END MAIN

预期结果:

5
2
1
0
1
2
5 
10

1 个答案:

答案 0 :(得分:2)

罪魁祸首在这里:

IEnumerable<EnumDto> genderDtos = dtos.Get<Gender>();
IEnumerable<EnumDto> maritalStatusDtos = dtos.Get<MaritalStatus>();
IEnumerable<EnumDto> residentialStatusDtos = dtos.Get<ResidentialStatus>();

在x86中,MOV指令未设置任何标志,因此JZ将采用上面该函数调用遗留的ZF标志的状态。

正确的代码如下:

mov [ebp-4], eax
jz skip