+ -
当前位置:首页 → 问答吧 → 问一题汇编加法进位的问题

问一题汇编加法进位的问题

时间:2011-05-31

来源:互联网

编写16位的汇编,
要求输入两个16位的数,输出求和
Assembly code

;******************************************************
;***  作业2:                                       ***
;***  输入两个十六进制数(<=FFFF),输出两数的和。   ***
;***  要考虑进位                                    ***
;******************************************************

data segment
x dw 0
y dw 0
z dw 0,0
sx db "0000",0
sy db "0000",0
sz db "000000",0
sa db "Please input two hex number: ",0
data ends
code segment
assume cs:code,ds:data
main:     mov ax,data
        mov ds,ax
        mov di,offset sa
        call output
        mov di,offset sx
        call input
        mov di,offset sy
        call input
        mov si,offset sx
        call atoi
        mov [x],ax
        mov si,offset sy
        call atoi
        mov [y], ax
        clc
        mov ax,[x]
        adc ax,[y]
        ;mov [z+1],ax
        mov di,offset sz
        call itoa
        mov di,offset sz
        call output
        mov ah,4ch
        int 21h
    
    input:    
        mov ah, 1
        int 21h
        cmp al, 0Dh
        je input_done
        mov [di], al
        inc di
        jmp input
        input_done:    
            mov byte ptr [di], 0
            mov ah,2
            mov dl,0dh
            int 21h
            mov ah,2
            mov dl,0ah
            int 21h
            ret

    output:    
        mov ah, 2
        mov dl, [di]
        cmp dl, 0
        je output_done
        int 21h
        inc di
        jmp output
        output_done:
            mov ah, 2
            mov dl, 0Dh
            int 21h
            mov ah, 2
            mov dl, 0Ah
            int 21h
            ret
    
    atoi:
        mov ax,0
        mov bx,0
        atoi_next:
            mov bl,[si]
            cmp bl,0
            je atoi_done
            mov cx,16
            mul cx
            cmp bl,'9'
            ja hexatof
            cmp bl,'9'
            jbe hex1to9
            hexatof:    
                sub bl,'a'
                add bl,10
                jmp atoinext
            hex1to9:
                sub bl,'0'
                jmp atoinext
            atoinext:
                add ax,bx
                inc si
                jmp atoi_next
        atoi_done:
            ret
    
    itoa:
        mov cx, 0 
        mov si, 16
        jo overflow        
        itoa_next:
            mov dx, 0
            div si
            push dx
            inc cx
            cmp ax, 0
            je itoa_pop
            jmp itoa_next
        itoa_pop:
            pop dx
            cmp dl,9
            ja atofhex
            cmp dl,9
            jbe to9hex
            atofhex:
                sub dl,10
                add dl,'a'
                jmp itoanext
            to9hex:
                add dl,'0'
                jmp itoanext
            itoanext:
                mov [di], dl
                inc di
                dec cx
                jnz itoa_pop
                mov byte ptr [di], 0
                ret                
        overflow:
            mov byte ptr [di],'1'
            jmp itoa_next
            
    code ends
end main


16进制的加法还好写,但是如果 ffff+1,有进位 1 ,怎么让这个‘1’也一起输出呢。。。

PS:16位的汇编,别说去用32位的寄存器……

作者: C_Alasad   发布时间: 2011-05-31

有个adc指令用于相加和大于16位的
加法

作者: MSOKD   发布时间: 2011-05-31