|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
; Subroutine to input a decimal data from the keyboard and convert it to binary
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
P_GETNEW PROC
PUSH BX; Export parameter: AX=Binary number represented by complement
PUSH CX; Note: Use "-" to guide negative numbers, and the data range is +32767~-32768
PUSH DX
XOR BX, BX; BX saves the result
XOR CX, CX; CX is positive and negative sign, 0 is positive, -1 is negative
MOV AH,1; enter a character
INT 21H
CMP AL,'+'; is "+", continue to input characters
JZ READ1
CMP AL,'-'; Yes "-, set the -1 flag
JNZ READ2
MOV CX,-1
READ1: MOV AH,1; continue to input characters
INT 21H
READ2: CMP AL,'0'; if it is not a character between 0-9, the input data is over
JB READ3
CMP AL,'9'
JA READ3
SUB AL, 30H; is a character between 0-9, then converted to a binary number
; Use the shift instruction to multiply the value by 10: BX←BX*10
SHL BX,1
MOV DX,BX
SHL BX,1
SHL BX,1
ADD BX,DX
MOV AH,0
ADD BX,AX; After the input value is multiplied by 10, it is added to the newly input value
JMP READ1; continue to enter characters
READ3: CMP CX,0; It is a negative number, to complement
JZ READ4
NEG BX
READ4: MOV AX,BX; set export parameters
POP DX
POP CX
POP BX
RET; subroutine return
P_GETNEW ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
; Output subroutine to convert binary data to decimal
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
P_OUTPUT PROC
PUSH AX; Entry parameter: shared variable WTEMP
PUSH BX
PUSH DX
MOV AX, WTEMP; Take out the displayed data
TEST AX, AX; Determine whether the data is zero, positive or negative
JNZ WRITE1
MOV DL,'0'; is '0', exit after displaying '0'
MOV AH,2
INT 21H
JMP WRITE5
WRITE1: JNS WRITE2; is a negative number, display "-"
MOV BX, AX; AX data is temporarily stored in BX
MOV DL,'-'
MOV AH,2
INT 21H
MOV AX,BX
NEG AX; Data complement (absolute value)
WRITE2: MOV BX,10
PUSH BX; 10 is pushed onto the stack as a push exit flag
WRITE3: CMP AX,0; data (quotient) is zero, turn to display
JZ WRITE4
SUB DX,DX; expand the dividend DX.AX
DIV BX; data divided by 10
ADD DL,30H; The remainder (0~9) is converted to ASCⅡ code
PUSH DX; Every bit of data is pushed into the stack first low bit and then high bit
JMP WRITE3
WRITE4: POP DX; Every bit of data pops out of the stack first high and then low
CMP DL,10; is the end mark 10, then exit
JE WRITE5
MOV AH,2; display
INT 21H
JMP WRITE4
WRITE5: POP DX
POP BX
POP AX
MOV DL,20H
MOV AH,02H
INT 21H
RET; subroutine return
P_OUTPUT ENDP |
|