Comparison of vectors in the assembly

I have two vectors; one of them (VectorA) is entered by the user, and the other (VectorB) contains a bunch of single-digit numbers. Both are char vectors. The idea is that the program should compare VectorA numbers in order to make sure they are valid numbers by comparing them with tags in VectorB that contain all valid numbers.

If all the numbers in VectorA are in VectorB, the program returns 0. If any of the VectorA numbers is not in VectorB, the program returns 1 instead. EAX Return Register.

Here is the code, I hope it is not too dirty, but carry me (also please excuse me if I use the 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: 

Firstly, I know that I probably shouldn't have used EAX as an index, but after trying with a number of other registries, EAX was the only one that did not do YASM freak out.

The problem is ... It does not seem to work. The first test cycle works fine. If the last VectorA number is 9, it actually jumps to the found_it tag and does what it should do. However, no matter what the second number is, he never finds it on VectorB.

For debugging purposes, I added the line mov esi, [VectorB+rcx-1] directly above the main CMP line, where two values ​​are compared. I learned that for the first time they are compared, ESI has the right value. However, the second time the program passes this instruction, ESI returns 14648, which, of course, does not match the contents of EAX in the next line.

Does anyone know what I'm doing wrong?

+6
source share
3 answers

Oops, I think I found a problem ... It seems that the eax register is not intended for single-byte content (vectors have characters in them whose size is equal to bytes). I changed the lines ...

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

... before...

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

And now it works. It seems that eax actually read 4 bytes of the vector instead of 1.

Thank you anyway.:)

+1
source

Two obvious mistakes to start with

mov edi, 5

and yet the first cmp has edi-1, so it starts at 4

====

mov rcx, 10

and yet the first cmp has rcx-1, so it starts at 9

====

cmp edi, 0

jne character_1

mov rcx, 10

character_1 resets the loop to 10 in the middle of the edi loop, is this normal?

====

I don’t know what rcx is, I assume that its ecx is used for the character_2 loop

mov rcx, 10 I assume it is ten digital, not 16 binary

Once you sort all the little details, they will swing, but the devil is in the details with these teenage procedures.

==========

"If the last VectorA number is 9, it actually jumps to the found_it tag"

This is because the jump always resets rcx to 10, then has cmp rcx-1 (therefore rcx = 9)

therefore it is 10 9 cmp 10 9 cmp 10 9 cmp instead of 10 cmp 9 cmp 8 cmp 7 6 etc.

0
source

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 

cycle symbol_2

What happens if there is no match?

edi will be edi-1 forever

You still need to find a way to discount edi

edi = edi-1 is a curious thing

you do

dec edi

if there is a match, but keep edi the same if there is no match

0
source

Source: https://habr.com/ru/post/943925/


All Articles