 # Array Indexing

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:00
Hello. This is Dr Miller, and this is Episode 11.9 of Assembly.
00:05
Today we're gonna talk about armor raise and arm indexing.
00:09
So in our Marais,
00:11
there are a lot of different methods of creating array. And what an array is is just a consecutive sequence of bites.
00:17
So it's a bunch of bites that are all in order, and we can't sort of break them up.
00:23
And so here we got a couple of different examples. So here we can use the dot skip, and then we can use a little bit of math,
00:29
so this would be 13 words. So 13 times four
00:34
gives us whatever that number is. It will allocate that much space.
00:38
We also technically, when we use an array of bytes for a string, so its array of characters So that's also an array. So it's defined as each one of these has an index or an offset.
00:50
Um, and they all take up size one for each one of those.
00:53
And additionally, we can define array by using the dot word so I can call it an array. And Aiken, define what the values are for each one of those We have
01:02
an array of words that has values 135
01:07
So when we have an array, we have to be able to index or go somewhere into that array. And so if we haven't array here that again has size 13 times four,
01:15
we can use LD R to get the address of that array, and then we can use, in addition, in order to get a particular offset.
01:25
So this would be our zero plus four, which would be index one. So if you deal in a high level language,
01:32
you would use a bracket one if they were all imagers. I'm here. We have to actually calculate how far that ISS So if we're using words that be offset for
01:42
and then we're just picking a value to movinto are too
01:46
for that.
01:47
And so you can see here the brackets denote that were de referencing that as a pointer. So there's a pointer. You had four to the pointer and then you get the value of that.
01:55
Now, additionally, you can directly use an offset. So instead of me having to add it, I can say are zero and then Oh, by the way, add the number four in there.
02:05
So again it gets the same value. So this would be offset. One offset zero. You would add zero but offset one. You would add four
02:13
again if we're using words inside of our ray.
02:16
So arm indexing. So arm has additional index modes that we can take advantage of
02:23
on the two additional modes, other than just normal indexing are pre index and post index.
02:29
So pre index, the offset is added before you try and access that element and then post indexes after you, um, access that element or either read or write that piece of data.
02:42
So here we got a couple different examples showing all the different types of indexing. So we got pre indexing and post indexing and normal.
02:50
So this is what we saw in the first example. So we have. Our two has our address of our array, and then here we are, adding eight to that offset,
02:59
and so are zero gets the value of R two plus eight. So it's getting going to offset to if, for these air, these are words. So each one of them takes up four slide be offset to
03:09
at the end right R two has not changed in this example.
03:14
Now we have pre indexing, which means that we're going to take our to and we're gonna add it. Add eight to it before we do the access.
03:21
But,
03:22
um, when we're all done, our too, then ends up being updated by eight. So it's getting the thing at at our two plus eight. Um, when we are trying to access set
03:34
and here you can see that the bang, that means that it is pre indexed.
03:38
So it's gonna add the offset before gets the access. So in this example, if we have a ray, the value that's going to get loaded into our zero is going to be,
03:49
um, are zero plus eight. So here's both zero plus four plus eights are zero is going to get the value two or three.
03:58
Now if we look in the post indexed. So here we are taking our two, and we're gonna add eight to it at the end. So we're adding eight to update the pointer,
04:08
but the value that gets stored in r zero is going to be the value that's at are two. So are, too, has a base of our race. So the value that it's going to get is the number one right, So it's going to get offset zero.
04:18
So that's you can see the difference doing pre index imposed Index in. Both of them were updating our to to say that we've moved,
04:26
um, forward some amount.
04:28
But, um, it just depends on when it reads the value. If it reads it, I you, um after or before
04:42
and then we can look at an example here. So here we are, defining an array that has 100 elements in it.
04:49
So we're skipping 100
04:51
and then we're going through here, and we are We go ahead and load that address into our two. We load our counter into our three it we set it at zero,
05:00
and then we can see here we are doing a store register. So are three is are offset. Are too is the beginning of the ray and our three. So we're basically writing the offset into the array.
05:13
There's just so that we would get some different values. And then here we are, manually updating our three. So here we are actually doing the update of the index,
05:21
um, for this one. And then we're comparing it to 100 times for so we see if there's
05:29
we go through 100 elements inside this array and try and do that, and then we loop around,
05:33
um, with this branch less unequal, the top, and then when we're all done loading the array, we go ahead and read a number from the user.
05:41
We
05:42
we are assuming that each one of these is four bites. So we're figuring out what are offset is going to be. So we have to multiply the number that we have read from the user by four, because each element takes up four bytes is what we're assuming in this example.
05:57
And then it will go ahead and it will load the array. And then again, use that offset are one which was the star R E R zero, which is the offset and are one which is the array to load that number and printed.
06:12
All right, so if they enter the number zero, they would get the number zero. If they enter one they would get, um, before I believe
06:19
right. So it goes through and does that now we can change this
06:27
so that we add the post right back.
06:30
So here we again are we have are three gets the number zero.
06:34
And so when we do the store, we're going to, um
06:39
right, right. The value to our to
06:42
So our two has our array.
06:44
So we're writing to that pointer, and then we're at when we're all done, we're adding for so we removed a necessary step, right? So that it we put it all in one command because we're post writing back.
06:55
And here I'm adding one toe are three so that we can go through 100 times.
06:59
Um, that allows us to to write the number 01234 So subtle, right? A little bit different numbers to the array.
07:06
And if want, you can type this in and you contest that and see, um if it does, we do you think it does, right? And again, we are multiplying by four. Because the user each one of these is imagers. And then we'll go ahead and do a
07:21
a load of that number and printed off for the uses they can see.
07:25
Um, what number? That was in the area at that location.
07:29
So we can use these when we are using loops so that we don't have to do multiple commands weaken Basically do due to operations in one command.
07:40
So today we talked about arm arrays and arm indexing.
07:44
I'm looking forward, will do an example using an array will code that live. And then we will look at, um the multiple load in store. And so there are additional functions that we can look at.
07:58
So, given the array above, so are too is our array. How do we offset the array at offset Are too with the right back of plus four.
08:09
So here we go ahead and access it, and then we are going to add for right. We wanted a
08:16
ah post post right back here.
08:22
So if you have questions, you can email me, Miller MJ at you and Kate I e to you. And you can find me on Twitter at Milhouse 30
Up Next