简体   繁体   中英

Comparing two strings in assembly

I am trying to compare two strings in a simple assembly program but for some reason it never jumps to the given destination but the jump if equal works fine if i put 5 in eax and ebx

I am using NASM as a compiler

SECTION .bss
SECTION .data
EatMsg: db "Eat at Joe's",10
EatLen: equ $-EatMsg
Input: times 100 db 0
ok: db "ok"
oklen: equ $-ok
TastyMsg: db "Its tazty",10
TastyLen: equ $-TastyMsg
SECTION .text 
global _start 

   _start:
   nop
   mov eax,4
   mov ebx,1
   mov ecx,EatMsg
   mov edx,EatLen
   int 80H

   mov eax,3
   mov ebx,0
   mov ecx,Input
   mov edx,100
   int 80H

   mov eax,Input
   mov ebx,ok
   cmp eax,ebx
   je tasty
   mov eax,1
   mov ebx,0
   int 80H

   tasty:
   mov eax,4
   mov ebx,1
   mov ecx,TastyMsg
   mov edx,TastyLen


   int 80H
   mov eax,1
   mov ebx,0
   int 80H

A popular tripping hazard in NASM: With mov eax,Input and mov ebx,ok the address of the respective label is loaded, not the content at this location. These two addresses differ of course. To load the content you have to enclose the labels with square brackets.

To compare any two strings, you have to compare them byte by byte in a loop. In your case, however, it is sufficient to load the two bytes of ok into a WORD register and compare them with the first two bytes of Input .

Change

mov eax,Input
mov ebx,ok
cmp eax,ebx

to

mov ax, [Input]
mov bx, [ok]
cmp ax, bx

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM