Why does this loop show 20 as the result instead of 120?
ORG 100H
facto dw ?
START:
MOV CX, 5
MOV AX, 5
DEC CX
repeat :
MUL CX
MOV facto , AX
LOOP repeat
ENDS
END START :
Why does this loop show 20 as the result instead of 120?
ORG 100H
facto dw ?
START:
MOV CX, 5
MOV AX, 5
DEC CX
repeat :
MUL CX
MOV facto , AX
LOOP repeat
ENDS
END START :
The code works fine, although with a few unneeded commands. Here is the full code for testing in FASM. You'll see that I combined mov cx, 5 and dec cx into mov cx, 4 and moved assignment from ax to facto away from the loop to avoid unnecessary code execution.
format PE console
entry main
include '%INCLUDE%/win32a.inc'
section '.data' data readable writeable
facto dd 0
section '.text' code readable executable
main:
mov ecx, 4
mov eax, 5
L0:
mul ecx
loop L0
mov [facto], eax
invoke ExitProcess, eax
section '.idata' data import readable
library kernel32,'kernel32.dll'
import kernel32,\
ExitProcess,'ExitProcess'