Debugging 在DOSBOX中使用调试汇编编程实现前5个数字的平方和

Debugging 在DOSBOX中使用调试汇编编程实现前5个数字的平方和,debugging,assembly,sum,x86-16,dosbox,Debugging,Assembly,Sum,X86 16,Dosbox,请帮助,我需要使用DOSBOX中的调试汇编编程计算前5个数字的平方和。这是一个2^2的示例: -a 100 </i> 15BC:0100 mov ax,1 15BC:0103 mov bx,2 ; Base Number 15BC:0106 mov cx,2 ; Exponent 15BC:0109 ;bucle 109: 15BC:0109 mov dx,0 15BC:010C m

请帮助,我需要使用DOSBOX中的调试汇编编程计算前5个数字的平方和。这是一个2^2的示例:

  -a 100 </i> 
       15BC:0100 mov ax,1 
       15BC:0103 mov bx,2 ; Base Number 
       15BC:0106 mov cx,2 ; Exponent 
       15BC:0109 ;bucle 109: 
       15BC:0109 mov dx,0  
       15BC:010C mul bx 
       15BC:010E loop 109  
       15BC:0110 int 20  
       15BC:0112 
       -g 110 
-a 100
公元前15年1月1日
15BC:0103 mov bx,2;基数
15BC:0106 mov cx,2;指数
公元前15年:0109;bucle 109:
15BC:0109 mov dx,0
15BC:010C mul bx
15BC:010E回路109
公元前15年:0110 int 20
公元前15:0112
-g 110

指令
mul bx
ax
的内容(始终)与
bx
寄存器(参数)的内容相乘。因为是16位寄存器,结果最多可以有32位。此结果的低16位放在
ax
中,高16位放在
dx
中。因此,您不必在
mul
之前清除
dx
。这仅适用于
div

然后,计算ax=3²的代码(一般来说,
ax=bx
cx
)为:

    mov  ax, 1
    mov  bx, 3  ; Base Number (that is multiplied by itself cx plus one times)
    mov  cx, 2  ; Exponent (how often the multiplication should be repeated)
expLoop:
    mul  bx
    loop expLoop
如果只计算平方,则不再需要循环,代码可以简化:

    mov  ax, 3 ; Base Number (that is multiplied by itself one time)
    mul  ax
这将计算
(通常
ax²
)。注意这里不再使用
bx
cx

正如Michael已经提到的,如果你想得到一个平方和,你必须在这之后对你的结果求和。因为不再使用
cx
,它可以与
loop
一起使用,以迭代所有要平方的数字
bx
可用于存储平方和:

    xor  bx, bx  ; Sum is initialised to zero
    mov  cx, 5   ; Iterate from 5 down to 1
iterate:
    mov  ax, cx
    mul  cx      ; Calculate the square of the acutal number
    add  bx, ax  ; Sum up the squares in bx
    loop iterate

指令
mul bx
ax
(始终)的内容与
bx
寄存器(参数)的内容相乘。因为是16位寄存器,结果最多可以有32位。此结果的低16位放在
ax
中,高16位放在
dx
中。因此,您不必在
mul
之前清除
dx
。这仅适用于
div

然后,计算ax=3²的代码(一般来说,
ax=bx
cx
)为:

    mov  ax, 1
    mov  bx, 3  ; Base Number (that is multiplied by itself cx plus one times)
    mov  cx, 2  ; Exponent (how often the multiplication should be repeated)
expLoop:
    mul  bx
    loop expLoop
如果只计算平方,则不再需要循环,代码可以简化:

    mov  ax, 3 ; Base Number (that is multiplied by itself one time)
    mul  ax
这将计算
(通常
ax²
)。注意这里不再使用
bx
cx

正如Michael已经提到的,如果你想得到一个平方和,你必须在这之后对你的结果求和。因为不再使用
cx
,它可以与
loop
一起使用,以迭代所有要平方的数字
bx
可用于存储平方和:

    xor  bx, bx  ; Sum is initialised to zero
    mov  cx, 5   ; Iterate from 5 down to 1
iterate:
    mov  ax, cx
    mul  cx      ; Calculate the square of the acutal number
    add  bx, ax  ; Sum up the squares in bx
    loop iterate

那么,你将如何用更高级的语言来写呢?你肯定需要在某个地方添加一个吗?那么,你将如何用更高级的语言编写呢?你肯定需要在什么地方加一个?