haris
haris

Reputation: 159

NASM Assembly; jumps not working in this code

In this code, I am trying to get the compiler to recognize the second character in a string. For example, I input in haris; then I want to be able to iterate to "a" in the string "haris". I use lea to get a pointer to the register store "haris". Then I incremented the register eax, which serves as the pointer and originally points to "h", so then it should point to "a"--right? However, my code doesn't seem to work as intended because if it worked, the jump to _works should be working.

extern _printf
extern _scanf
extern _printf
extern _system
extern _strcmp

segment .data


szPAUSE db'PAUSE', 0
szStrIn db '%s',0
szName db 0,0
szNworks db 'Doesnt work', 10,0
szworks db 'Does work', 10,0


segment .code

global _main

_main:
enter 0,0

;get name. I input "haris" for test
push szName 
push szStrIn
call _scanf
add esp, 8

;;test to see if name was inputted
mov esi, szName
push esi
call _printf
add esp, 4

;;get the address of the second character in szName, which in my case is "a"
lea eax, [esi+1]

;; compare the second character with "a"
cmp eax, 0x61
je _works

;; if the character is not equal to "a"
push szNworks
call _printf
add esp, 4

push szPAUSE
call _system
add esp, 4

leave 
ret

;;if the character is equal to "a"
_works:
push szworks
call _printf
add esp, 4

push szPAUSE
call _system
add esp, 4

leave 
ret

Upvotes: 0

Views: 329

Answers (1)

Jester
Jester

Reputation: 58822

As your comment says, you load the address into eax, not the character itself. Instead of the lea, you should use movzx eax, byte [esi+1] to load the character with zero extension (because you compare to eax later). If you keep the lea you need to insert a movzx eax, byte [eax] afterward. You can use simple mov if you later take care to only use the low 8 bits, such as:

mov al, [esi+1]
cmp al, 0x61

Upvotes: 3

Related Questions