;FILE UNIT11.TXT REVISION 2008.07.01 ; ; This file contains all of the ; subroutines which we have used ; so far. If we put the line: ; ; %include "UNIT11.ASM" ; ; at the end of our programs, NASM ; will include all of this stuff. ; This file will crash if we try to ; assemble and run it all by itself. ;-------------------------------------- ; ; Output an inline ASCIIZ string ; INLINE_ASCIIZ_OUT: POP SI ;Cue SI to return CALL ASCIIZ_OUT ;output string PUSH SI ;Get new return address RET ;And return to it ; ; Output an ASCIIZ string (from [SI]) ; AZLP: CALL ASCII_OUT ;output 1 byte ASCIIZ_OUT: ;Entry here saves code LODSB ;MOV AL,[SI] - INC SI AND AL,AL ;zero byte? JNZ AZLP ;loop if not RET ;else, return to main code ; ; Output a Space ; 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 a "Carriage Return" and a ; "Line Feed" ; CRLF_OUT: PUSH AX ;save AL (and AH) MOV AL,13 ;output a C.R. CALL ASCII_OUT MOV AL,10 ;output a L.F. CALL ASCII_OUT 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 ;LSN only OR AL,"0" ;Fix 0-9 CMP AL,":" ;Fix A-F JC HEXOK2 ADD AL,"A"-":" HEXOK2: CALL ASCII_OUT ;Output it POP AX ;restore AL (and AH) RET ; ; Output AL in ASCII ; ASCII_OUT: MOV AH,0Eh ;select "output" PUSH BX ;save BL and BH MOV BL,0Ah ;select color MOV BH,0 ;and page INT 10h ;BIOS routines POP BX ;restore BL and BH RET ;return to main code ; ; 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 ; ; Input AL in ASCII ; ASCII_IN: MOV AH,0 ;normal keyboard INT 16H ;key into AL RET ;return to main code