Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing vectors in assembly

Tags:

assembly

I have two vectors; one of them (VectorA) is introduced by the user, and the other (VectorB) contains a bunch of single digit numbers. Both of them are char vectors. The idea is that the program has to compare one by one the numbers of VectorA to make sure they are valid numbers, by comparing them to the ones in VectorB, which contains all the valid numbers available.

If all the numbers in VectorA are contained in VectorB, the program returns 0. If any of the numbers of VectorA is not present in VectorB, the program returns 1 instead. The return register is EAX.

Here's the code, I hope it's not too messy, but bear with me (also please excuse if I use wrong terminology since English is not my native language)...


    MOV edi, 5     ;VectorA is a 5 digit vector.

character_1:
    mov rcx, 10    ;VectorB is a 10 digit vector.

character_2:
    mov eax, [ebx+edi-1]       ;ebx contains the address of VectorA    
    cmp eax, [VectorB+rcx-1]    
    je found_it    
    loop character_2  

    mov eax, 1    
    jmp end_comp   
found_it:

    dec edi    
    cmp edi, 0    
    jne character_1

    mov eax, 0
end_comp:

First off, I know I probably shouldn't have used EAX as an index, but after trying with a whole bunch of other registries EAX was the only one that didn't make YASM freak out.

The problem is... It doesn't seem to work. The first verification loop works fine. If the last number of VectorA is a 9, it actually goes to the found_it tag and does what it has to do. However, no matter what the second number is, it never finds it on VectorB.

For debugging purposes, I added a mov esi, [VectorB+rcx-1] line right above the main CMP line where the two values are compared. I found out that the first time they are compared, ESI has the correct value. However, the second time the program goes through that instruction, ESI returns a 14648 value, which of course doesn't correspond to the contents of EAX in the next line.

Does anyone know what am I doing wrong?

like image 561
user2333787 Avatar asked Apr 29 '13 22:04

user2333787


1 Answers

Whoops, I think I found the problem... It seems the eax register is not meant for single-byte contents (the vectors have characters in them, which are byte-sized). I changed the lines...

mov eax, [ebx+edi-1]    
cmp eax, [VectorB+rcx-1]

...to...

mov al, [ebx+edi-1]    
cmp al, [VectorB+rcx-1]

And now it seems to work. It seems eax was actually reading 4 bytes of the vector instead of 1.

Thanks anyways. :)

like image 159
user2333787 Avatar answered Nov 04 '22 17:11

user2333787