writing some simple assembly code, the program segfaults at the second call of subroutine _printint. This only happens if i remove push rdx and pop rdx from either the _printint subroutine or the PrintNewline macro.
section .data
digit db 0
newline db 10
section .text
global _start
%macro PrintNewline 0
push rdx ;this push
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
pop rdx ;this pop
%endmacro
%macro exit 0
mov rax, 60
mov rdi, 0
syscall
%endmacro
_printdigit:
add al, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
mov rdx, 1
syscall
ret
_printint:
push rdx ;this push
mov r10, 0
mov rcx, 10
_printint0:
div rcx
push rdx
inc r10
cmp rax, 0
jne _printint0
_printint1:
pop rax
call _printdigit
dec r10
cmp r10, 0
jne _printint1
pop rdx ;this pop
ret
_start:
mov rax, 10
call _printint
PrintNewline
mov rax, 10
call _printint
PrintNewline
exit
I am unable to understand what is the purpose of them.
writesystem call after storing into a buffer; each system call is very slow compared to even a 64-bitdivinstruction.