check if a number is negative or positive
section .bss
num resb 10
section .data
positive_msg db "The number is Positive", 10, 0
negative_msg db "The number is Negative", 10, 0
zero_msg db "The number is Zero", 10, 0
prompt db "Enter a number: ", 0
section .text
global _start
_start:
lea rsi, [prompt]
call print_string
lea rsi, [num]
mov rdx, 10
call read_number
lea rsi, [num]
call ascii_to_integer
cmp rax, 0
je .zero
jl .negative
.positive:
lea rsi, [positive_msg]
call print_string
jmp .exit
.negative:
lea rsi, [negative_msg]
call print_string
jmp .exit
.zero:
lea rsi, [zero_msg]
call print_string
.exit:
mov rax, 60
xor rdi, rdi
syscall
print_string:
push rsi
push rdx
xor rcx, rcx
.count_length:
cmp byte [rsi + rcx], 0
je .found_length
inc rcx
jmp .count_length
.found_length:
mov rax, 1
mov rdi, 1
mov rdx, rcx
syscall
pop rdx
pop rsi
ret
read_number:
mov rax, 0
mov rdi, 0
mov rdx, 10
syscall
ret
ascii_to_integer:
xor rax, rax
xor rcx, rcx
xor rbx, rbx
.check_sign:
movzx rdx, byte [rsi]
cmp rdx, '-'
jne .convert_loop
inc rsi
mov rbx, 1
.convert_loop:
movzx rdx, byte [rsi + rcx]
cmp rdx, 10
je .done
sub rdx, '0'
imul rax, rax, 10
add rax, rdx
inc rcx
jmp .convert_loop
.done:
cmp rbx, 1
jne .end
neg rax
.end:
ret