急求:用汇编语言编写一个多字节非压缩BCD加法程序,要求有注释哦,万分感谢!!!十万火急!!!

2024-10-28 14:22:13
推荐回答(2个)
回答1:

在一楼代码的基础上调整,完全满足你的要求。



        CODE  SEGMENT
              ASSUME    CS:CODE
   
           X  DB        7H,8H,9H
           Y  DB        9H,8H,7H
           N  EQU       $-Y
           Z  DB        N+1 DUP(0)
   
      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES          ; 使数据段、附加段与代码在同一段中
              MOV       CX,N        ; 字节数置入cx
              MOV       BX,0        ; 基址寄存器置0
              MOV       AH,0        ; 清进位借位标志 (ASCII 调整指令,进位存在ah中)
              CLC
         @0:
              MOV       AL,X[BX]    ; 将被加数的低字节移入al
              ADD       AL,Y[BX]    ; 加加数的低字节
              ADD       AL,AH       ; 加进位
              MOV       AH,0        ; 清进位
              CMP       AL,9
              JBE       @001
              ADD       AL,6
              AND       AL,0FH
              MOV       AH,1        ; ASCii码 加法调整
       @001:
              MOV       Z[BX],AL    ; 将结果移入和的低字节
              INC       BX          ; 基址寄存器加1
              LOOP      @0          ; 循环
                
              ADD       BYTE PTR Z[BX],AH       ; 处理最高位
        @01:
              LEA       SI,Z        ; 将和的首地址置入 si
              MOV       CX,N+1      ; 字节数置入 cx
              CLD
         @1:
              LODSB                 ; 装入和的低字节到 al
              CALL      DSPAL       ; 调用子程序以16进制形式显示(其实是显示bcd数)
              LOOP      @1          ; 循环
   
              MOV       AH,4CH
              INT       21H
   
;=============================
       DSPAL  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSHF
              MOV       CX,2        ; 一个字节只有2位的16进制数
              MOV       BX,AX       ; 将al 保存到 bl 中
    @DSPAL1:
              ROR       BL,1        ; 循环将bl 右移4次 ,即将高位移到右侧
              ROR       BL,1
              ROR       BL,1
              ROR       BL,1
              MOV       DL,BL       ; 放到 dl 中
              AND       DL,0FH      ; 清掉 高位
              CMP       DL,9        ; 和9比较
              JA        @DSPAL2     ; 大于 即在 A~F 之间
              ADD       DL,30H      ; 否则就是 0~9,加30H变为 ASCii码
              JMP       @DSPAL3
    @DSPAL2:
              ADD       DL,37H      ; 加 37H 变为 A~F 的ASCii码
    @DSPAL3:
              MOV       AH,2
              INT       21H
              LOOP      @DSPAL1     ; 循环输出 低位
              MOV       AH,2
              MOV       DL,'H'      ; 显示 H
              INT       21H
              MOV       DL,32       ; 显示空格
              INT       21H
              POPF
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
       DSPAL  ENDP
   
        CODE  ENDS
              END       START

回答2:

        CODE  SEGMENT
              ASSUME    CS:CODE
  
           X  DB        12H,34H,56H
           Y  DB        65H,98H,98H
           N  EQU       $-Y
           Z  DB        N+1 DUP(0)
  
      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES          ; 使数据段、附加段与代码在同一段中
              MOV       CX,N        ; 字节数置入cx
              MOV       BX,0        ; 基址寄存器置0
              CLC                   ; 清进位借位标志
         @0:
              MOV       AL,X[BX]    ; 将被加数的低字节移入al
              ADC       AL,Y[BX]    ; 带进未的加,加加数的低字节
              DAA
              MOV       Z[BX],AL    ; 将结果移入和的低字节
              INC       BX          ; 基址寄存器加1
              LOOP      @0          ; 循环
               
              MOV       BYTE PTR Z[BX],0        ; 处理最高位
              JNC       @01
              MOV       BYTE PTR Z[BX], 1
        @01:
              LEA       SI,Z        ; 将和的首地址置入 si
              MOV       CX,N+1      ; 字节数置入 cx
              CLD
         @1:
              LODSB                 ; 装入和的低字节到 al
              CALL      DSPAL       ; 调用子程序以16进制形式显示(其实是显示bcd数)
              LOOP      @1          ; 循环
  
              MOV       AH,4CH
              INT       21H
  
;=============================
       DSPAL  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSHF
              MOV       CX,2        ; 一个字节只有2位的16进制数
              MOV       BX,AX       ; 将al 保存到 bl 中
    @DSPAL1:
              ROR       BL,1        ; 循环将bl 右移4次 ,即将高位移到右侧
              ROR       BL,1
              ROR       BL,1
              ROR       BL,1
              MOV       DL,BL       ; 放到 dl 中
              AND       DL,0FH      ; 清掉 高位
              CMP       DL,9        ; 和9比较
              JA        @DSPAL2     ; 大于 即在 A~F 之间
              ADD       DL,30H      ; 否则就是 0~9,加30H变为 ASCii码
              JMP       @DSPAL3
    @DSPAL2:
              ADD       DL,37H      ; 加 37H 变为 A~F 的ASCii码
    @DSPAL3:
              MOV       AH,2
              INT       21H
              LOOP      @DSPAL1     ; 循环输出 低位
              MOV       AH,2
              MOV       DL,'H'      ; 显示 H
              INT       21H
              MOV       DL,32       ; 显示空格
              INT       21H
              POPF
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
       DSPAL  ENDP
  
        CODE  ENDS
              END       START