Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MSG1 DB "x = ","$" ; string to display for (1)
- MSG2 DB "y = ","$" ; string to display for (1)
- MSG3 DB 0DH,0AH,"x+y = ","$" ; string for sum format
- MSG4 DB 0DH,0AH,"x-y = ","$" ; string for sub format
- MSG5 DB 0DH,0AH,"Error","$" ; error display message
- START: ; initialize index
- MOV DI,00H ; keep position for XLAT
- MOV BX,200H
- MOV SI,04H
- READ_HEX_NUMBER:
- MOV DH,02H ; counter for pair of 2HEX remaining
- READ_FIRST:
- MOV AH,08H
- INT 21H
- CMP AL,0DH ; if ENTER is pressed
- JE ERROR ; then goto ERROR
- CMP AL,30H ; if digit is valid then read again
- JL READ_FIRST
- CMP AL,39H ; if digit is valid the goto STORE_NUM
- JLE STORE_NUM1
- CMP AL,41H
- JL READ_FIRST
- CMP AL,46H
- JLE STORE_NUM1
- CMP AL,61H
- JL READ_FIRST
- CMP AL,66H
- JG READ_FIRST
- STORE_NUM1: ; stores digit in array
- CMP AL,60H
- JLE SKIP_LINE
- SUB AL,20H
- SKIP_LINE:
- MOV [BX+DI],AL ; increase counter of array position
- INC DI
- READ_SECOND:
- MOV AH,08H ; same procedure as for first digit
- INT 21H
- CMP AL,0DH ; if ENTER is pressed
- JE ERROR ; then goto ERROR
- CMP AL,30H
- JL READ_SECOND
- CMP AL,39H
- JLE CONTINUE
- CMP AL,41H
- JL READ_SECOND
- CMP AL,46H
- JLE CONTINUE
- CMP AL,61H
- JL READ_SECOND
- CMP AL,66H
- JG READ_SECOND
- CONTINUE: ; store second digit in array
- CMP AL,60H
- JLE SKIP_LINE2
- SUB AL,20H
- SKIP_LINE2:
- MOV [BX+DI],AL ; increase counter of array position
- INC DI
- DEC DH ; decrease remaining numbers left to read
- CMP DH,00H ; if 2 numbers got read then continue
- JNZ READ_FIRST ; else read 2nd number
- MOV DX, offset MSG1 ; display string MSG1
- MOV AH,09H
- INT 21H
- MOV AL,00H ; display 1st digit of 1st number
- XLAT
- MOV DL,AL
- MOV AH,02H
- INT 21H
- MOV AL,01H ; display 2nd digit of 1st number
- XLAT
- MOV DL,AL
- MOV AH,02H
- INT 21H
- MOV DL,20H ; print "space" character
- MOV AH,02H
- INT 21H
- MOV DX, offset MSG2 ; display string MSG2
- MOV AH,09H
- INT 21H
- MOV AL,02H ; display 1st digit of 2nd number
- XLAT
- MOV DL,AL
- MOV AH,02H
- INT 21H
- MOV AL,03H ; display 2nd digit of 2nd number
- XLAT
- MOV DL,AL
- MOV AH,02H
- INT 21H
- MOV AH,08H
- INT 21H
- CMP AL,0DH ; if ENTER is pressed after 2 numbers
- JE PRINT_SUM_SUB ; then print sum and difference of numbers
- ; else if there are more than 2 numbers
- WAIT_FOR_ENTER: ; then wait for ENTER to display error message
- MOV AH,08H
- INT 21H
- CMP AL,0DH
- JE ERROR
- JMP WAIT_FOR_ENTER
- ERROR:
- MOV DX, offset MSG5 ; display "Error" message on screen
- MOV AH,09H
- INT 21H
- JMP FINISH
- PRINT_SUM_SUB:
- MOV AL,00H
- XLAT
- CALL HEXPROD ; call hexprod to keep values for hex numbers
- MOV CL,04H ; define shift value for ROL
- MOV DL,CH
- ROL DL,CL ; take first ASCII value and convert it
- MOV AL,01H
- XLAT
- CALL HEXPROD
- MOV CL,04H
- ADD DL,CH ; take second ASCII code to complete first number
- MOV AL,02H
- XLAT
- CALL HEXPROD
- MOV CL,04H
- ADD DH,CH
- ROL DH,CL
- MOV AL,03H ; start translating second hex number
- XLAT
- CALL HEXPROD
- MOV CL,04H
- ADD DH,CH
- MOV BL,DH
- MOV BH,00H ;first number saved in BX
- MOV DH,00H ;second number saved in DX
- MOV CX,BX
- ADD BX,DX
- MOV AX,BX
- CALL HEX_TO_DECIMAL
- AND DX,00FFH
- SUB DX,CX
- MOV CX,DX
- MOV DX, offset MSG3 ; display MSG3 string
- MOV AH,09H
- INT 21H
- MOV BX,300H ; take from array output values
- MOV AL,00H
- XLAT
- CMP AL,00H ; if left most digit is zero then dont display it
- JE SKIP1 ; and move to 2nd digit
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- JMP NEXT_NUM2
- SKIP1: ; if both 1st digit and 2nd digit is zero
- MOV AL,01H ; then dont display it and move to 3rd digit
- XLAT
- CMP AL,00H
- JE SKIP7
- NEXT_NUM2:
- MOV AL,01H
- XLAT
- CMP AL,00H
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- SKIP7:
- MOV AL,02H
- XLAT
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- MOV DX, offset MSG4 ; display MSG4 string
- MOV AH,09H
- INT 21H
- CMP CX,00H
- JGE CONT3 ; if result < 0
- NEG CX ; convert to positive
- MOV DL,2DH ; output minus sign
- MOV AH,02H
- INT 21H
- CONT3:
- MOV AX,CX
- CALL HEX_TO_DECIMAL ; same logic as before to print the sub
- MOV BX,300H
- MOV AL,00H
- XLAT
- CMP AL,00H
- JE SKIP5
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- JMP NEXT_NUM1
- SKIP5:
- MOV AL,01H
- XLAT
- CMP AL,00H
- JE SKIP6
- NEXT_NUM1:
- MOV AL,01H
- XLAT
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- SKIP6:
- MOV AL,02H
- XLAT
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- FINISH:
- MOV AH,02H
- MOV DL,0AH ; print "\n" character
- INT 21H
- MOV AH,02H
- MOV DX,0DH ; print carriage return character
- INT 21H
- MOV AH,02H
- MOV DL,0AH ; print "\n" character
- INT 21H
- MOV AH,02H
- MOV DX,0DH ; print carriage return character
- INT 21H
- JMP START
- HEX_TO_DECIMAL: ; convert HEX number to decimal
- PUSH BX
- MOV SI,00H
- MOV DH,64H
- MOV BX,300H ; store each decimal digit in array
- DIV DH ; divide by 100 to get the right most digit as remainder
- MOV [BX+SI],AL
- AND AX,0FF00H
- XCHG AH,AL
- MOV DH,0AH ; divide by 10 the rest of the previous division
- DIV DH ; to get the left most digit as quotient
- INC SI ; and the middle digit as remainder
- MOV [BX+SI],AL
- INC SI
- MOV [BX+SI],AH
- POP BX
- RET
- HEXPROD: ; convert ASCII to HEX
- MOV CL,AL ; CL contains the value of ASCII
- MOV CH,00H ; CH contains the value of HEX -> result
- MOV AL,30H
- HEXSTART:
- CMP AL,CL
- JE HEXFOUND
- INC CH
- INC AL
- JNC SKIP0
- CMC
- SKIP0:
- DAA
- CMP AL,40H
- JNE AVOID
- INC AL
- JNC SKIP3
- CMC
- SKIP3:
- DAA
- AVOID:
- JMP HEXSTART
- HEXFOUND:
- RET
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement