WEBVTT

00:01.010 --> 00:02.689
Hello, this is Dr. Miller and this

00:02.690 --> 00:04.013
is Episode ten point one of

00:04.040 --> 00:05.040
Assembly.

00:08.320 --> 00:09.489
Today, we're going to talk about

00:09.490 --> 00:10.689
pointers and erase

00:12.130 --> 00:13.130
pointers.

00:14.560 --> 00:15.880
So pointers, what are they?

00:15.940 --> 00:17.529
Well, they point to a location in

00:17.530 --> 00:18.755
memory and this is RAM or

00:19.510 --> 00:20.739
the RAM that you would install on

00:20.740 --> 00:21.740
your computer, and

00:22.600 --> 00:24.100
then you can

00:24.430 --> 00:25.557
get the address of them

00:26.350 --> 00:28.719
by using one of two operations.

00:29.350 --> 00:30.879
So we can either use the move,

00:31.060 --> 00:33.020
assembly pneumonic and have that

00:33.040 --> 00:34.929
or copy the address, or you can

00:34.930 --> 00:36.106
use LYA, which will do a

00:36.460 --> 00:38.019
calculation, but it won't actually

00:38.020 --> 00:39.020
load any memory.

00:40.600 --> 00:42.100
So here are a couple of examples.

00:43.000 --> 00:44.289
So we can have the

00:44.920 --> 00:45.949
array text and we can

00:46.930 --> 00:48.789
move that into yak's and

00:48.790 --> 00:50.049
that will load the address

00:50.740 --> 00:52.599
or similarly we can say

00:52.600 --> 00:54.021
get the data at that address.

00:54.280 --> 00:55.989
So they had data at text

00:56.320 --> 00:57.986
and that will be loaded into yak's

00:58.210 --> 00:59.390
and this will move a D word.

01:01.120 --> 01:02.835
So here we can add another example.

01:03.220 --> 01:05.589
We can copy that address into Yak's.

01:06.010 --> 01:07.774
We can add two to it, which is going

01:07.780 --> 01:09.429
to be called our offset, and then we

01:09.430 --> 01:11.150
can get the data at that location.

01:11.860 --> 01:13.450
So those are a couple examples.

01:14.380 --> 01:16.209
And then if we want to use Leha or

01:16.210 --> 01:18.159
load effective address, we

01:18.160 --> 01:19.581
can basically say load me the

01:19.720 --> 01:21.519
address of the

01:21.700 --> 01:23.680
variable text so that

01:23.830 --> 01:25.660
does the equivalent of that move.

01:26.560 --> 01:28.149
And then here we can just copy the

01:28.150 --> 01:29.949
data and if we want or we could even

01:29.950 --> 01:31.599
use X inside the brackets.

01:32.650 --> 01:34.350
And then here we can use Lia

01:34.720 --> 01:36.279
to do this calculation, so it'll

01:36.280 --> 01:38.049
take the address of text and then

01:38.050 --> 01:39.640
it'll add the offset, be

01:40.030 --> 01:41.157
Ebix into that and then

01:41.920 --> 01:44.180
store that address in SBX.

01:44.200 --> 01:45.640
And so that's one of the benefits of

01:45.650 --> 01:47.259
LIA is that we can do that.

01:48.890 --> 01:49.890
ARray's,

01:50.900 --> 01:52.819
so what are race, an array

01:52.820 --> 01:54.388
is a region of contiguous memory

01:54.950 --> 01:56.389
in the sense that it is all

01:56.390 --> 01:57.530
allocated at once

01:58.370 --> 02:00.409
and it it is contiguous

02:00.410 --> 02:01.549
and that there are no breaks in

02:01.550 --> 02:03.219
between different elements of that.

02:04.460 --> 02:05.869
And this ignores sort of memory

02:05.870 --> 02:07.310
segmentation and pages.

02:08.630 --> 02:10.149
And then an array is defined by

02:10.610 --> 02:12.199
the element size or how big each

02:12.200 --> 02:14.060
element is and then how long it is.

02:14.090 --> 02:15.649
So when you define an array,

02:16.070 --> 02:17.179
you're going to have to tell it how

02:17.180 --> 02:19.099
big it is and how many elements

02:19.100 --> 02:20.180
are going to be in that array.

02:21.050 --> 02:22.189
And then if you're going to actually

02:22.190 --> 02:24.259
use the array, you have a base

02:24.260 --> 02:25.669
address or the address of the

02:25.670 --> 02:27.529
beginning of the array, the offset

02:27.530 --> 02:28.489
that you're looking at.

02:28.490 --> 02:29.869
And then you need to know the size

02:29.870 --> 02:31.789
of each element inside that

02:31.790 --> 02:32.790
array.

02:34.250 --> 02:35.569
So here are some examples of

02:35.570 --> 02:37.249
defining arrays, we can I.D.

02:37.250 --> 02:38.719
the data section where we actually

02:38.720 --> 02:39.720
give them values.

02:40.310 --> 02:42.199
So here's an array of D words,

02:42.200 --> 02:43.670
an array of words, an array of

02:44.090 --> 02:45.090
bytes, or we can

02:46.070 --> 02:47.450
use the best.

02:47.570 --> 02:49.236
And so this is a array of D words,

02:49.610 --> 02:50.809
but we don't give them initial

02:50.810 --> 02:52.249
values. We just say that I'd like

02:52.250 --> 02:54.229
five D words are like

02:54.230 --> 02:56.479
five words or I'd like five bytes.

02:59.490 --> 03:01.349
And then inside of C, we

03:01.350 --> 03:02.549
have a couple of different ways of

03:02.550 --> 03:04.020
actually allocating a raise,

03:05.160 --> 03:07.229
and this is just a little extra info

03:07.230 --> 03:08.896
so you can either allocate them on

03:09.030 --> 03:11.279
the stack or you can use Malac.

03:11.370 --> 03:12.989
And those are the two typical ways

03:12.990 --> 03:14.509
that we define arrays inside of

03:14.820 --> 03:15.820
C, and then C

03:16.650 --> 03:18.479
uses a bracket style notation

03:18.480 --> 03:20.639
to do the offset

03:20.640 --> 03:21.719
of the index.

03:21.970 --> 03:23.685
But we don't have this available to

03:23.820 --> 03:25.049
us in assembly, so we have to

03:25.050 --> 03:26.309
actually do it manually.

03:28.400 --> 03:30.017
So what is the offset, the offset

03:30.110 --> 03:31.482
represents how much from the

03:31.520 --> 03:32.809
beginning of the ride that we're

03:32.810 --> 03:34.759
going to add, so how far into

03:34.760 --> 03:36.229
the array are we going to get?

03:36.830 --> 03:38.449
And that uses those pointers.

03:38.450 --> 03:39.919
And so you take a pointer and then

03:39.920 --> 03:41.809
you add the offset in order to

03:42.440 --> 03:43.812
get the data at a particular

03:44.240 --> 03:45.240
location.

03:45.530 --> 03:46.699
And then you have to do the

03:46.700 --> 03:47.700
calculation of how

03:48.740 --> 03:50.539
big each element is in the array.

03:50.720 --> 03:52.669
And so the standard sizes, as

03:52.670 --> 03:53.993
we've seen, are bitts words

03:54.650 --> 03:55.650
and words.

03:56.000 --> 03:57.889
And so for a character array

03:57.950 --> 03:59.539
or a string,

03:59.780 --> 04:01.779
each index is going to be one.

04:02.180 --> 04:03.739
But if you have a bunch of words

04:03.740 --> 04:05.479
than our index is going to be two

04:06.020 --> 04:07.489
or sorry, if you have a bunch of

04:07.490 --> 04:08.749
words, that's going to be two.

04:08.750 --> 04:10.099
And if we have a bunch of words,

04:10.100 --> 04:11.120
that's going to be for.

04:13.600 --> 04:14.919
So here's an example, so we have

04:14.920 --> 04:16.449
several arrays that we defined

04:16.450 --> 04:17.577
earlier and then we say

04:18.279 --> 04:20.050
we're going to read an integer

04:20.290 --> 04:22.120
and then we're going to moving into

04:22.750 --> 04:24.879
the value add array plus

04:25.240 --> 04:26.240
our offset.

04:26.650 --> 04:28.779
And so Array one

04:28.780 --> 04:29.919
is a bunch of D words.

04:29.920 --> 04:30.999
And we're going to take the beginning

04:31.000 --> 04:32.879
array and we're going to add those D

04:32.890 --> 04:33.849
words and then we're just going to

04:33.850 --> 04:35.000
print off what the integer is.

04:35.020 --> 04:36.309
So hopefully we'd get one, two,

04:36.310 --> 04:37.310
three, four or five.

04:39.340 --> 04:41.229
But we see when we run that code

04:41.560 --> 04:43.149
that when we type zero or we get

04:43.150 --> 04:44.865
one, which is what we expected, but

04:44.890 --> 04:45.890
when we type one, we

04:46.720 --> 04:48.790
get thirty five, five, five,

04:49.150 --> 04:51.279
four, four, three, two or

04:51.280 --> 04:52.456
two, we get 13 one, two,

04:53.320 --> 04:54.320
seven, two.

04:55.060 --> 04:56.060
So why is this?

04:56.860 --> 04:58.179
Well, if we look back at the

04:58.180 --> 04:59.307
beginning here, we said

05:00.100 --> 05:01.668
this is an array of D words, but

05:01.930 --> 05:04.029
we are only adding one each

05:04.030 --> 05:05.696
time through. But a D word is four

05:05.740 --> 05:07.810
bytes. So let's look at how this is

05:08.530 --> 05:10.147
so in-memory. This is how D words

05:10.360 --> 05:11.979
are laid out. These are each bytes

05:11.980 --> 05:13.119
I've written them in hex.

05:13.450 --> 05:15.519
And so we have the number one and

05:15.520 --> 05:16.899
that is at the base I address.

05:17.170 --> 05:18.940
And then the beginning, four

05:19.270 --> 05:21.309
bytes of this or next three

05:21.310 --> 05:22.486
bytes actually are going

05:23.170 --> 05:24.309
to be filled with zeros.

05:24.310 --> 05:25.839
And the number two and these are

05:25.840 --> 05:26.919
filled with zeros and the number

05:26.920 --> 05:28.029
three and these are filled with

05:28.030 --> 05:29.030
zeros.

05:29.260 --> 05:30.789
So our base address is right here,

05:30.790 --> 05:32.064
but we're adding one which

05:32.680 --> 05:33.954
is still part of the same,

05:34.930 --> 05:36.819
our first array and element.

05:37.730 --> 05:38.730
And so

05:39.550 --> 05:41.389
if we look at reading, we're

05:41.390 --> 05:42.379
adding a D word, right.

05:42.380 --> 05:43.509
That's going to be four bytes

05:43.510 --> 05:45.069
starting at offset zero.

05:46.050 --> 05:47.549
And so we read these four bites and

05:47.550 --> 05:48.539
this is our number, and then we

05:48.540 --> 05:50.550
convert that from hex into

05:50.580 --> 05:51.580
decimal.

05:52.150 --> 05:53.767
But if we load offset one, that's

05:54.220 --> 05:55.788
the data starting right here and

05:55.960 --> 05:57.519
we're loading these set of bytes

05:58.150 --> 05:59.319
where we can see if we have the

05:59.320 --> 06:01.419
number zero zero

06:01.420 --> 06:02.792
two zero zero zero zero zero

06:03.430 --> 06:04.430
zero in hex and

06:05.320 --> 06:06.660
we convert that to base 10.

06:06.670 --> 06:08.379
We get the three three five five

06:08.380 --> 06:09.579
four four, three, two.

06:10.690 --> 06:12.454
And so the problem is that we're not

06:12.580 --> 06:14.170
adding the right offset.

06:14.470 --> 06:15.699
Let's look at one more example.

06:15.700 --> 06:17.415
So if we do offset to which we also

06:17.500 --> 06:19.068
printed, we get the numbers zero

06:19.420 --> 06:21.399
zero zero two zero zero

06:21.430 --> 06:22.959
zero zero in hex.

06:23.470 --> 06:24.969
And if we take that and we convert

06:24.970 --> 06:26.409
that into decimal, we get this

06:26.410 --> 06:27.410
number here.

06:27.430 --> 06:29.049
So we start reading at this location

06:29.050 --> 06:30.069
and we go this way.

06:30.070 --> 06:31.660
And that is the number that we read.

06:32.170 --> 06:33.885
And so because we're not adding the

06:33.970 --> 06:35.019
right offset,

06:35.890 --> 06:37.507
because we are not multiplying by

06:37.690 --> 06:39.279
the size of each element in the

06:39.280 --> 06:40.659
array, we're getting the wrong

06:40.660 --> 06:41.660
values.

06:43.310 --> 06:44.535
So to fix that here, when

06:45.140 --> 06:46.904
we read an integer, then we multiply

06:47.300 --> 06:49.339
our offset by four or the size

06:49.340 --> 06:51.319
of each word and then

06:51.560 --> 06:53.226
get the data at Arae plus whatever

06:53.600 --> 06:55.099
that offset is, and then we can

06:55.100 --> 06:56.100
print that.

06:56.570 --> 06:58.187
And when we run that, if we enter

06:58.370 --> 06:59.959
offset zero, we get one.

06:59.960 --> 07:01.430
If we enter offset one, we get

07:01.910 --> 07:02.939
to two. We get three.

07:03.290 --> 07:04.429
Three, we get four and four.

07:04.430 --> 07:05.430
We get five.

07:05.780 --> 07:07.489
Which as we look here, those are the

07:07.490 --> 07:09.199
values that are in our array.

07:09.560 --> 07:10.849
So it's reading each one of those

07:10.850 --> 07:11.850
properly.

07:11.960 --> 07:13.519
And so you got to know the size of

07:13.520 --> 07:15.709
each element and then multiply that

07:15.710 --> 07:16.820
so that you can

07:17.660 --> 07:18.934
get the correct data given

07:19.730 --> 07:20.819
the size of the array.

07:21.680 --> 07:23.269
And so what you should do is maybe

07:23.270 --> 07:24.270
try taking this

07:25.160 --> 07:27.019
code and using Array

07:27.020 --> 07:28.879
two and figuring out how

07:28.880 --> 07:30.829
to go through that

07:30.830 --> 07:31.830
array.

07:34.170 --> 07:35.170
So string's.

07:38.050 --> 07:39.399
So a string is an array of

07:39.400 --> 07:40.400
characters.

07:41.110 --> 07:42.669
Where characters are defined by the

07:42.670 --> 07:44.139
Askey standard, and that's what

07:44.140 --> 07:46.029
those values are, and then each

07:46.030 --> 07:47.304
element is a bite and they

07:47.860 --> 07:49.809
actually ask uses seven

07:49.810 --> 07:51.839
bits, but we fill it out to eight.

07:51.850 --> 07:53.369
So that way it matches the size

07:53.950 --> 07:55.899
that our computers are allowed to

07:56.200 --> 07:57.200
read in store.

07:57.880 --> 07:59.829
And then at the end of an array, we

07:59.830 --> 08:00.908
have a zero or no byte

08:01.690 --> 08:02.759
hex zero zero.

08:03.160 --> 08:04.779
And this tells us that we are done

08:04.780 --> 08:06.129
with the string

08:06.610 --> 08:08.259
and that is the standard that is

08:08.260 --> 08:10.389
used by nearly all programing

08:10.390 --> 08:12.309
languages in order to store the fact

08:12.310 --> 08:14.320
that we are at the end of a

08:14.470 --> 08:15.879
string of characters.

08:17.660 --> 08:19.579
And so here we've defined a an

08:19.580 --> 08:21.679
example here, so we have string

08:21.680 --> 08:23.389
and it says this is a test and then

08:23.390 --> 08:25.105
it has the 10, which is a new line,

08:25.370 --> 08:26.749
and then a zero, which terminates

08:26.750 --> 08:27.750
our string.

08:28.280 --> 08:29.569
And we're actually using a couple of

08:29.570 --> 08:31.639
functions. So this is this

08:31.640 --> 08:32.749
function is going to figure out how

08:32.750 --> 08:34.339
long this string is and then going

08:34.340 --> 08:36.319
to go ahead and print it without

08:36.320 --> 08:37.379
using print string.

08:38.179 --> 08:39.349
So a couple of functions that we're

08:39.350 --> 08:40.259
going to use. We're going to use

08:40.260 --> 08:41.260
Sterling and put car

08:42.350 --> 08:43.428
sterling. We'll figure out the

08:43.429 --> 08:44.239
length of a string.

08:44.240 --> 08:45.563
Pushkar will print a single

08:45.830 --> 08:47.329
character to the console.

08:48.660 --> 08:50.639
So here, in order to use Sterling,

08:50.640 --> 08:52.409
we have to figure out the address of

08:52.410 --> 08:54.059
our string so we load that address

08:54.060 --> 08:56.190
and push that onto the stack

08:56.430 --> 08:58.749
and then call Sterling and then ESX

08:58.830 --> 09:00.960
will have the length of our string.

09:01.930 --> 09:03.069
And then we can add forward to

09:03.070 --> 09:04.449
correct the stack and then we're

09:04.450 --> 09:05.889
going to write a little loop here,

09:06.050 --> 09:07.780
and so to initialize the loop,

09:08.080 --> 09:09.579
we're going to use X because we're

09:09.580 --> 09:10.659
going to loop, right?

09:10.690 --> 09:11.919
We're going to loop up to print.

09:12.550 --> 09:14.069
So we copy that length ticks so

09:14.440 --> 09:16.269
we can do our loop loop properly.

09:17.290 --> 09:18.613
It is going to be our index

09:19.240 --> 09:21.004
or our index that we're going to get

09:21.100 --> 09:22.809
within the array or our offset.

09:23.780 --> 09:24.780
And then SBX is

09:25.820 --> 09:27.379
going to have the address of the

09:27.380 --> 09:28.380
string.

09:28.990 --> 09:30.729
And so here, when we copy the data

09:30.730 --> 09:32.890
in, we're going to copy into X

09:33.610 --> 09:35.539
SBX plus, so

09:35.560 --> 09:37.479
Ebix is our base, Ed is

09:37.480 --> 09:38.480
our offset.

09:38.950 --> 09:40.899
And then all this code right here

09:40.900 --> 09:42.539
is to save all the registers.

09:42.560 --> 09:43.899
We save these three registers

09:43.900 --> 09:46.029
because Pushka will clobber

09:46.030 --> 09:47.030
them.

09:47.050 --> 09:48.519
We then push on the argument that we

09:48.520 --> 09:49.809
want. So we want to print off the

09:49.810 --> 09:50.810
character X, so we

09:51.640 --> 09:53.339
push that onto the stack called

09:53.350 --> 09:55.269
Pushkar. Now that single character

09:55.270 --> 09:56.559
gets printed on the screen

09:57.100 --> 09:58.329
and then we have to correct the

09:58.330 --> 09:59.950
stack. So this is just

10:00.250 --> 10:01.919
we pushed it so we might as well pop

10:01.930 --> 10:03.999
it. And then here we are restoring

10:04.000 --> 10:05.000
our registers.

10:05.770 --> 10:07.209
And then each time through the loop,

10:07.210 --> 10:08.639
we're going to increment IDEX,

10:09.100 --> 10:11.399
so I will step through the array

10:11.830 --> 10:13.153
so it'll go from index zero

10:14.020 --> 10:16.119
one to three four

10:16.360 --> 10:18.220
at the same time, X is being

10:18.230 --> 10:19.589
decrements by our loop.

10:19.840 --> 10:21.459
So we're going back up to the top

10:21.460 --> 10:22.881
each time because we know how

10:23.290 --> 10:24.489
many times we need to print.

10:24.500 --> 10:25.359
So we're going to go through and

10:25.360 --> 10:27.190
print each one of these characters

10:27.490 --> 10:29.169
and we don't have to print the zero.

10:29.350 --> 10:30.429
And so we just go all the way

10:30.430 --> 10:31.430
through the end.

10:33.160 --> 10:34.826
So today, we talked about pointers

10:35.020 --> 10:37.000
and a raise and a very specific type

10:37.080 --> 10:38.350
of array called a string.

10:40.240 --> 10:41.559
And in the future, we're going to

10:41.560 --> 10:43.177
have some examples of a raise and

10:43.420 --> 10:44.649
we're going to actually look at some

10:44.650 --> 10:46.414
more in depth string operations that

10:46.450 --> 10:47.450
we can have.

10:49.040 --> 10:50.216
So here's our quiz, what

10:50.900 --> 10:51.900
is appointer?

10:54.340 --> 10:56.200
It points to a location in memory.

10:57.180 --> 10:58.679
And then what info do you need to

10:58.680 --> 10:59.680
access an array?

11:01.260 --> 11:03.299
Well, you need the base address

11:03.300 --> 11:05.159
pointer, you need the offset,

11:05.160 --> 11:06.389
and then you need to know how big

11:06.390 --> 11:07.590
each one of the elements are.

11:09.380 --> 11:10.999
So if you have questions, you can

11:11.000 --> 11:12.519
email me, Miller, MJ at you and

11:12.860 --> 11:14.479
Kadiatu and you can find me on

11:14.480 --> 11:15.889
Twitter at Milhouse 30.

