0

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
Martin
  • 1
  • 1
  • 2
    `"%li"` for scanf is a `long int`, but on Windows x64 that's a 32-bit type. So you're only scanning into the low dword of the qword you reserved at `numero`. Use a debugger to examine memory, you should see `0x00000000FFFFFFFF` after scanning `-1`, since you only told scanf to write the low 32 bits, but told `cmp qword` to read all of them. – Peter Cordes Oct 22 '22 at 09:12
  • Thank you so much!!! Changed from li to lli and solved the problem :), I thought li was for 64 bits, mb there. How can I choose your question as "best answer" and problem as solved? I'm new here. Thanks again! – Martin Oct 22 '22 at 09:43
  • It's already marked as resolved, as a duplicate. I didn't need to write a full answer because people already have, for the same problem of a `scanf` result never being negative because of the wrong format for a qword. Unfortunately your edit flagged it for reopening. – Peter Cordes Oct 22 '22 at 09:45

0 Answers0