简体   繁体   中英

Why is the sum of numbers displayed incorrectly?

I have numbers like 5000 and 5001, and I need to add them. I added the numbers but the result of the output is wrong.

s3       DW  5000
s4       DW 5001
s5       DW 0
        mov ax, s3
        add ax, s4
        mov bx, 10
        div bx
        add dx, '0'
        mov s5+3, dx
        mov dx, 0
        mov bx, 10
        div bx
        add dx, '0'
        mov s5+2, dx
        mov dx, 0
        mov bx, 10
        div bx
        add ax, '00'
        mov s5, ax
        mov s5+1, dx
        mov s5+4, '$'
        LEA DX, s5
        mov ah, 9
        INT 21h
s3  DW 5000
s4  DW 5001
s5  DB '00000$'   ; Reserve space for 5 digits, $-terminated.

   mov ax, [s3]   ; AX=5000.
   add ax, [s4]   ; AX is now 5000+5001=10001, five decimal digits to display.
   MOV DX, 0      ; Do not relay on undefined register contents.
   mov bx, 10
   div bx         ; AX=1000, DX=1.
   add dx, '0'    ; DX=0x0031
 ; mov [s5+3], dx ; Wrong, the last (fifth) digit of the sum should go from DL to [s5+4].
   MOV [s5+4],DL
   mov dx, 0
 ; mov bx, 10     ; This is redundand, BX=10 unchanged.
   div bx         ; AX=100, DX=0.
   add dx, '0'
 ; mov [s5+2], dx ; Wrong, the fourth digit of the sum should go from DL to [s5+3].
   MOV [s5+3],DL
   mov dx, 0
 ; mov bx, 10     ; This is redundand, BX=10 unchanged.
   div bx         ; AX=10, DX=0.
 ; add ax, '00'
 ; mov [s5], ax   ; Wrong, the third digit of the sum should go from DL to [s5+2].
   ADD DL, '0'
   MOV [s5+2],DL
   MOV DX, 0
   DIV BX          ; AX=1, DX=0.
   ADD DL, '0'
 ; mov [s5+1], dx  ; Wrong, the second digit of the sum should go from DL to [s5+1].
   MOV [s5+1], DL
   ADD AL,'0'      ; The first digit of the sum should go from AL to [s5+0].
   MOV [s5+0], AL
 ; mov [s5+4], '$' ; Wrong, this would overwrite the fifth digit of the sum.
   LEA DX, [s5]
   mov ah, 9       ; Display the sum at s5.
   INT 21h
   MOV AH,4Ch      ; Don't forget return to DOS.
   INT 21h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM