; helper routine: a formatter. converts a 64bit value to its ascii/hex digits representation
; has 5 entry points for call: hex64, hex64_, hex64__, hex32_, hex16
; register usage:
; rax: input, 64(32,16)bit value to convert, destroyed
; rdi: input, destination buffer, exactly 16(17,19,9,4) characters, on return it points after
; rbx, rcx: scratch
; hex value formats with _ delimiters are compatible with assembler syntax, just add etither a '0x' prefix or 'h','H' postfix
; hex value formats with _ delimiters are compatible with assembler syntax, just add etither a '0x' prefix or 'h','H' postfix
section .text
hex64:
mov rcx,16
.init0:
cld
mov rbx,rax
xor rax,rax
.loop:
rol rbx,4
mov al,bl
and al,0x0f
lea rsi,[hex64.hex_digits+rax]
movsb
loop hex64.loop
ret
hex64_: ; same as above, but ads a middle _ for better readability. and so uses 17 characters buffer
mov rcx,8
call hex64.init0
mov al,'_'
stosb
mov rcx,8
jmp hex64.loop
hex64__: ; again same as above, but ads two more _ for readability, 19 characters buffer
mov rcx,4
call hex64.init0
mov al,'_'
stosb
mov rcx,4 ; repeating a mov imm is faster than tripple push/pop, and won't waste another register
call hex64.loop
mov al,'_'
stosb
hex32_in:
mov rcx,4
call hex64.loop
mov al,'_'
stosb
mov rcx,4
jmp hex64.loop
hex32_:
cld
mov rbx,rax
rol rbx,32
xor rax,rax
jmp hex32_in
hex16:
cld
mov rcx,4
mov rbx,rax
ror rbx,16
xor rax,rax
jmp hex64.loop
section .rodata
hex64.hex_digits:
db "0123456789ABCDEF"
section .text
; end hex64.inc