I am new to assembly i don't know how to convert 64 bit function to 32 bit to 16 bit to 8 bit
Purpose of below functions is to print number and return number of digits in it.
64 bit:
global print_uint64
section .text
print_uint64:
mov rax,rdi
mov rdi,10
mov rsi,rsp
while:
xor rdx ,rdx
div rdi
add rdx ,48
dec rsi
mov [rsi],dl
cmp rax ,0
jne while
mov rax,1
mov rdi,1
lea rdx,[rsp]
sub rdx,rsi
syscall
lea rax,[rsp]
sub rax,rsi
ret
this works fine
32 bit:
global print_uint32
section .text
print_uint32:
mov eax,edi
mov edi,10
mov rsi,rsp
while:
xor edx ,edx
div edi
add edx ,48
dec rsi
mov [rsi],dl
cmp eax ,0
jne while
mov eax,1
mov edi,1
lea edx,[rsp]
sub edx,esi
syscall
lea eax,[rsp]
sub eax,esi
ret
this works fine
16 bit:
global print_uint16
section .text
print_uint16:
mov ax,di
mov di,10
mov rsi,rsp
while:
xor dx ,dx
div di
add dx ,48
dec rsi
mov [rsi],dl
cmp ax ,0
jne while
mov ax,1
mov di,1
lea dx,[rsp]
sub dx,si
syscall
lea ax,[rsp]
sub ax,si
ret
but this did not work
I studied some questions on stack overflow regarding to this question, what i understand is i can't change rsp to esp because esp set upper 32 bits to zero so when we use [] on that access the memory not allocated to that program so it throws segment fault.
My question is:
1)What is the basic rules to convert 64 bit to 32 bit to 16 bit to 8 bit.