使用DEBUG的前5个数字的平方和-DOSBOX中的汇编编程

问题描述

请帮助,我需要使用DEBUG-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 

解决方法

指令mul bxax的内容(总是)与bx寄存器(参数)的内容相乘。因为是16位寄存器,结果最多可以有32位。此结果的低16位放在ax中,高16位放在dx中。因此,您不必在dx之前清除mul。这仅对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²)。请注意,这里不再使用bxcx

正如迈克尔已经提到的那样,如果要求平方和,就必须在这之后总结结果。原因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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...