You are on page 1of 4

;DISPLAY NAME AND PROGRAM TITLE ON THE OUTPUT SCREEN ;DISPLAY INSTRUCTIONS FOR THE USER ;PROMPT THE

USER TO ENTER TWO NUMBERS ;CALCULATE THE SUM,DIFFERENCE,QUOTIENT,AND REMAINDER OF THE NUMBERS ;DISPLAY A PARTING MESSAGE .MODEL SMALL .STACK 64 ;------------------------.DATA TEN DW 10 ASCNUM DB 'ABCDEF$' STRLEN DB 2 ORG 10H BINNUM DW 'aa','$' MSG1 DB "MATH 101 By William D'Ariano".0DH,0AH,'$' DB "Enter 2 numbers, and I'll show you the sum, diference, product, quotient, a nd remainder.0DH,0AH'$' MSG2 DB 0DH,0AH,0DH,0AH, 'Enter first number: $' MSG3 DB "Enter second number: $" MSG4 DB 0DH,0AH, CALCULATE '+','-','*','/' BETWEEN FIRST NUMBER & SECOND NUMBER DB DISPLAY EACH OPERATION RESULT MSG5 DB 0DH,0AH, "Fascinated?, Great! Ciao",0DH,0AH,'$' NUM1 DW ? NUM2 DW ? -------------------------.CODE MAIN MOV AX,@DATA MOV DS,AX MOV DX,OFFSET MSG1 ; DX POINTS TO OUTPUT BUFFER MOV AH,9 INT 21H LEA DX,MSG2 MOV AH,09H ; OUTPUT STRING AT DS:DX INT 21H CALL PROMPT2 CALL GET_NUM ;GETS 1ST NUMBER AND PUTS IT IN ASCNUM PUSH ASC2B_CONV ; CONVERT ASCNUM TO BINARY PUT RESULT IN BINNUM PUSH BINNUM ;SAVES BINNUM CALL NEW_LINE CALL PROMPT3 CALL NEW_LINE CALL GET_NUM ;GETS 2ND NUMBER CALL ASC2B_CONV ;CONVERT ASCNUM TO BINARY PUT RESULT IN BINNUM POP AX ;RESTORES OLD NUMBER FROM STACK AND STORES IN AX ADD BINNUM,AX ;ADDS THE TWO NUMBERS IN BINARY, SUM STORED IN BINNUM SUB BINNUM,AX ;SUBTRACTS THE TWO NUMBERS IN BINARY, DIFFERENCE STORED IN BINNUM MUL BINNUM,AX ;MULTIPLYS THE TWO NUMBERS IN BINARY, PRODUCT STORED IN BINNUM DIV BINNUM,AX ;DIVIDES THE TWO NUMBERS IN BINARY, QUOTIENT STORED IN BINNUM CALL B2ASC_CONV ;CONVERT THE CALCULATIONS IN BINNUM TO ASCII AND PUTS RESULTS I N ASCNUM CALL NEW_LINE CALL DISPLAY_RESULT1 DISPLAYS THE RESULT NOTICE ORIGINAL NUMBERED ENTERED IN AS

CNUM IS GONE MOV AH,4CH INT 21H ;GO BACK TO DOS MAIN ENDP ;---------------------------------------;CONVERTS ASCII TO BINARY ASCII_CONV PROC SUB DI,DI ;CLEARS DI FOR BINARY(HEX) RESULT GETS 0 MOV SI,OFFSET ASNUM+2 ;SI=BEGINNING OF ASCII STRING POINTS TO ASCNUM+2 MOV BL,STRLEN ;BL= LENGTH OF ASCII STRING GETS 2 FROM DATA SUB BH,BH ;BH=0 USE BX IN BASED INDEX MODE DEC BX ;BX IS OFFSET TO LAST DIGIT MOV CX,1 ;CX = WEIGHT FACTOR AGAIN: MOV AL,[SI+BX] ;GETS THE ASCII DIGIT AND AL,0FH ;STRIP OFF'3' SUB AH,AH ;CLEAR AH FOR WORD MULTIPLICATOIN MUL CX ;MULTIPLY BY THE WEIGHT FACTOR MUL TEN ;BY TEN MOV CX,AX ;FOR NEXT INTERATION DEC BX ;DECREMENT DIGIT POINTER JNS AGAIN ;JUMP IF COUNTER >= 0 MOV BINNUM,DI ;SAVE THE BINARY (HEX) RESULT RET ASC2B_CONV ENDP ;--------------------------------------B2ASC_CONV PROC MOV BX,10 ;BX=10 THE DIVISOR MOV SI,OFFSET ASCNUM ; SI= BEGINNING OF ASCII STRING ADD SI,2 ;ADD LENGTH OF STRING DEC SI ;SI POINTS TO LAST ASCII DIGIT MOV AX,BINNUM ;LOAD BINARY (HEX) NUMBER BACK: SUB DX,DX ;DX MUST BE IN 0 IN WORD DIVISION DIV BX ;DIVIDE HEX NUMBER BY 10 (BX=10) OR DL,30H ;TAG '3' TO MAKE IT ASCII.REMAINDER IS IN DL MOV [SI],DL ;MOVE THE ASCII DIGIT DEC SI ;DECREMENT POINTER CMP AX,0 ;CONTINUE LOOPING WHILE AX > 0 JA BACK RET B2ASC_CONV ENDP ;-------------------------------------; CALCULATIONS CMP OPR,'+' ; ADDS THE 2 NUMBERS JE DO_PLUS CMP OPR,'-' JE DO_MINUS ;SUBTRACTS THE 2 NUMBERS CMP OPR,'*' ;MULTIPLYS THE 2 NUMBERS JE DO_MULT CMP OPR,'/' ;DIVIDES THE 2 NUMBERS JE DO_DIV

LEA DX,MSG5 MOV AH,09H INT 21 ;GOES BACK TO DOS DO_PLUS MOV AX,NUM1 ADD AX,NUM2 CALL PRINT_NUM ; PRINTS AX VALUE JMP EXIT DO_MINUS MOV AX,NUM1 SUB AX,NUM2 CALL PRINT_NUM ;PRINT AX VALUE JMP EXIT DO_MULT MOV AX,NUM1 IMUL NUM2 ;(DX AX) =AX *NUM2 CALL PRINT_NUM ;PRINT AX VALUE JMP EXIT DO_DIV MOV DX,0 MOV AX,NUM1 IDIV NUM2 ;AX = (DX AX) / NUM2 CMP DX,0 JNZ APPROX CALL PRINT_NUM ;PRINTS AX VALUE LEA DX, SMTH MOV AH,09H ;OUTPUT STRING AT DS:DX INT 21H JMP EXIT ;---------------------------------------PROMPT1 PROC MOV AH,09 ;DISPLAY FUNCTION MOV DX,OFFSET MSG1 ; DX POINTS TO THE OUTPUT BUFFER INT 21H RET PROMPT1 ENDP ;---------------------------------------PROMPT2 PROC MOV AH,09 ;DISPLAY FUNCTION MOV DX,OFFSET MSG2 ;DX POINTS TO OUTPUT BUFFER INT 21H RET PROMPT2 ENDP ;---------------------------------------PROMPT3 PROC MOV AH,09 ;DISPLAY FUNCTION MOV DX,OFFSET MSG3 ;DX POINTS TO OUTPUT BUFFER INT 21H RET PROMPT3 ENDP ;---------------------------------------PROMPT4 PROC MOV AH,09 ;DISPLAY FUNCTION MOV DX,OFFSET MSG4 ;DX POINTS TO OUTPUT BUFFER

INT 21H RET PROMPT4 ENDP ;---------------------------------------GET_NUM PROC MOV AH,0AH ;READS KEYBOARD FUNCTION MOV DX,OFFSET ASCNUM ; DX POINTS TO INPUT BUFFER, READING KEY INTO ASCNUM INT 21H RET GET_NUM ENDP ;---------------------------------------DISPLAY_RESULT1 PROC MOV AH,09 ;DISPLAY FUNCTION MOV DX,OFFSET ASCNUM ; DX POINTS TO OUTPUT BUFFER MOV ASCNUM+2, '$' ;PUT AN END TO STRING USING $ ;---------------------------------------PROMPT5 PROC MOV AH,09 ;DISPLAY FUNCTION MOV DX,OFFSET MSG3 ;DX POINTS TO OUTPUT BUFFER INT 21H RET PROMPT5 ENDP ;---------------------------------------NEW_LINE PROC MOV AH,2 ;MOVES THE CURSOR DOWN ONE LINE MOV DL,0DH INT 21H MOV DL,0AH INT 21H RET NEW_LINE END P END MAIN