I am pretty new to assembly I would like criticism on two "methods" I have written. One converts string to int and the other prints an int to stdout. Any advise helps out.
bits 64
section .data
endl db 10, 0
endlLength equ $ - endl
section .bss
string resb 100
section .text
global _start
global _strToInt
global _printInt
_start:
mov rbp, rsp
;get int from user
mov rax, 0
mov rdi, 0
lea rsi, [string]
mov rdx, 100
syscall
;make string to int
push string
call _strToInt
pop r8
push rax
call _printInt
pop rax
mov rax, 1
mov rdi, 1
mov rsi, endl
mov rdx, endlLength
syscall
mov rax, 60
mov rbx, 0
syscall
;last value pushed to stack must be address to string. stores int in rax
_strToInt:
;prolog
push rbp
mov rbp, rsp
;save registers
push rbx
;actual code
xor rax, rax
mov rbx, 10 ;will be used to multiply by 10 later
mov rcx, [rbp + 16]
cmp byte[rcx], '-' ;check to see if value is negetive
jne _convertStringLoop
inc rcx ;increment the address
_convertStringLoop:
xor rdx, rdx ;clear rdx
mul rbx ;multiply rax by 10
mov dl, [rcx] ;get 1 byte from rcx address
sub dl, '0' ;seb by '0' to get actual value
add rax, rdx ;add it to rax
inc rcx
cmp byte[rcx], 10 ;see if new line char. exit if new line char
je _exitConvertStringLoop
cmp byte[rcx], 0 ;see if end of line char. exit if end of line
jne _convertStringLoop
_exitConvertStringLoop:
mov rcx, [rbp + 16]
cmp byte[rcx], '-' ;if not negetive jump
jne _exitStrToInt
not rax ;1's complement (make negetive)
inc rax ;2's complement (make negetive)
_exitStrToInt:
;restore registers
pop rbx
;epilog
pop rbp
ret
;last value pushed to stack will be printed
_printInt:
;prolog
push rbp
mov rbp, rsp
;save registers
push rbx
;actual code
mov rsi, rsp
mov rax, [rbp + 16] ;get the value that user wants to print
mov rbx, 10 ;will be used to divide by 10 later
xor rcx, rcx
cqo
cmp rdx, -1 ;check to see if negetive
jne _divisionLoop ;if not negetive jump
;print negetive sign
mov [rsi + 1], byte '-'
mov rax, 1
mov rdi, 1
inc rsi
mov rdx, 1
syscall
dec rsi
;convert to positive number
mov rax, [rbp + 16]
;imul rax, -1
dec rax
not rax
xor rcx, rcx
_divisionLoop:
xor rdx, rdx
div rbx ;divides number by 10 to move over last digit into rdx reg
add dl, '0'
dec rsi
mov [rsi], dl
inc rcx ; count for how many digits added to stack
cmp rax, 0
jnz _divisionLoop ;jump if the division did not result in a zero
;print all the values
mov rax, 1
mov rdi, 1
mov rdx, rcx
syscall
;restore register
pop rbx
;epilog
pop rbp
ret
One more thing I wanted to ask was is it better to do
mov rax, -20
mov rbx, -1
imul rbx
or
mov rax, -20
dec rax
not rax
when I know a number is negetive and I want to turn it to a positive.