0

I'm new to assembly trying to compare two 5 byte long strings together from user input. For example, the user types EEEEE then enter then ABCDE then the program compares the 2 strings together. I'm looping it until it finds a wrong character or loops 5 times. The problem I have is that it will not point to the next character after the first character. For example, If you input AAAAA and ABCDE then the program will output that its identical to each other, but if you input AAAAA and BBCDE it will out not identical to each other. How do I get to point to the next letters in the string to compare them?

section .data
output1:     db 'IDENTICAL',10    
outputLen1:  equ $-output1            

output2:     db 'NOT INDENTICAL',10    
outputLen2:  equ $-output2            

newline: db 10

section .bss
string1 resb 5 ;hold first string
string2 resb 5 ;hold 2nd string
filler resb 5 ; take space
counter resb 1 ;countdown from 5

section .text
    global _start

_start:
;read first input
mov eax,3            
mov ebx,0           
mov ecx,string1        
mov edx,5                   
int 80h              

;take space
mov eax,3            
mov ebx,0           
mov ecx,filler        
mov edx,2                  
int 80h    

;read second input
mov eax,3            
mov ebx,0           
mov ecx,string2        
mov edx,5                  
int 80h              

;move address
mov eax, string1
mov ebx, string2

;amount of times to loop
mov [counter], byte '5'

;Loop 5 times without being unidentical = true
;if al and bl equals each other keep looping
loop: 

   mov al, [eax]  ; load a character from string1
   mov bl, [ebx]  ; load charcter from string2
   cmp al, bl
   jne false       ; if not eqaul, cout << "Not identical"

   inc eax ;move to next letter in string
   inc ebx ;move to next letter in string

;count down from 5
DEC byte [counter]
cmp [counter] , byte '0' ;count down to zero
jz  true; if both strings match

jmp loop

false:
;display message
mov eax,4            
mov ebx,1           
mov ecx,output2        
mov edx,outputLen2                 
int 80h  
jmp end

true:
;display message
mov eax,4            
mov ebx,1           
mov ecx,output1        
mov edx,outputLen1                 
int 80h  
jmp end


end:
;end program
mov eax,1            
mov ebx,0            
int 80h;
Noelle472
  • 1
  • 3
  • If you know they're exactly 5 bytes, you don't need to loop, just do a dword compare and a byte compare for the final byte. Or pad the buffers with zeros so you can do one 8-byte compare (e.g. in 32-bit mode with SSE2 `movq` / `pcmpeqb` / `pmovmskb eax, xmm0` / `test al, 0b00011111` to only check the low 5 byte elements of the compare, so the padding doesn't even have to be zero.) – Peter Cordes Sep 30 '22 at 02:01
  • As for your actual code that loops a byte at a time, you can keep the counter in a register (such as ECX), so you're doing `dec ecx` / `jnz loop_top`. And use integer `5`, not its ASCII code `'5'`. But the actual problem is that you're replacing the low bytes of your pointers with the characters you load: AL is the low byte of EAX. You can see this when you single-step with a debugger; your pointer will change, and your next load will get garbage. Use different registers for your pointers, like ESI and EDX. – Peter Cordes Sep 30 '22 at 02:05
  • No, ESI and EDX are 32-bit registers. Use those for your pointers instead of EAX and EBX, so you can keep loading bytes into AL and BL. (Unless you *want* to load and compare your string data in 4-byte chunks instead of 1 byte at a time, which would be much more efficient but you'd have to deal with the fact that 5 isn't a multiple of 4.) – Peter Cordes Sep 30 '22 at 02:24
  • @PeterCordes I changed the registor to mov edx, string 1 move ecx, string2 then I change al to eax, [edx] and bl to ebx, [ecx] and cmp eax, ebx. Now the first 4 charcters are working but the 5ft character is not effecting the output. Do you know what the problem is. Thanks for the help. – Noelle472 Sep 30 '22 at 02:42
  • When you single-step in a debugger, what values get loaded into AL and BL on the final iteration? `cmp eax, ebx` compares 4 bytes. Like I've said repeatedly, you should *only* change the pointers, keep using AL and BL for the data you're comparing (unless you do byte loads into dword registers with `movzx eax, byte [ecx]` and `movzx ebx, byte [edx]`, which is actually more efficient and the normal way to do byte loads. [How to load a single byte from address in assembly](https://stackoverflow.com/q/20727379)). Presumably with dword loads you're getting garbage from after your buffers – Peter Cordes Sep 30 '22 at 05:57

0 Answers0