2 heatdeath HeatDeath 于 2016.04.25 14:00 提问

汇编求两个任意位数的十进制数字的差,并输出。结果总是输出10进制的补码是什么情况 1C
data segment
    buff1 db 20         ;存放最大字符个数
    n1 db ?             ;实际存放字符个数
    content1 db 20 dup(0)       ;存放输入字符
    buff2 db 20         ;buf=buffer缓冲区,content目录
    n2 db ?
    content2 db 20 dup(0)
    buff4 db 21 dup(0)      ;buff3用于存放结果
data ends

code segment
    assume cs:code,ds:data
start:
    mov ax,data
    mov ds,ax
buffa:
    mov dx,offset buff1     
    mov ah,0ah      
    int 21h

    mov dl,0ah          
    mov ah,2
    int 21h
buffb:
    mov dx,offset buff2 
    mov ah,0ah      
    int 21h

    mov cl,n1
    mov si,offset content1
transfer1:              
    sub byte ptr[si],30h        
    inc si          
    loop transfer1          
    dec si          
    mov di,si       

    mov cl,n2       
    mov si,offset content2      
transfer2:
    sub byte ptr[si],30h
    inc si
    loop transfer2
    dec si              
    mov bx,si       
;=====================================
subbbb:
    mov si,offset buff4
    mov dl,n1
    cmp dl,n2

    clc
    jg lowern2
    mov cl,n1
    mov dl,n2
;=========n1<=n2================
subln1:
    mov al,[di]
    sbb al,[bx]

    aas
    mov [si],al
    inc si
    dec di
    dec bx
    loop subln1
    pushf

    mov cl,n2
    sub cl,n1
    cmp cl,0
    je ifequ
    popf
;=========================
remain1n1:
    mov al,0
    sbb al,[bx]
    aas
    mov [si],al
    inc si
    dec bx
    loop remain1n1
    jmp done
;==========================
lowern2:
    mov cl,n2
    mov dl,n1
subln2:
    mov al,[di]
    sbb al,[bx]
    aas
    mov [si],al
    inc si
    dec di
    dec bx
    loop subln2
    pushf
    mov cl,n1
    sub cl,n2

remain1n2:
    mov al,0
    sbb al,[di]
    aas
    mov [si],al
    inc si
    dec di
    loop remain1n2
;=============================
done:
    jnc over
    jmp refinement
;===========================
ifequ:
    popf
    jnc over
;===========================
refinement:
    mov al,0
    sbb al,0
    mov [si],al
    inc dl
    inc si
;===========================
over:
    dec si
    mov cl,dl
    mov dl,0ah
    mov ah,2
    int 21h
;==============================
transfer3:
    add byte ptr[si],30h
    mov dl,[si]
    mov ah,2
    int 21h

    dec si
    loop transfer3
    mov ah,4ch
    int 21h
code ends
end start


;============================
该段代码的测试结果显示
当n1>n2时,且用0 填补空位,则输出结果正确

当n1<n2时,且用0 填补空位,则输出结果
例:n1=0123,n2=1234,结果显示为/8889,而8889=10000-(1234-0123)
且该规律始终不变


Csdn user default icon
上传中...
上传图片
插入图片