比较汇编中的两个字符串

时间:2018-09-28 20:49:05

标签: assembly nasm

我正在尝试在一个简单的汇编程序中比较两个字符串,但是由于某种原因,它永远不会跳转到给定的目标,但是如果我在eax和ebx中输入5,则相等的跳转就可以了

我正在使用NASM作为编译器

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

1 个答案:

答案 0 :(得分:2)

NASM中常见的绊倒危险:使用mov eax,Inputmov ebx,ok加载了相应标签的地址,而不是此位置的内容位置。这两个地址当然有所不同。要加载内容,您必须将标签用方括号括起来。

要比较任何两个字符串,您必须在循环中逐字节比较它们。但是,根据您的情况,将ok的两个字节加载到WORD寄存器中并将它们与Input的前两个字节进行比较就足够了。

更改

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

mov ax, [Input]
mov bx, [ok]
cmp ax, bx
相关问题