;FILENAME: UNIT10.TXT ; ; ASSEMBLY LANGUAGE COURSE, UNIT 10 ; By Don Stoner Revision: 2008.07.01 ; ORG 100h ; Code Starts at 100h ; ; This program will input a hexadecimal ; number into the 16-bit DH and DL ; register pair. (Together, DH & DL are ; called the DX register.) ; LOOP_BACK_HERE: ; ; Use new subroutine (below) ; to enter a 2-byte hex number: ; CALL HEX_IN ;Get the number CALL SPACE_OUT ;Leave a space ; ;Echo both bytes of that number ; MOV AL,DH ;MSB out using CALL HEX_OUT ;Old routine MOV AL,DL ;LSB out using CALL HEX_OUT ;Old routine CALL SPACE_OUT ;Leave a space ; ; Check if DX is zero and ; Loop back until it is ; AND DX,DX ;This sets Zero flag JNZ LOOP_BACK_HERE ;if not =0 ; INT 20h ;Else, Back to DOS ; End of Main code ;-------------------------------------- ; Subroutines: ; ; Hexadecimal input ; HEX_IN: ; Clear out the DX register: ; XOR DX,DX ;This sets DX to 0 ; It is less obvious ; than: MOV DX,0 but it ; does essentially the ; same thing while ; using less of the ; computer's resources. HEX_IN_LOOP: ; ; Input one ASCII key ; CALL ASCII_IN ;Turn input key OR AL,20H ;into lowercase CALL ASCII_OUT ;Echo it ; ;Is it a valid ASCII character? ; CMP AL,"0" ;key < 0? JC HEX_IN_END CMP AL,"9"+1 ;key < 9+1? JC GOOD_HEX CMP AL,"a" ;key < a? JC HEX_IN_END CMP AL,"f"+1 ;key < f+1? JNC HEX_IN_END SUB AL,"a"-10 ;turn a-f->10-15 GOOD_HEX: AND AL,0FH ;4 LSBits only SHL DX,4 ;Shift other bits OR DL,AL ;Combine new digit JMP HEX_IN_LOOP HEX_IN_END: RET ;Return to main program ; ; Output a Space ; ; (PUSH and POP commands "save data on" ; and "restore data from" the "stack." ; SPACE_OUT: PUSH AX ;save AL (and AH) MOV AL," " ;output an " " CALL ASCII_OUT ;ASCII space POP AX ;restore AL (and AH) RET ; ; Output AL in Hexadecimal ; HEX_OUT: PUSH AX ;save AL (and AH) SHR AL,4 ;step 1: MSN -> LSN CALL LSN_OUT ;LSN (MSN) out POP AX ;restore AL (and AH) LSN_OUT: PUSH AX ;save AL (and AH) AND AL,0FH ;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 POP AX ;restore AL (and AH) RET ; ; 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