0

I am having trouble showing if the number is odd or even. Is there a better way to get user input? It looks like I am getting a string from the user input.

.data
     num1 dw 70 dup(?)
     prompt db "Enter the number: ",0
     result db "This is your value: ",0
     evenmsg db "This is an even number",0
     oddmsg db "This is an odd number",0

.code
    start:
       push offset prompt
       call StdOut

       push 70
       push offset num1
       call StdIn


       mov ax, num1
       and ax, 1
       jz even_num
       jnz odd_num

    even_num:
       push offset evenmsg
       call StdOut
       jmp exitx
   odd_num:
       push offset oddmsg
       call StdOut
       jmp exitx
exitx:
    

end start
How
  • 1
  • 4
  • You don't need to convert the whole string to a binary integer; checking the low bit of the low digit is sufficient because (base) 10 is a multiple of (base) 2. (And the ASCII code for `'0'` is even.) So after reading input, you need the string length to find the last digit character. You haven't shown the `StdIn` function; if that's text input, you're checking the low bit of the *most* significant digit, so your code would say that 14 is odd. – Peter Cordes Jan 25 '23 at 03:05
  • In general for converting string to integer, [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) shows the standard algorithm. Also note that it's a bit unusual to use `dw` for ASCII text input, but if your `StdIn` function needs some buffer space for the input text and then replaces the low 16-bit word with an integer value, that could make sense. But in 32-bit code you normally would use 32-bit integers (unless you have a large array and want to save cache footprint / memory bandwidth, or some other reason for picking a specific size.) – Peter Cordes Jan 25 '23 at 03:08

0 Answers0