Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 使用组件中的循环求和数字1,2,3,4,5,6,7,8,9,10_Loops_Assembly_X86 16_Emu8086 - Fatal编程技术网

Loops 使用组件中的循环求和数字1,2,3,4,5,6,7,8,9,10

Loops 使用组件中的循环求和数字1,2,3,4,5,6,7,8,9,10,loops,assembly,x86-16,emu8086,Loops,Assembly,X86 16,Emu8086,我需要使用8086汇编中的循环对数字1,2,3,4,5,6,7,8,9,10求和。以下是我的尝试: MOV AX,01h MOV CX,0ah LABEL1: inc AX LOOP LABEL1 HLT 你需要某种容器来储存你的钱。您可以为此选择寄存器DX 首先确保在开始循环之前清空此寄存器。 然后在循环的每次迭代中,您将的当前值AX添加到此寄存器DX mov ax, 1 mov cx, 10 xor

我需要使用8086汇编中的循环对数字
1,2,3,4,5,6,7,8,9,10
求和。以下是我的尝试:

    MOV AX,01h
    MOV CX,0ah
LABEL1:
    inc AX
    LOOP LABEL1
    HLT  
你需要某种容器来储存你的钱。您可以为此选择寄存器
DX

首先确保在开始循环之前清空此寄存器。
然后在循环的每次迭代中,您将
的当前值
AX
添加到此寄存器
DX

    mov     ax, 1
    mov     cx, 10
    xor     dx, dx    ;This puts zero in DX
Label1:
    add     dx, ax    ;This adds int turn 1, 2, 3, ... ,10 to DX
    inc     ax
    loop    Label1
不确定是否需要使用
loop
指令,但另一个循环使用
AX
进行循环控制

    mov     ax, 1
    cwd               ;This puts zero in DX because AX holds a positive number
Label1:
    add     dx, ax    ;This adds in turn 1, 2, 3, ... ,10 to DX
    inc     ax
    cmp     ax, 10
    jbe     Label1
一个更好的循环将数字从高到低相加。这样就不再需要
cmp
指令

    mov     ax, 10
    cwd               ;This puts zero in DX because AX holds a positive number
Label1:
    add     dx, ax    ;This adds in turn 10, 9, 8, ... ,1 to DX
    dec     ax
    jnz     Label1

请提供一个明确的问题,而不是'MOV AX,01h',我将使用'MOV AX,00h',而不是
inc AX
,我将使用
ADD AX,CX
…我这样做了,我认为AX=37,但正确的答案是55@over如何在AX中打印结果?也许问题就在那里(我们看不到代码的那一部分)…你也可以使用
HLT
。。。我希望
RET
从程序或子程序返回…37H=55decimal@over我的两个循环的结果都是55(十进制)。您使用的调试器很可能是以十六进制显示结果,其中十进制55显示为十六进制37。(3 x 16+7)