簡體   English   中英

如何在微處理器8086中以匯編語言添加兩個16位數字

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

嘿,我正在使用Windows 7 x86。 我想添加兩個16位數字。

當我添加3+3其答案是正確的,但是當我添加7+7時,其答案無效。 我想添加兩個數字,例如75+75其答案應為150。

請問我的程序是什么。 提前感謝

.model small
.stack 100h
.data
num db 9 dup(0)
result dw 9 dup (0)
.code
main proc
mov ax,@data
mov ds,ax

mov ah, 1
int 21h    ; get input from user
mov num, al    ; store in the array 

int 21h              ;get 2nd number from user
mov num+1, al        ;store in the array at num[1] index

mov al, num          ;mov number into al
add dl, num+1        ;add num[1] in the num which is in dl

sub dl, 48           ; subract from assci so it become number 0 ~ 9

mov ah, 2            ; output
int 21h

mov ah, 4ch
int 21h
main endp
end main

這是在8086上添加2個16位數字的代碼:

.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

這里的竅門在於使用“ aaa”命令解壓數字。

使用INT 21h Fn 02您只能獲得一個字符。 要接收更多字符,您必須創建一個棘手的循環。 但是DOS中還有另一個功能: INT 21h Fn 0Ah 要進行大於一位數字的轉換,您需要兩個轉換例程-教科書中對此進行了詳細說明。 看一下我的例子:

.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM