我试图使用jmp和jz在汇编中编写一个阶乘代码,并且我是汇编的新手
输入应为eax,输出为ebx
mov eax,3h ;factorial of 3
;initialize registers
mov ebx,0h
mov ecx,eax
dec ecx
mov edx,0h
loopstart:
mul ecx ;edx:eax = eax * ecx
add ebx,eax ;ebx = ebx + eax
dec ecx ;ecx = ecx - 1
jz exitloop ;jump to exitloop if the last math is zero ( in this ex:dec)
jmp loopstart ;jump to loopstart
exitloop:
;mov eax,ebx
call ebxprint; should be 6
找出eax的阶乘
输入:2
输出:2
输入:3
输出:12
输入:4
输出:60
那么,这里的问题是什么?
答案 0 :(得分:0)
感谢@PaulHankin
我必须删除add ebx, eax
并取消注释mov eax,ebx
以下是编辑后的代码:
mov eax,3h ;factorial of 3
;initialize registers
mov ebx,0h
mov ecx,eax
dec ecx
mov edx,0h
loopstart:
mul ecx ;edx:eax = eax * ecx
dec ecx ;ecx = ecx - 1
jz exitloop ;jump to exitloop if the last math is zero ( in this ex:dec)
jmp loopstart ;jump to loopstart
exitloop:
mov eax,ebx
call ebxprint; should be 6