Loading
  1. ; helper routine: a formatter. converts a 64bit value to its ascii/hex digits representation
  2. ; has 5 entry points for call: hex64, hex64_, hex64__, hex32_, hex16
  3. ; register usage:
  4. ; rax: input, 64(32,16)bit  value to convert, destroyed
  5. ; rdi: input, destination buffer, exactly 16(17,19,9,4) characters, on return it points after
  6. ; rbx, rcx: scratch
  7. ; hex value formats with _ delimiters are compatible with assembler syntax, just add etither a '0x' prefix or 'h','H' postfix
  8. ; hex value formats with _ delimiters are compatible with assembler syntax, just add etither a '0x' prefix or 'h','H' postfix
  9.  
  10.         section .text
  11.  
  12. hex64:
  13.         mov     rcx,16
  14. .init0:
  15.         cld
  16.         mov     rbx,rax
  17.         xor     rax,rax
  18. .loop:
  19.         rol     rbx,4
  20.         mov     al,bl
  21.         and     al,0x0f
  22.         lea     rsi,[hex64.hex_digits+rax]
  23.         movsb
  24.         loop    hex64.loop
  25.         ret
  26.  
  27. hex64_: ; same as above, but ads a middle _ for better readability. and so uses 17 characters buffer
  28.         mov     rcx,8
  29.         call    hex64.init0
  30.         mov     al,'_'
  31.         stosb
  32.         mov     rcx,8
  33.         jmp     hex64.loop
  34.  
  35. hex64__: ; again same as above, but ads two more _ for readability, 19 characters buffer
  36.         mov     rcx,4
  37.         call    hex64.init0
  38.         mov     al,'_'
  39.         stosb
  40.         mov     rcx,4   ; repeating a mov imm is faster than tripple push/pop, and won't waste another register
  41.         call    hex64.loop
  42.         mov     al,'_'
  43.         stosb
  44. hex32_in:
  45.         mov     rcx,4
  46.         call    hex64.loop
  47.         mov     al,'_'
  48.         stosb
  49.         mov     rcx,4
  50.         jmp     hex64.loop
  51. hex32_:
  52.         cld
  53.         mov     rbx,rax
  54.         rol     rbx,32
  55.         xor     rax,rax
  56.         jmp     hex32_in
  57.  
  58. hex16:
  59.         cld
  60.         mov     rcx,4
  61.         mov     rbx,rax
  62.         ror     rbx,16  
  63.         xor     rax,rax
  64.         jmp     hex64.loop
  65.        
  66.         section .rodata
  67.        
  68. hex64.hex_digits:
  69.         db      "0123456789ABCDEF"
  70.        
  71.         section .text
  72.  
  73. ; end hex64.inc