I have to Write a nasm (or other) assembler program for an x86 processor that prints a 32-bit hexadecimal number on the standard output, such as printf("%x\n",123456), and use the write system call to display. I wrote some code but it seems to not work. Can comeone help me?
section .data
message db '0x',0
number dq 123456
section .text
global _start
_start:
; Print "0x"
mov eax, 4
mov ebx, 1
mov ecx, message
mov edx, 2
int 0x80
; Print number in hex
mov eax, number
push eax
call print_hex
add esp, 4
; Exit
mov eax, 1
xor ebx, ebx
int 0x80
; Function to print number in hex
print_hex:
push ebx
push edx
mov ebx, 16
mov edx, 0
mov ecx, 0
print_hex_loop:
xor edx, edx
div ebx
add edx, '0'
cmp edx, '9'
jg print_hex_digit
push edx
inc ecx
test eax, eax
jne print_hex_loop
print_hex_done:
mov eax, 4
mov ebx, 1
mov edx, ecx
int 0x80
print_hex_pop_loop:
pop edx
mov [esp], edx
inc esp
dec ecx
jne print_hex_pop_loop
pop edx
pop ebx
ret
print_hex_digit:
add edx, 'A' - '9' - 1
jmp print_hex_done
I am new to assembler so I don't have many ideas how to get this to work properly