Article Index

;name: nibbletoascii.asm
;
;description: branch free conversion of a NIBBLE in DIL to ASCII in AL
;             with use of single register AX.
;
;build: nasm "-felf64" nibbletoascii.asm -o nibbletoascii.o
;	ld -s -melf_x86_64 nibbletoascii.o -o nibbletoascii  
;
;the algorithm: see nibbletoascii.txt file

bits 64

%include "nibbletoascii.inc"

global _start

section .bss
    
section .data
    hexnibble:	db  0xC
    output:	db  "0x0"
    buffer:	db  0
    crlf:	db  10
    length:	equ $-output
    
section .text

_start:
    mov	    dil,byte[hexnibble]
    call    nibbletoascii
    mov	    byte[buffer],al
    syscall write,stdout,output,length
    syscall exit,0
    
; in : DIL has the nibble to convert
; out : AL has the converted nibble
nibbletoascii:
    mov	    al,dil
    and	    al,0x0F		;only lower 4 bits counts
    shl	    ax,4		;shift to most significant positions
    add	    ax,0x0060		;add 0110 to nibble
    shr	    al,4		;move back to least significant positions
    sub	    al,6		;subtract 6 from nibble
    and	    al,0x0F		;mask bits 7 to 4
    sub	    al,ah		;subtract AH from AL
    shl	    ah,3		;multiplicate AH by 8
    sub	    al,ah		;subtract AH from AL
    add	    ah,0x18		;add 2 to bits in AH
    shl	    ah,1		;multiply AH by two
    or	    al,ah		;make AL ASCII
    xor	    ah,ah		;zero out bits from AH
    ret