Loading

Paste #p0pvwljvt

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