UMBC CMSC 211

UMBC | CSEE


An Application -- Code to Implement PutHex16

When we have a sixteen bit value that we want to output as four hex characters, we can do it is a couple of way. There is one version in book, but since there is a need for outputting 8 bits as hex, we will also look at an alternative.

Book Version

INCLUDE PCMAC.INC
;
.MODEL SMALL
.CODE
;
PUBLIC PutHex16
;
;
push ax ; Work Register
push bx ; Used to save AX
push cx ; Used for shift counter
push dx ; Used by _PutCh
;
mov cl, 12 ; current right shift amount
mov bx, ax ; preserve for later use
;
Hexloop:
mov ax, bx; ; Get next digit into position ...
shr ax, cl ; ... (shifting by zero works!)
and al, 00fh ; ... and clear other bits
cmp al, 10 ; Convert digit to ASCII character
jl DecDigit
add al, 'A' - 10 ; 10 ==> 'A', 11 ==> 'B', etc.
jmp PutCh
DecDigit:
add al, '0'
PutCh:
_PutCh al
jge HexLoop
;
pop dx ; Restore registers
pop cx
pop bx
pop ax
;
ret
PutHex16 ENDP
END

Alternative Version

INCLUDE PCMAC.INC
.MODEL SMALL
.CODE
;
PUBLIC PutHex16a
PUBLIC PutHex8
;
PutHex16a PROC
;
push ax
push cx
mov cl, 8
shr ax, cl ; get the high bits -- could have
; used mov al, ah
call PutHex8
pop cx
pop ax
;
push ax ; This is a save version!
and ax, 00FFh
call PutHex8
pop ax
;
ret
PutHex16a ENDP
;
PutHex8 PROC
;
push ax ; Work Register
push bx ; Output Register
push cx ; Used for shift counter
push dx ; Used by _PutCh
;
;split byte into two nybbles and align
mov ah, al
mov cl, 4
shr ah, cl
and al, 0Fh
;Check for values greater than 9
cmp ah, 9
jbe hiOK
add ah, 7
hiOK:
cmp al, 9
jbe loOK
add al, 7
loOK:
;
;Convert to print characters
add ax, '00' ; convert binary to ASCI binary to ASCII
;
mov bx, ax
_PutCh bh, bl
;
pop dx ; Restore registers
pop cx
pop bx
pop ax
;
ret
PutHex8 ENDP
END


UMBC | CSEE