-2

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 :
Kevin Wang
  • 2,673
  • 2
  • 10
  • 18
  • Works for me, AX = 120 = 0x78 at the end of the loop. (I tested with NASM in a Linux static executable, and single-stepped with GDB). Not a [mcve]. – Peter Cordes Mar 26 '19 at 22:22

1 Answers1

1

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'