masm-two 16-bit binary number subtraction simulation

Source: Internet
Author: User
Tags diff

Function Description: Enter two sets of 16-bit binary code to simulate two 16-bit binary integers, and to find the difference and 16 output.

Basic algorithm: Using complement, simulate the second mechanism subtraction (additive similarity), the difference (complement) is converted to the original code, and then converted to hexadecimal and output. 16 The method of converting the binary to 4 is hexadecimal: The 16-bit binary is divided into 4 groups, each of which consists of successive 4-bit binaries, Simulates a 4-bit binary to a 16-bit conversion, converts each group into a 16-digit number and outputs it. See Code

This code in MASM.5 debugging through

Run the interface:

Such as

Binary (Yuanma) num1=1000000000001011; the original code
Binary (Yuanma) num2=1000000000001101;
Binary (Buma) NUM1 bm=1111111111110101;
Binary (Buma) num2 bm=1111111111110011;
Binary (Buma) diff sub=0000000000000010; difference complement
Binary (Yuanma) diffsub=0000000000000010; difference Original code
Hex sumhex=0002
;/************************************************/
;
; bsub.asm
; hbyufan@163.com
;
Tsout MACRO dx0
MOV ah,09h
MOV Dx,offset dx0
int 21h
Endm
Data segment
NUM1 db DUP (' $ ')
NUM2 db DUP (' $ ')
Sum db 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 '; simple processing of NUM1 input error---Re-enter
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 '; simple processing of NUM1 input error---Re-enter
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; function: to find the complement
Algorithm: Simulation of the complement operation, such as the binary method to find the complement of the same.
BX, BX as a parameter, incoming NUM1 or NUM2 memory space first address
MOV al,[bx+0]
CMP al, ' 0 '
JZ Qbret; If a positive integer, the function returns; if it is a negative number, the sign bit is unchanged and the remaining bits are reversed by 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 treatment. Method 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 Qbincre
Qbret:
Ret
Questbuma ENDP
;*******************************************
Bsubproc proc near function: 16-bit binary number subtraction simulation.
Algorithm: phase reduction, flag for borrow signs (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; functions such as the name of a function
BX, BX as a parameter, incoming NUM1 or NUM2 memory space first address
MOV dh,2
MOV si,0
Bth1:
MOV al,0, group conversion, analog 4 conversion from binary to 1 to 16 binary numbers
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

<<<<

Learn Proverbs:

1, a smattering is dangerous.

2, if a language doesn't affect your idea of programming, it's not worth learning.

>>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.