How can I add two 16 bit numbers in assembly language in microprocessor 8086

后端 未结 2 1783
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 13:13

Hey I am using window 7 x86. I want to add two 16 bit numbers.

When I add 3+3 its answer is correct but when I add 7+7 it\'s not working. A

相关标签:
2条回答
  • 2020-12-22 13:25

    With INT 21h Fn 02 you can get only one character. To receive more characters you must create a tricky loop. But there is another function in DOS: INT 21h Fn 0Ah. For conversion of a number greater than one digit you need two conversion routines - surely detailed explained in your schoolbook. Take a look at my example:

    .MODEL small
    .386
    
    .STACK 1000h
    
    .data
    
        num label
            max db len
            real db 0
            buf db 6 dup(0)             ; Input (5 digits) + CR
            len = $-buf
    
        db 'ENDE'
    
        int1 dw 0
        int2 dw 0
        int3 dw 0
    
        result db 6 dup ('$')           ; Output (5 digits) + CR
    
    .code
    
    main PROC
    
        mov ax,@data
        mov ds,ax                       ; Init DS
        mov es,ax                       ; Init ES for stosb
    
        mov dx, OFFSET num
        mov ah, 0Ah                     ; Input a string
        int 21h
    
        call dec2int
        mov [int1], ax
    
        mov dl, 0Ah                     ; Linefeed
        mov ah, 02h                     ; Cooked Output one character
        int 21h
    
        mov dx, OFFSET num
        mov ah, 0Ah                     ; Input a string
        int 21h
    
        call dec2int
        mov [int2], ax
    
        mov ax, [int1]                  ; first number
        add ax, [int2]                  ; add with second number
        mov [int3], ax                  ; Store result in [int3]
    
        mov dl, 0Ah                     ; Linefeed
        mov ah, 02h                     ; Cooked Output one character
        int 21h
    
        mov di, OFFSET result           ; [ES:DI] = receives the result string
        mov ax, [int3]                  ; AX = result from addition
        call int2dec
        mov dx, OFFSET result
        mov ah, 09h                     ; Output until '$'
        int 21h
    
        mov ax, 4C00h                   ; Exit(0)
        int 21h
    
    main ENDP
    
    dec2int PROC
        xor ax, ax                      ; AX receives the result
        mov si, OFFSET buf
        movzx cx, byte ptr [real]       ; Number of characters
        test cx, cx                     ; Buffer empty?
        jz _Ret                         ; yes: return with AX=0
    
        _Loop:                          ; Repeat: AX = AX * 10 + DX
            imul ax, 10
            mov dl, byte ptr [si]
            and dx, 000Fh               ; Convert ASCII to integer
            add ax, dx
            inc si
            loop _Loop
    
        _Ret:
        ret
    dec2int ENDP
    
    int2dec PROC
        mov bx, 10                      ; Base 10 -> divisor
        xor cx, cx                      ; CX=0 (number of digits)
      Loop_1:
        xor dx, dx                      ; No DX for division
        div bx                          ; AX = DX:AX / BX   Remainder DX
        push dx                         ; Push remainder for LIFO in Loop_2
        add cl, 1                       ; Equivalent to 'inc cl'
        or  ax, ax                      ; AX = 0?
        jnz Loop_1                      ; No: once more
      Loop_2:
        pop ax                          ; Get back pushed digits
        or ax, 00110000b                ; Conversion to ASCII
        stosb                           ; Store only AL to [ES:DI] (DI is a pointer to a string)
        loop Loop_2                     ; Until there are no digits left
        mov al, '$'                     ; Termination character for 'int 21h fn 09h'
        stosb                           ; Store AL
        ret
    int2dec ENDP
    
    END main
    
    0 讨论(0)
  • 2020-12-22 13:40

    Here is the code to add 2 16-bit numbers on 8086:

    .model small
    .data
    a db "Enter the first number$"
    b db "Enter the second number$"
    c db "The sum is: $"
    d db 00h
    
    .code
    start:
    mov ax,@data
    mov ds,ax
    mov dx,offset a
    mov ah,09h
    int 21h
    
    mov ah,01h
    int 21h
    mov bh,al
    mov ah,01h
    int 21h
    mov bl,al
    
    mov dx,offset b
    mov ah,09h
    int 21h
    mov ah,01h
    int 21h
    mov ch,al
    mov ah,01h
    int 21h  
    mov cl,al
    add al,bl
    mov ah,00h
    aaa
    add bh,ah
    add bh,ch
    mov d,al
    mov al,bh
    mov ah,00h
    aaa
    mov bx,ax
    add bx,3030h
    
    mov dx,offset c
    mov ah,09h
    int 21h
    
    mov dl,bh
    mov ah,02h
    int 21h
    mov dl,bl
    mov ah,02h
    int 21h
    mov dl,d
    add dl,30h
    mov ah,02h
    int 21h
    end start
    

    The trick here lies in using 'aaa' command to unpack the digits.

    0 讨论(0)
提交回复
热议问题