Not tested, but maybe useful:
bits 64
default rel
section .rodata
hextable:
db "0123456789ABCDEF"
section .text
; From Windows API.
; You'll need to link with the corresponding static library for those (kernel32.lib?).
extern __imp_GetStdHandle
extern __imp_WriteConsoleA
extern __imp_ExitProcess
; Convert ECX to string pointed by RDX.
cvt2hex:
mov r9d,ecx ; save ecx.
mov ecx,28 ; we'll start left shifting 28 bits.
.loop:
mov eax,r9d ; restore value to eax.
shr eax,cl ; isolate the corresponding nibble in LSB.
and eax,0x0f
sub ecx,4 ; next time, left shift 4 bits less.
movzx eax,byte [hextable+rax] ; gets the hex digit...
mov [rdx],al ; ... and store in the buffer.
add rdx,1 ; advance buffer position.
test ecx,ecx
jns .loop ; continue in the loop if ECX isn't negative.
ret
; Print EDX chars from the buffer pointed by RCX.
Print:
push rsi
push rbx
sub rsp,8 ; Allocate space for WriteConsoleA last argument.
mov esi,edx ; save EDX and RCX.
mov rbx,rcx
mov ecx,-11 ; -11 is STD_OUTPUT_HANDLE constant (console api).
call qword [__imp_GetStdHandle]
; Here EAX contains the STD_OUTPUT_HANDLE console handle.
xor r9d,r9d ; num chars writen pointer is NULL.
mov r8d,esi ; num chars to write.
mov rdx,rbx ; pointer to char buffer.
mov qword [rsp],0 ; last argument is NULL.
mov rcx,rax ; console handle.
call qword [__imp_WriteConsoleA]
add rsp,8 ; Deallocate space allocated on stack.
pop rbx
pop rsi
ret
; Display ECX on console, in hexadecimal.
display:
sub rsp,8 ; Allocate buffer of 8 chars.
; Convert ECX to string.
mov r10,rsp
mov rdx,rsp
call cvt2hex
; Print the string.
mov edx,8
mov rcx,r10
call Print
add rsp,8 ; Deallocate the buffer.
ret
global _start
_start:
mov ecx,0xDEADBEEF
call display
; Put the rest of your code here...
xor ecx,ecx
jmp qword [__imp_ExitProcess]
; Never gets here!
On Linux, with MinG64 installed, this should work (not tested):
$ nasm -fwin64 -o test.o test.asm
$ x86_64-w64-mingw32-ld -o test.exe test.o -e_start -L /usr/x86_64-w64-mingw32/lib -l:libkernel32.a
I leave to you to discover how to link this with microsoft linker...