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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.