;FILENAME: UNIT6.TXT ; ; ASSEMBLY LANGUAGE COURSE, UNIT 6 ; By Don Stoner Revision: 2008.06.25 ; ; WARNING: Assembly language programs, ; can do absolutely anything! ; Eventually, YOU *WILL* CRASH THE HARD ; DRIVE OF THE COMPUTER YOU ARE USING! ; Make sure that there is ABSOLUTELY ; NOTHING on that computer that you ; can't afford to loose! ; ; To edit, assemblem and test, type: ; ; MAKE UNIT6 ; UNIT6 ; ; Code Starts at 100h ; ORG 100h ; ; This time we'll echo the ; typed keys in hexadecimal ; instead of ASCII. This will ; let us examine all of the ; ASCII keyboard codes. ; ; Tell NASM where to loop back: ; LOOP_BACK_TO_HERE: ; ; First, we read a CALL ASCII_IN ; Key into AL MOV DL,AL ;(save a copy in DL ; ; since we will be ; ; messing with AL). ; ; Next, we output the MSN (Most ; Sugnificant Nibble, left four ; bits) of AL in hexadecimal. ; This requires several steps: ; ; 1) Shift the left four bits ; over to the right with "SHR". ; SHR AL,4 ;(SHift AL Right 4x) ; ; 2) This "AND" instruction ; turns the left four bits ; of our character into zeros. ; AND AL,00001111b ;(binary) ; ;(same as 0Fh) ; ; 3) This "OR" turns the binary ; numbers 0-15 into the ASCII ; codes for: 0123456789:;<=>? ; OR AL,"0" ;ASCII "0" is 30h ; ; 4) If the code is less than ; ":" we're done. The compare ; (CMP) command subtracts its ; operand (":" or 3Ah) from AL ; without changing AL - but it ; does change the status flags. ; JC jumps if the cary flag is ; set (":" greater than AL). ; CMP AL,":" ;IS IT < ":" ? JC HEXOK ;IF SO, OUTPUT 0-9 ; ; Otherwise we must turn :;<=>? ; into ABCDEF by adding 41h-3Ah ; (same as "A"-":" or 6). ; ADD AL,"A"-":" ;Add six to AL ; ; 5) Output the ASCII code ; HEXOK: CALL ASCII_OUT ;output MSN ; ; Finally, Output the LSN ; (Least Sifnificant Nibble) ; by repeating steps 2-5 (no ; shift this time) using the ; original data (from DL). ; MOV AL,DL ;restore AL from DL AND AL,0FH ;=00001111b ;step 2 OR AL,"0" ;step 3 CMP AL,":" ;step 4 JC HEXOK2 ;step 4 ADD AL,"A"-":" ;step 4 HEXOK2: CALL ASCII_OUT ;step 5 ; ; Restore the typed key again ; MOV AL,DL ; ; And either loop back, or exit ; CMP AL,"x" ;Loop if not "x" JNE LOOP_BACK_TO_HERE INT 20h ;Else, back to DOS ; End of Main code ;-------------------------------------- ; Subroutines: ; ; Output AL in ASCII ; ASCII_OUT: MOV AH,0Eh ;select "output" MOV BL,0 ;select page and MOV BH,0 ;graphics color INT 10h ;BIOS routines RET ;return to main code ; ; Input AL in ASCII ; ASCII_IN: MOV AH,0 ;normal keyboard INT 16H ;key into AL RET ;return to main code