;FILENAME: UNIT12.TXT
;
; ASSEMBLY LANGUAGE COURSE, UNIT 12
; By Don Stoner Revision: 2008.07.02
;
ORG 100h ; Code Starts at 100h
;
; This is a program for a 4-function
; hexidecimal calculator. It uses an
; %INCLUDE statement at the bottom so
; all of the files from UNIT11.ASM are
; accessable by this program. This
; calculator will keep its "sum" in
; the 16-bit register named CX.
;
; This label is a special loopback
; point to allow us to clear out the
; CX register to zero with the "space"
; key. We set CX to zero here so that
; our calculator will always display
; zero when we first begin running it.
;
RESET: ; Clear the calculator's
; "sum" register to zero:
;
MOV CX,0 ;Start with 0 in CX
;
; This is the normal loopback point if
; we don't press the "space" key:
;
LOOP_BACK_HERE:
;
; First we display the 4-digit
; hexadecimal "sum"
;
MOV AL,CH ;Display "sum"
CALL HEX_OUT ;from reg: CX
MOV AL,CL
CALL HEX_OUT
; ; And output a
; ;space to separate
; ; it from the
CALL SPACE_OUT ; next entry.
;
; Next, we call HEX_IN to get
; another number (into the DX
; register). When we exit the
; HEX_IN sunroutine, AL will
; contain the ASCII code for
; the final key which was
; typed (the non-ASCII key
; which caused us to exit the
; routine). The terminating
; key (+ - * / space or Q)
; will tell us which function
; to perform with whatever
; hex number was entered.
;
; You may remember that our
; HEX_IN subroutine ORs the
; 20h bit of the number in AL
; so that both uppercase and
; lowercase letters will work
; the same. This also means
; that the "Carriage Return"
; key will be modified; our
; calculator with think it is
; a "-" key.
;
CALL HEX_IN ;Get the number
CALL SPACE_OUT ;space again
;
; Check terminating character
; (in AL) for next function
;
; Is it a "+" (for addition)?
;
CMP AL,"+" ;Add?
JNE NOT_PLUS ;skip if not
ADD CX,DX ;Add DX to CX
JMP LOOP_BACK_HERE ;continue
NOT_PLUS: ;(skip to here if not "+")
;
; Is it a "-" (subtraction)?
;
CMP AL,"-" ;Subtract?
JNE NOT_SUB ;skip if not
SUB CX,DX ;Subtract DX from CX
JMP LOOP_BACK_HERE ;continue
NOT_SUB: ;(skip to here if not "-")
;
; Is it a "*" (for multiply)?
;
CMP AL,"*" ;Multiply?
JNE NOT_MULT ;skip if not
; Multiply only operates on AX
MOV AX,CX ;Get sum to AX
MUL DX ;Times DX
MOV CX,AX ;AX Back to CX
JMP LOOP_BACK_HERE ;continue
NOT_MULT: ;(skip to here if not "*")
;
; Is it a "/" (for multiply)?
;
CMP AL,"/" ;Divide?
JNE NOT_DIV ;skip if not
; Divide only works on AX, but
; DX also needs to be zeroed
MOV AX,CX ;Get sum to AX
MOV CX,DX ;and DX to CX
XOR DX,DX ;zero out DX
DIV CX ;Divide by "CX"
MOV CX,AX ;AX back to CX
JMP LOOP_BACK_HERE ;continue
NOT_DIV: ;(skip to here if not "/")
;
; Is it a space (for clear)?
;
CMP AL," " ;Space means
JE RESET ;Reset to 0
;
; Else, if not + - * / or space
;
INT 20h ;return to DOS
;--------------------------------------
%INCLUDE "UNIT11.ASM"