first of all pardon my english if there's any misspelling. I'm learning assembly x86 on windows using NASM
This is a simple program, you type a number and it prints if the number is greater than 0, equal to 0, or lower to 0
It works fine for positives and 0, but it doesn't for negatives (it tells me that the numbers is greater than 0) and I don't know why
This is what I type for compilation:
nasm filename.asm -f win64 gcc filename.obj -o filename
This is the code:
global main
extern printf
extern gets
extern sscanf
extern puts
section .data
msjIngresarNumero db "Ingrese un numero: ",0
numFormat db "%li",0
msjError db "Error, no se ingreso un numero"
msjNumeroMayorQueCero db "El numero ingresado es mayor que 0",10,0
msjNumeroIgualQueCero db "El numero ingresado es igual que 0",10,0
msjNumeroMenorQueCero db "El numero ingresado es menor que 0",10,0
msjDebug db "Paso por aca",10,0
msjIngreso db "El numero ingresado es %li",10,0
section .bss
buffer resb 10
numero resq 1
section .text
main:
;Pido que ingrese un numero (caracter)
mov rcx,msjIngresarNumero
sub rsp,32
call printf
add rsp,32
;Obtengo el numero (caracter) ingresado
mov rcx,buffer
sub rsp,32
call gets
add rsp,32
;Lo convierto en numero y almaceno en "numero"
mov rcx,buffer
mov rdx,numFormat
mov r8,numero
sub rsp,32
call sscanf
add rsp,32
cmp rax,1 ;
jl error ; Esta linea y la de arriba son para verificar error
;Paso el numero al rbx
mov rbx,[numero]
;Imprimo el numero ingresado
mov rcx,msjIngreso
mov rdx,[numero]
sub rsp,32
call printf
add rsp,32
;Veo si el numero es mayor que 0
cmp qword[numero],0
jg numeroMayorQueCero
;Veo si el numero es 0
cmp qword[numero],0
je numeroEsCero
;Veo si el numero es menor que 0
cmp qword[numero],0
jl numeroMenorQueCero
;Si llego hasta aca, tuvo que haber algun error
mov rcx,msjError
sub rsp,32
call printf
add rsp,32
ret
numeroMayorQueCero:
mov rcx,msjNumeroMayorQueCero
sub rsp,32
call printf
add rsp,32
ret
numeroEsCero:
mov rcx,msjNumeroIgualQueCero
sub rsp,32
call printf
add rsp,32
ret
numeroMenorQueCero:
mov rcx,msjNumeroMenorQueCero
sub rsp,32
call printf
add rsp,32
ret
error:
;Imprime un mensaje de error y retorna
mov rcx,msjError
sub rsp,32
call puts
add rsp,32
ret