Two 16-bit binary subtraction Simulation
Function Description: enter two 16-bit binary codes to simulate two 16-bit binary integers. Evaluate the difference and output the result in hexadecimal notation.
Basic algorithm: uses the Complement Algorithm to simulate the two-mechanism subtraction (similar to addition). After the difference (complement Code) is converted to the original code, it is converted to hexadecimal format and output. the 16-bit binary is converted into a hexadecimal number by dividing the 16-bit binary into four groups, each of which is composed of four consecutive binary numbers, simulate 4-bit binary to one-bit hexadecimal conversion. convert each group to a hexadecimal number and output it. for details, see the code.
This code is successfully debugged in masm.5.
Running interface:
For example
Binary (Yuanma) num1 = 1000000000001011; subtrahend
Binary (Yuanma) num2 = 1000000000001101; subtrahend
Binary (buma) num1 Bm = 1111111111110101; subtrahend
Binary (buma) num2 Bm = 1111111111110011; subtrahend
Binary (buma) diff sub = 0000000000000010; Difference Complement
Binary (Yuanma) diffsub = 0000000000000010; bad original code
Hex sumhex= 0002
; /*************************************** *********/
;
; Bsub. ASM
Hbyufan@163.com
;
Tsout macro dx0
MoV ah, 09 h
MoV dx, offset dx0
Int 21 h
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, 01 H
Int 21 h
CMP Al, '0'; simple processing of num1 input error --- re-Input
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, 01 H
Int 21 h
CMP Al, '0'; simple processing of num1 input error --- re-Input
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 21 h
; **************************************** ***
Questbuma proc near; function: Complement
Algorithm: simulates the complement operation, for example, the binary method is the same.
; BX as the parameter, input the first address of the num1 or num2 memory space
MoV Al, [bx + 0]
CMP Al, '0'
JZ qbret; if it is a positive integer, the function returns; if it is a negative number, the symbol bit remains unchanged, and the remaining bits are reversed with 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; Add 1 for processing. The method is similar to subtraction.
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 qbinre
Qbret:
RET
Questbuma endp
; **************************************** ***
Bsubproc proc near; function: 16-bit binary number subtraction simulation.
; Algorithm: Subtraction by bit, flag is the borrow sign (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; function name
; BX as the parameter, input the first address of the num1 or num2 memory space
MoV DH, 2
MoV Si, 0
Bth1:
MoV Al, 0; group conversion, simulating conversion from binary 4 to hexadecimal 1
MoV Di, 0
Bth1:
Mul DH
Add Al, [BX] [di]
Sub Al, '0'
INC di
CMP Di, 4
Jl b22.
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
<
Learn proverbs:
1. It is dangerous
2. If a language cannot affect your thoughts on programming, it is not worth learning.
>>>