Hello, this is Dr. Miller, and this
is Episode eleven point nine of
Assembly.
Today, we're going to talk about
arms race and arm indexing.
So an armory, there are
a lot of different methods of
creating array.
And what an array is, is just a
consecutive sequence of bytes.
So it's a bunch of bytes that are
all in order and we can't
sort of break them up.
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.
So this would be 13
words. So 13 times four
gives us whatever that number is,
it'll allocate that much space.
We also technically when we use an
array of
bytes for a string, so it's an
array of characters.
So that's also an array.
So it's defined as each one of these
has an index or an offset
and they all take up size one for
each one of those.
And then additionally, we can define
array by using the word so I
can call it an array and I can
define what the values are for each
one of those. So we have an
array of words that has values, one
through five.
So when we have an array, we have to
be able to index or go somewhere
into that array.
And so if we have an array here
that, again, has size 13 times
four, we can use Eldard
to get the address of that
array and then we can use
an addition in order to get a
particular offset.
So this would be our zero plus
four, which would be index one.
So if you deal in a high level
language, you would use
a bracket one if they were all
integers.
Here we have to actually calculate
how far that is.
So if we're using words that would
be offset for.
And then we're just picking a value
to move into R two
for that, and so you can see here
the brackets denote that we were
referencing that as a pointer.
So this is a pointer.
You add four to the pointer and then
you get the value of that.
Now, additionally, you can directly
use an offset.
So instead of me having to add it, I
can say R zero and then, oh, by
the way, add the number four in
there.
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
again if we were using words inside
of our ring.
So arm indexing, so ARM has
additional index modes that we
can take advantage of
and then the two additional modes
other than just normal indexing our
price index and post index.
So price index, the offset is added
before you try and access that
element.
And then post index is after
you access that element
or either read or write that piece
of data.
So here we got a couple different
examples showing all the different
types of indexing, so we've got pre
indexing and post indexing in
normal.
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.
And so our zero gets the value of
our two plus eight.
So it's going to offset to
if these are these are words.
So each one of them takes up four.
So that would be offset to
at the end.
Our two is not changed in this
example.
Now we have pre indexing, which
means that we are going to take our
two and we're going to add it, add
eight to it before we do the
access.
But when we're all
done, two then ends up being updated
by eight.
So it's getting the thing at
at our two plus eight
when we are trying to access it.
And here you can see that the the
the bang, that means that it is pre
indexed.
So it's going to add the offset
before it gets the access.
So in this example, if we have
array, the value that's
going to get loaded into our zero is
going to be our zero
plus eight.
So here is plus zero plus four
plus eight. So our zero is going to
get the value two or three.
Now, if we look at in the post
indexed, so here we
are taking our two and we're going
to add eight to it at the end.
So we're adding eight to update the
pointer.
But the value that gets stored in
our zero is going to be the
value that's at R2.
So R2 has the base of our array.
So the value that it's going to get
is the number one.
Right? So it's going to get offset
zero.
So that's you can see the difference
between pre index and post index and
both of them. We're updating our two
to say that we've moved
forward some amount, but
it just depends on when it reads the
value, if it reads the value
after or before.
And then we can look at an example
here. So here we are defining
an array that has 100
elements in it.
So we're skipping 100.
And then we're going through here
and we are we go ahead and load
that address into our two
and we load our counter into our
three, we set it at zero.
And then we can see here we are
doing a store register.
So our three is our offset.
Our two is the beginning of the
day and our three.
So we're basically writing the
offset into the array
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
for this one and then we're
comparing it to one hundred
times four. So we see
if there's we'll go through one
hundred elements inside this array
and try and do that.
And then we loop around
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.
We we are assuming
that each one of these is four
bytes. So we're figuring
out what our offset is going to be.
So we have to multiply the number
that we read from the user by four
because each element takes up four
bytes is what we're assuming in this
example.
And then it'll go ahead and it will
load the array and then again use
that offset our one,
which was the R
r r zero, which is the offset,
and R one, which is the
array to load that number and print
it. Right.
So if they enter the number zero,
they would get the number zero.
If they enter one, they would
get four I
believe.
And so it goes through and does that
now we can change this.
So that we add the post right back,
so here we again are we have
our three gets the number zero.
And so when we do the store,
we were going to
write it right.
The value to our two.
So our two has our array.
So we're writing to that pointer.
And then we're at when we're all
done, we're adding four.
So we removed a necessary
step, right. So that if we
put it all in one command because
we're post writing back and here
I'm adding one to our three so that
we can go through a hundred times,
that allows us to to write the
number zero one, two, three, four.
So I'll write a little bit different
numbers to the array.
And if we want, you can type this in
and you can test it and see
if it does what you think it does.
Right. And so again, we are
multiplying by four because the user
each one of these is integers and
then we'll go ahead and do a
load of that number and print it off
for the user so they can see
what number that was in the array at
that location.
So we can use these when
we are using loops so that we don't
have to do multiple commands we can
basically do due to operations
in one command.
So today, we talked about armories
and arm indexing.
I'm looking forward, we'll do an
example using an array, we'll code
that live and then
we will look at the
multiple load in store.
And so there are additional
functions that we can look at.
So given the array above, so
are two is our array, how
do we offset the array at offset
our two with a right back of plus
four?
So here we go ahead and access it
and then we are going to add four,
we wanted a post
post right back
here.
So if you have questions, you can
email me Milhem, you include
IDU and you can find me on Twitter
at Millhone Sturdy.