flat assembler
Message board for the users of flat assembler.

Index > DOS > Tutorial: Convert byte to hex

Author
Thread Post new topic Reply to topic
Ciper



Joined: 01 Jan 2013
Posts: 27
Ciper 05 Jan 2013, 14:10
Code:
org 100h

macro print_digit
{
mov bx,digits
add bx,ax
mov ah,2
mov dl,[bx]
int 21h
}

macro print_nibble
{
xor ch,ch
shl cx,4
xor ax,ax
or al,ch
print_digit
}

mov cx,0efh     ; Byte to convert
rept 2 { print_nibble }
int 20h

digits db '0123456789ABCDEF'
    


With macro expansions:
Code:
org 100h

;macro print_digit
;{
; mov bx,digits
; add bx,ax
; mov ah,2
; mov dl,[bx]
; int 21h
;}

;macro print_nibble
;{
; xor ch,ch
; shl cx,4
; xor ax,ax
; or al,ch
; print_digit
;}

mov cx,0efh
;rept 2{print_nibble}
;print_nibble
xor ch,ch
shl cx,4
xor ax,ax
or al,ch
;print_digit
mov bx,digits
add bx,ax
mov ah,2
mov dl,[bx]
int 21h
;print_nibble
xor ch,ch
shl cx,4
xor ax,ax
or al,ch
;print_digit
mov bx,digits
add bx,ax
mov ah,2
mov dl,[bx]
int 21h
int 20h

digits db '0123456789ABCDEF'

    


This code has been revised as follows: (thanks to AsmGuru62)

Code:
org 100h
macro print_nibble {
xor ch,ch
shl cx,4
mov al,ch
cmp al,10
sbb al,69h
das
int 10h
}
mov cx,0efh     ; Byte to convert
mov ah,0eh
rept 2 {print_nibble}
int 20h
    

_________________
Ciper


Last edited by Ciper on 05 Jan 2013, 16:17; edited 1 time in total
Post 05 Jan 2013, 14:10
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1619
Location: Toronto, Canada
AsmGuru62 05 Jan 2013, 15:32
This one made my head spin (from ASM JEMS page)!
Code:
;
; input:
;   al = single hex digit
;
; output:
;   al = single ASCII digit
;
; destroys:
;   flags
;

        cmp     al,10           ; if x < 10, set CF = 1
        sbb     al,69h          ; 0-9: 96h .. 9Fh,  A-F: A1h..A6h
        das                     ; 0-9: subtr. 66h -> 30h-39h,
                                ; A-F: subtr. 60h -> 41h..46h
    
Post 05 Jan 2013, 15:32
View user's profile Send private message Send e-mail Reply with quote
Ciper



Joined: 01 Jan 2013
Posts: 27
Ciper 05 Jan 2013, 16:06
Wow!

And it works: (of course Smile)
Code:
org 100h
macro print_nibble {
xor ch,ch
shl cx,4
mov al,ch
cmp al,10
sbb al,69h
das
int 10h
}
mov cx,0efh     ; Byte to convert
mov ah,0eh
rept 2 {print_nibble}
int 20h
    

_________________
Ciper
Post 05 Jan 2013, 16:06
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.