Debugging 在DOSBOX中使用调试汇编编程实现前5个数字的平方和
请帮助,我需要使用DOSBOX中的调试汇编编程计算前5个数字的平方和。这是一个2^2的示例: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
-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
这将计算3²
(通常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
这将计算3²
(通常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
那么,你将如何用更高级的语言来写呢?你肯定需要在某个地方添加一个吗?那么,你将如何用更高级的语言编写呢?你肯定需要在什么地方加一个?