功能說明:輸入兩組16位二進制代碼模擬兩為16位二進制整數,對其求差並以16進制輸出.
基本算法:采用補碼,模擬二機制減法(加法類同),得到差(補碼)轉換成原碼後,轉化成十六進制並輸出.16為二進制轉換成4為十六進制數的方法是采用:將16位二進制分成4組,每組由連續4位二進制組成,模擬4位二進制到一位十六進制轉換,將每組轉換成16進制數並輸出.詳見代碼
本代碼在MASM.5調試通過
運行界面:
如
binary(YuanMa) num1=1000000000001011 ;被減數原碼
binary(YuanMa) num2=1000000000001101 ;減數原碼
binary(Buma) num1 BM=1111111111110101 ;被減數補碼
binary(Buma) num2 BM=1111111111110011 ;減數補碼
binary(Buma) diff sub=0000000000000010 ;差補碼
binary(YuanMa)diffsub=0000000000000010 ;差原碼
hex sumHEX=0002
;/************************************************/
;
;bsub.asm
;[email protected]
;
TSOUT MACRO dx0
mov ah,09h
mov dx,offset dx0
int 21h
ENDM
data segment
num1 db 19 dup(''$'')
num2 db 19 dup(''$'')
sum db 19 dup(''$'')
hexsum db 5 dup(''$'')
tsnum1 db 0ah,0dh,''binary(YuanMa) num1=$''
tsnum2 db 0ah,0dh,''binary(YuanMa) num2=$''
tssum db 0ah,0dh,''binary(Buma) diff sub=$''
tsnum11 db 0ah,0dh,''binary(Buma) num1 BM=$''
tsnum22 db 0ah,0dh,''binary(Buma) num2 BM=$''
tssum33 db 0ah,0dh,''binary(YuanMa)diffsub=$''
tssumhex db 0ah,0dh,''hex sumHEX=$''
data ends
code segment
assume ds:data,cs:code
start:
mov ax,data
mov ds,ax
re:
TSOUT tsnum1
mov si,0
n1roat:
mov ah,01h
int 21h
cmp al,''0'' ;對NUM1輸入錯誤簡單處理---重新輸入
jl re
cmp al,''1''
ja re
mov num1[si],al
inc si
cmp si,16
jl n1roat
re2:
TSOUT tsnum2
mov si,0
n2roat:
mov ah,01h
int 21h
cmp al,''0'' ;對NUM1輸入錯誤簡單處理---重新輸入
jl re2
cmp al,''1''
ja re2
mov num2[si],al
inc si
cmp si,16
jl n2roat
mov bx,offset num1
call questBuma
mov bx,offset num2
call questBuma
TSOUT tsnum11;//
TSOUT num1;//
TSOUT tsnum22;//
TSOUT num2;//
call bsubproc
TSOUT tssum;
TSOUT sum ;//test
lea bx,sum
call questBuma
TSOUT tssum33;//
TSOUT sum;//
lea bx,sum
call BinaryToHexAndOut
TSOUT tssumhex;//
TSOUT hexsum;//
;******************************
mov ah,4ch
int 21h
;*******************************************
questBuma proc near ;功能:求補碼
;算法:模擬求補碼運算,如二進制求補碼方法一致.
;bx ;bx作為參數,傳入NUM1或NUM2內存空間首地址
mov al,[bx+0]
cmp al,''0''
jz qbret ;如果是正整數,函數返回;如果是負數,符號位不變,其余位取反加1.
mov si,1
qbroat:
mov al,[bx][si]
cmp al,''0''
jz qb1
mov al,''0''
mov [bx][si],al
jmp qb2
qb1:
mov al,''1''
mov [bx][si],al
qb2:
inc si
cmp si,16
jl qbroat
qnext:
mov dh,1 ;加1處理.方法類似減法
mov si,15
qbincre:
mov al,[bx][si]
sub al,''0''
add al,dh
mov dh,0
cmp al,2
jnz qbi1
mov al,0
mov dh,1
qbi1:
add al,''0''
mov [bx][si],al
dec si
cmp si,0
jnl qbincre
qbret:
ret
questBuma endp
;*******************************************
bsubproc proc near ;功能:16位二進制數減法模擬.
;算法:逐位相減,flag為借位標志(BX)
push ax
push bx
push cx
push si
mov bx,0 ;//flag
mov si,15
broat: mov al,num1[si]
add al,bh
mov bh,0 ;//clear bh
cmp al,num2[si]
jnl bnext
add al,2
dec bh
bnext:
sub al,num2[si]
add al,''0''
mov sum[si],al
dec si
cmp si,0
jnl broat
pop si
pop cx
pop bx
pop ax
ret
bsubproc endp
;*******************************************
BinaryToHexAndOut proc near ;功能如函數名
;bx ;bx作為參數,傳入NUM1或NUM2內存空間首地址
mov dh,2
mov si,0
bth1:
mov al,0 ;分組轉換,模擬4為二進制到1為16進制數的轉換
mov di,0
bth2:
mul dh
add al,[bx][di]
sub al,''0''
inc di
cmp di,4
jl bth2
cmp al,9
jna bth3
sub al,10
add al,''A''
jmp bth4
bth3:
add al,''0''
bth4:
mov hexsum[si],al
inc si
add bx,4
cmp si,4
jl bth1
ret
BinaryToHexAndOut endp
;*******************************************
code ends
end start
<<<<
學習箴言:
1,一知半解是危險的
2,如果一門語言不能影響你對編程的想法,那它就不值得去學
>>>