; print.asm
;
; this routine contains generic "printing" routines
;
; written on Sun  06-16-1996  by Ed Beroset
;
        .386
        .MODEL small

        public AsciiByte
        public PrintAX
        public PrintLetter
        public PrintString

        .CODE

LOWAL2ASCII macro
        and     al,0fh                  ; std macro turns low nybble
        add     al,90h                  ; of al into ASCII
        daa                             ; representation (0-9,A-F)
        adc     al,40h                  ; of corresponding hex digit.
        daa                             ; NOTE: destroys AL
ENDM

; AsciiByte
;
; convert the value in AL to two hex ASCII digits in AX.
;
; Entry:
;       al = byte to convert to ASCII hex
;
; Exit:
;       ah,al = high and low ASCII hex digits
;
; Trashed:
;       none but flags
;
; Example:
;       AL = 3Ah on entry, AX = 4133h on exit.
;
;
AsciiByte proc
        mov     ah,al
        LOWAL2ASCII
        xchg    al,ah
        shr     al,4
        LOWAL2ASCII
        ret
AsciiByte endp

PrintAX proc                            ;

        push    bx                      ; save used regs
        push    dx                      ;
        mov     bl,'$'                  ; stick DOS TTY terminator
        push    bx                      ; ... on the stack
        mov     bx,ax                   ; save old AX
        mov     ah,al                   ; calculate low nybbles first
        LOWAL2ASCII                        ; standard macro
        xchg    al,ah                   ; recall high nybble
        shr     al,4                    ; put it into place
        LOWAL2ASCII                        ; convert to ASCII
        push    ax                      ; save on stack
        mov     al,bh                   ; do high nybbles now
        mov     ah,al                   ; low half first
        LOWAL2ASCII                        ; do it
        xchg    al,ah                   ; now recall high nybble
        shr     al,4                    ; put into place
        LOWAL2ASCII                        ; calculate 'em
        push    ax                      ; save to stack
        mov     ah,9                    ; DOS TTY print function
        mov     dx,sp                   ; use stack as pointer
        int     21h                     ; call DOS function
        add     sp,6                    ; dispose of scrap on stack
        pop     dx                      ; recall used registers
        pop     bx                      ;
        ret                             ; return to caller
PrintAX endp

;
; PrintLetter
;
; prints the ASCII letter passed in via AL to the output device
;
; Entry:
;       al = letter to be printed
;
; Exit:
;       no change
;
; Trashed:
;       flags may be altered
;
PrintLetter proc
        push    ax
        push    bx
        mov     bx,0007h
        mov     ah,0eh
        int     10h
        pop     bx
        pop     ax
        ret
PrintLetter endp

;
; PrintString
;
; prints a passed ASCIIZ string
;
; Entry:
;       es:si ==> ASCIIZ string to be printed
;
; Exit:
;       no changes
;
; Trashed:
;       none (flags preserved)
;
PrintString proc
        pushf
        push ax
        push si
        cld
@@MoreLetters:
        lodsb
        or      al,al
        jz      @@Exit
        call    PrintLetter
        jmp     @@MoreLetters
@@Exit:
        pop  si
        pop  ax
        popf
        ret
PrintString endp

        END
