I'm currently getting started with NASM and wanted to know, how to output the contents of a register with NASM in Hexadecimal. I can output the content of eax with
section .bss
reg_buf: resb 4
.
.
.
print_register:
mov [reg_buf], eax
mov eax, SYS_WRITE
mov ebx, SYS_OUT
mov ecx, reg_buf
mov edx, 4
int 80h
ret
Let's say eax contains 0x44444444 then the output would be "DDDD". Apparently each pair of "44" is interpreted as 'D'. My ASCII table approves this.
But how do I get my program to outpu开发者_Python百科t the actual register content (0x44444444)?
This is how i was taught to do it..
.
.
SECTION .data
numbers: db "0123456789ABCDEF" ;; have initialized string of all the digits in base 16
.
.
.
;;binary to hex
mov al , byte [someBuffer+someOffset] ;; some buffer( or whatever ) with your data in
mov ebx, eax ;; dealing with nybbles so make copy for obtaining second nybble
and al,0Fh ;; mask out bits for first nybble
mov al, byte [numbers+eax] ;; offset into numbers is the hex equiv in numbers string
mov byte [someAddress+someOffset+2], al
;;store al into a buffer or string or whatever it's the first hex number
shr bl, 4 ;; get next nybble
mov bl, byte [numbers+ebx] ;; get the hex equiv from numbers string
mov byte [someAddress+someOffset+1], bl
;;place into position next to where al was stored, this completes the process,
;;you now have your hexadecimal equivalent output it or whatever you need with it
.
.
.
You need to format your register as a text string first. The simplest to use API would likely be itoa, followed by your write call. You will need a string buffer allocated for this to work.
If you don't want to do it in assembly, you can make a quick C/Python/Perl/etc program to read from your program and make all output text.
加载中,请稍侯......
精彩评论