简体   繁体   English

为什么这个循环的结果是 20 而不是 120?

[英]Why does this loop show 20 as the result instead of 120?

Why does this loop show 20 as the result instead of 120?为什么这个循环的结果是 20 而不是 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.这是在 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.您会看到我将 mov cx, 5 和 dec cx 组合成 mov cx, 4 并将赋值从 ax 移到远离循环的 facto 以避免不必要的代码执行。

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM