hello coders! Sorry, but I do not know English very well! on CIS forums, alas, there is no solution to my question. I am solving a problem. You need to display the number on the screen. In assembler, the number comes from the si function and looks like this:
void ft_putnbr_fd(int nbr, int fd)
... where fd is a descriptor and nbr is a number that may be negative.
I check the number first. if it is greater than or equal to 0, then I go to the body of recursion (_body). otherwise, I display the symbol '-' on the screen, and I make the number positive.
In the recursion body, I save the whole and the remainder each time on the stack. after that I divide the integer by 10. if after dividing the integer value is greater than 0 then I go to _body and when the integer is 0 I go to the output of _display
and here I have problems. I am using lldb debugger, intel mnemonics, x86_64, ios
during debugging, I go all the way to _display but after displaying the character on the screen, I see the following message:
-> 0x100000dd4 <+26>: retq
0x100000dd5: nop
0x100000dd6: nop
0x100000dd7: nop
after that the program crashes.
my code
%define CALL(n) 0x2000000 | n
%define WRITE 4
global _ft_putnbr_fd
extern _ft_putchar_fd
section .data
pos dq 0
minus db '-'
section .text
_ft_putnbr_fd:
push rbp;
mov rbp, rsp;
mov rax, rdi
mov rdx, 0
cmp rax, 0
jae _body
neg rax
push rax
mov rsp, 45
call _ft_putchar_fd
pop rax
jmp _body
ret
_body:
push rdx
xor rdx, rdx
mov rcx, 10
div rcx
cmp rax, 0
jne _body
jmp _display
ret
_display:
add rdx, '0'
push rdi
mov rdx, 1
mov rdi, rsi
mov rsi, rdx
mov rax, CALL(WRITE)
syscall
pop rdi
;call _ft_putchar_fd
pop rdx
ret