Welcome back.
In this lesson, we are going to go through each element of the array using a program.
So over here, this is a program written in assembly that can help you iterate through the array.
So as before, I've already inserted all the elements of the array in this address.
Base address 403180.
And then the second element at this address.
And so on, as we have done in the previous lesson.
So to loop through, we use this program.
The first step is to load the address, the base address, into a register.
So in this case, the base address is 403180.
So we load the base address of the array into -.
- will be our register which holds the base address.
And then we initialize the index to zero.
So - will be our index and also our counter.
And we set it to zero to start off.
Then we enter the loop.
So inside the loop, we have a start of the loop.
This address here will be where the instruction "move" begins.
So load the current element of the array into -.
So to access the first element of the array, we use the formula - plus - times eight.
Since - is the base address, so you plus the offset, you will get the first element.
The first offset will be - times eight.
When - is zero, zero times eight still remains as zero.
So you get back the first element of the array.
Then you increment the counter.
And then after that, you compare whether the counter is less than five.
Why five?
Because there are five elements inside here, so you need to go through it five times.
And then if - is less than five, then you will jump less.
Go back to the loop again and start all over again.
So JL here means jump less than, and then you keep on looping like this.
And each time it loops, it will iterate through the array one by one.
So in the second loop, because your - is now one, one times eight is eight, so eight plus the base
address will take you to this location here.
And then here you retrieve 22, assign it to -.
Then you can do whatever you want with -.
If you wanted to.
Next, you will increment counter.
Counter will become, counter will become two.
And then you compare.
Is two less than five?
Yes.
And it goes up again.
Then it retrieves the third element and so on.
Eventually, - will be five.
So when you compare, five is not less than five.
So you exit the loop and continue with the rest of the code after the loop.
So let us now go inside, inside your x64dbg and put in this code.
So next.
So just go to this line here and type your code, "move -".
So you need this address. For me, it's this address here.
So I just copy this address for you.
It might be different depending on where you put your array.
Then next one will be to "move -, zero".
This will be a counter and an index.
Then will be this one.
Move.
-.
Okay.
Take a note over here.
You are doing the calculation within the square brackets.
So this is permitted inside the assembly.
So you are doing the calculation here.
And then you are dereferencing it to access the value stored there.
And then you assign it to -, assign it to -.
Next, you increase
- by one, and then you compare.
-, 5.
And then here is where you need to get the address of loop start.
Loop start is where this instruction begins.
So the instruction here is loop start.
So we copy that address and come here and type "jump less than" and paste your address there.
So let us check the code, make sure we entered it correctly.
Move -, the base address 1403180, which is here.
Then you move zero to -, then you move this into -.
This is our.
This formula is - plus - times eight.
Then you increment -, you compare - with five.
Then you jump less to the start of the loop, which is 401564.
As you can see, when you click on this line, there's a grayed-out arrow indicating that this is where
it is potentially going to jump to.
So it is correct.
So now we put a breakpoint and run to our breakpoint and then step over.
So now it's going to move this address, base address, into -.
So base address is 1403180.
Step over and you can see 403180 is in -.
Then it's going to move zero to -.
Step over.
And now zero is in -.
Next is going to retrieve the value at the first element of the array, which is 11, and move it into
-.
And now we see 11 is in -.
Then you increase our - by one.
So - is now one.
Initially it was zero.
And then you compare whether one, whether zero is less than five.
Yes, it is less than five.
Therefore, it will jump up to the loop.
The red arrow is highlighted in red, and then the jump is taken is shown down here.
So you step over, goes back up again.
And now it's going to retrieve the next value of the element of the array, which is 22, and move it
to -.
So our - is now 22.
Increase - by one.
- becomes two.
Is two less than five?
Yes.
Jump up again.
And now it's going to be three.
The third element, which is 33.
And move it into -.
Increase -.
- now becomes three.
Is three less than five?
Yes.
Jump up again.
And now you're going to retrieve the next element, which is 44.
And then now 44 is in -. Increase -.
- becomes four.
Compare, is four less than five?
Yes.
Jump back up.
Now we're going to retrieve 55.
Step over, and after 55 is in -, increase -, - now becomes five.
Compare.
-, is it five?
Is five less than five?
No.
So it will not jump up again.
It will quit the loop.
That's it.
So this is how you can use this assembly to implement the loop that can iterate
through the array inside your assembly code.
And this is also how you can implement an array within your x64dbg.
So this will be useful because when you are reversing code, sometimes you can go through the array
directly and directly access the array in memory, and then do whatever you want with the array to alter
the program or whatever you like to do.
So that's all for this video.
Thank you for watching.