0

I just started learning assembly, as a fun challange. I have made the obligatory hello world program as well as a simple one that just asks for input then displays it. Now i think the next step would be a simple 'guess the number i'm thinking of' program, here's the code

      section .data
           First:  db 'Try and guess the number im thinking of',10
           Firstlen equ $-First
           Correct:  db 'YAY! you guessed my number correctly',10
           Correctlen equ $-Correct
           Fail:  db 'You Suck',10
           Faillen equ $-Fail
      section .bss
          num resb 5
      section .text
          global _start
      _start:
          mov eax,4
          mov ebx,1
          mov ecx,First
          mov edx,Firstlen
          int 80h

          ;get users number
          mov eax,3
          mov ebx,2
          mov ecx,num
          mov edx,5
          int 80h


          ;compare number
          mov edx,num
          cmp edx,7
          je correct; jump if equal
          jne fail; jump if not equal to exit
      fail:
          mov eax,4
          mov ebx,1
          mov ecx,Fail
          mov edx,Faillen
          int 80h

          ;exit
          mov eax,1
          mov ebx,0
          int 80h
      correct:
          mov eax,4
          mov ebx,1
          mov ecx,Correct
          mov edx,Correctlen
          int 80h

          ;exit agian
          mov eax,1
          mov ebx,0
          int 80h 

No matter what i do the fail function is always called. So i assume that i'm not comparing what i think i am, but i cant seem to find any help online. What am i doing wrong?

ps. I am using nasm 2.11.05

  • 1
    When you get input from the user using `int 0x80` the buffer is filled with the characters representing numbers. So you need to convert a string of ascii characters representing digits into an integer and then `cmp` that number with the value you want to test. – Michael Petch Feb 21 '16 at 04:52
  • Although not an exact duplicate this question and the answer may help you: http://stackoverflow.com/questions/19309749/nasm-assembly-convert-input-to-integer – Michael Petch Feb 21 '16 at 05:22
  • Ah I see makes sense. Guess i have a lot more reading to do – Patrick Greene Feb 21 '16 at 05:29

1 Answers1

0

you can use the short jump equivalents of je/jne as they're within the same module:

jz correct
jnz fail

je/jne are used for jumps across a "further" distance in memory. They essentially do the same thing. A subtraction takes place for the comparison, and then jumps depending upon the flags set. In this case, the zero flag is set if they are equal.

X0r
  • 113
  • 5