WEBVTT

00:01.570 --> 00:03.099
Hello, this is Dr. Miller and this

00:03.100 --> 00:04.329
is Episode ten point three of

00:04.330 --> 00:05.330
Assembly.

00:06.060 --> 00:07.139
Today, we're going to talk about

00:07.140 --> 00:08.140
string functions.

00:09.210 --> 00:10.469
So we'll start with the length of a

00:10.470 --> 00:11.470
string.

00:12.090 --> 00:13.609
So we have the standard library

00:13.710 --> 00:15.449
available to us because we are

00:15.450 --> 00:16.450
using.

00:16.770 --> 00:18.600
And so we can use functions like

00:18.960 --> 00:19.960
Sterling.

00:20.100 --> 00:21.920
And so if we're given a string A

00:22.260 --> 00:24.059
and we want to figure out how long

00:24.060 --> 00:25.481
it is right. We can copy that

00:26.010 --> 00:26.849
address.

00:26.850 --> 00:28.049
We talked about addresses and

00:28.050 --> 00:29.429
pointers, anti-tax,

00:29.940 --> 00:31.439
push that onto the stack and then

00:31.440 --> 00:32.249
call Strollin.

00:32.250 --> 00:34.229
And we just let Sterling handle

00:34.230 --> 00:35.549
all of that. And so that's something

00:35.550 --> 00:36.869
that somebody has already written.

00:37.470 --> 00:38.819
The basically scan through the

00:38.820 --> 00:40.710
string figures out where the

00:40.740 --> 00:42.749
null bite is and then

00:42.750 --> 00:44.639
returns how far it had to scan

00:44.640 --> 00:45.640
in order to get there.

00:45.750 --> 00:46.979
And then we go ahead and correct the

00:46.980 --> 00:47.980
stack.

00:48.030 --> 00:49.259
So we're going to look at maybe some

00:49.260 --> 00:50.583
of the the low level string

00:51.060 --> 00:52.320
functions that we have.

00:52.620 --> 00:53.879
So here are some string

00:53.880 --> 00:54.880
instructions.

00:55.720 --> 00:57.279
So a couple of things to start off

00:57.280 --> 00:58.897
with, so when we're getting ready

00:59.290 --> 01:01.005
to scan a string, we can talk about

01:01.120 --> 01:02.950
the direction flag so

01:03.340 --> 01:04.810
sealed is clear, the direction

01:05.290 --> 01:06.129
flag.

01:06.130 --> 01:07.539
And that means when we are going to

01:07.540 --> 01:08.769
use string operations, we're going

01:08.770 --> 01:10.779
to increment index registers.

01:11.620 --> 01:13.629
We also have set direction flag.

01:13.630 --> 01:15.760
And if we call if we use that

01:16.570 --> 01:18.187
mnemonic in our instruction, it's

01:18.310 --> 01:19.749
going to set the direction flag and

01:19.750 --> 01:21.122
then we will decrement index

01:21.310 --> 01:22.869
registers. So typically we'll use

01:22.870 --> 01:24.010
called for that.

01:25.010 --> 01:26.329
And then when we're looking at

01:26.330 --> 01:27.499
these, we have two different

01:27.500 --> 01:29.479
registers, so we have Essi

01:29.510 --> 01:31.078
the source register and Etai the

01:31.310 --> 01:33.079
destination register and we'll see

01:33.080 --> 01:34.080
those come into play.

01:35.590 --> 01:37.409
So, for example, we've got the load

01:37.420 --> 01:38.939
instructions, each of these has

01:39.400 --> 01:41.319
a different prefix we got by word

01:41.320 --> 01:43.419
and deed word and so load string

01:43.420 --> 01:45.040
byte is Al

01:45.370 --> 01:46.399
is going to equal the

01:47.260 --> 01:48.583
value at S.I and then we'll

01:49.150 --> 01:50.620
increment S.I if the direction

01:51.040 --> 01:52.480
plug is set to increment

01:53.350 --> 01:55.419
and we increment that by one with

01:55.420 --> 01:57.310
a word, we increment it by two

01:57.640 --> 01:59.159
and with a D word, we increment

01:59.470 --> 02:01.289
it by four. So this is to load a

02:01.300 --> 02:03.069
bite and it's going to automatically

02:03.070 --> 02:05.049
use Al and use Essi or

02:05.530 --> 02:07.629
and IACI or X and Essi.

02:08.620 --> 02:10.709
And then store string bite,

02:11.170 --> 02:13.149
so Eddi is going to equal A-L,

02:14.260 --> 02:15.789
so it'll just automatically copy

02:15.790 --> 02:17.554
that into the location at pointed to

02:17.710 --> 02:19.229
by Eddi and then increment Eddi

02:20.020 --> 02:22.119
by one or two

02:22.120 --> 02:22.999
or four.

02:23.000 --> 02:24.000
Right. And we're going to copy.

02:24.550 --> 02:26.469
So we'll copy two bytes or recopy,

02:26.860 --> 02:27.860
which is four bytes.

02:28.450 --> 02:30.399
So store string byte store string

02:30.400 --> 02:32.379
words. Drawstring D word.

02:34.100 --> 02:35.276
We also have the move or

02:35.930 --> 02:37.279
copy, right, because it's not

02:37.280 --> 02:39.319
actually removing the data

02:39.320 --> 02:40.099
from one location.

02:40.100 --> 02:41.509
So it's really a copy, but we use

02:41.510 --> 02:42.882
the Amobi, so we've got move

02:43.550 --> 02:44.419
string byte.

02:44.420 --> 02:46.669
So Eddi is equal to Essi.

02:47.000 --> 02:48.568
The value at IACI is copied into

02:48.770 --> 02:49.770
Eddi.

02:49.880 --> 02:51.469
And here we're copying one byte

02:51.470 --> 02:52.729
because it's string byte.

02:53.330 --> 02:54.529
We also have string words.

02:54.530 --> 02:56.830
So Ediz, again, equal to S.I.

02:56.900 --> 02:58.279
We're going to copy a word

02:58.790 --> 03:00.019
and then we'll increment each of

03:00.020 --> 03:01.020
these by two here.

03:01.400 --> 03:02.625
We increment by one here.

03:02.660 --> 03:03.949
We increment by two.

03:04.310 --> 03:05.889
And here we increment by four.

03:06.470 --> 03:07.999
And so move string dewormed.

03:10.240 --> 03:11.919
We can also scan through a string

03:12.550 --> 03:13.726
so we can compare Al and

03:14.590 --> 03:15.590
the value at Eddi as

03:16.600 --> 03:18.070
a bite, and then we'll set the

03:18.460 --> 03:19.881
flags so we can set the flags

03:20.680 --> 03:22.570
to see if we find that and

03:22.660 --> 03:24.009
basically just keep going through

03:24.010 --> 03:25.010
the string if we

03:25.840 --> 03:26.739
want.

03:26.740 --> 03:28.809
We can also scan for a word

03:28.810 --> 03:30.159
and we're going to use X.

03:30.190 --> 03:32.169
So here we have Al X.

03:32.170 --> 03:34.180
and X four byte

03:34.210 --> 03:36.069
word and delord and

03:36.070 --> 03:37.687
each time is going to use Eddi or

03:37.900 --> 03:39.169
our destination register.

03:40.030 --> 03:41.979
We also have compar string byte, so

03:41.980 --> 03:44.050
compare Essi and Eddi.

03:44.500 --> 03:46.117
So compare those two and then set

03:46.300 --> 03:48.539
flags. And here we're doing a bite.

03:49.150 --> 03:50.522
We do a compare string word.

03:50.830 --> 03:51.830
We do a word.

03:52.750 --> 03:54.150
There's a typo. Compare String D

03:54.160 --> 03:55.679
word and we'll use a D word and

03:56.110 --> 03:57.110
we'll set the flags.

03:59.060 --> 04:00.259
And we also have the repeat

04:00.260 --> 04:01.436
commands, so for repeat,

04:02.120 --> 04:04.069
we will terminate when X

04:04.070 --> 04:05.070
is zero, if

04:06.050 --> 04:07.999
we just say repeat, otherwise

04:08.000 --> 04:10.099
we have repeat equal or zero.

04:10.490 --> 04:12.499
And so we can

04:12.500 --> 04:14.479
terminate when either X is zero

04:14.480 --> 04:16.579
or the zero flag is zero

04:16.820 --> 04:18.069
or we got not equal.

04:18.079 --> 04:19.879
And so then we go into the zero flag

04:19.880 --> 04:21.154
as one or terminate when X

04:22.430 --> 04:23.600
is zero.

04:25.810 --> 04:27.427
So let's look at an example of an

04:27.580 --> 04:28.690
assembly string length.

04:29.730 --> 04:31.589
So here we can take all of

04:31.590 --> 04:32.879
those commands that we've been

04:32.880 --> 04:34.319
talking about and we can put them

04:34.320 --> 04:36.149
together so

04:36.270 --> 04:37.270
we can

04:38.340 --> 04:39.908
hear we're going to use ganef in

04:40.140 --> 04:41.429
order to redistrict so we don't know

04:41.430 --> 04:42.809
what the string is going to look

04:42.810 --> 04:43.799
like.

04:43.800 --> 04:45.749
So we use ganef,

04:46.140 --> 04:47.519
we define a format for that, and

04:47.520 --> 04:48.520
then we push

04:49.980 --> 04:52.259
ah, we get our location

04:52.260 --> 04:53.975
and we want to scan it into we push

04:54.160 --> 04:55.829
yak's and then push the format

04:56.160 --> 04:57.180
and then we'll call SCANA.

04:57.230 --> 04:58.896
And so now A will have a string in

04:58.980 --> 05:00.239
it and we don't have to worry about

05:00.240 --> 05:01.906
that. But here we want to find the

05:01.920 --> 05:02.920
string length.

05:03.180 --> 05:05.009
So, for example, what

05:05.010 --> 05:06.539
we do is we start off and we use

05:06.540 --> 05:08.750
Eddi as our base register.

05:08.790 --> 05:10.162
So we're going to use a scan

05:10.470 --> 05:11.470
function.

05:12.320 --> 05:13.939
So we go ahead and copy a

05:14.210 --> 05:15.778
or the address of a into Eddi so

05:16.190 --> 05:17.856
that it can be used and then we're

05:18.080 --> 05:19.939
going to set X to negative one and

05:19.940 --> 05:21.409
this is going to tell it how long to

05:21.410 --> 05:23.076
run. So we want it to run until we

05:23.330 --> 05:24.769
find a null.

05:25.490 --> 05:26.629
We're going to clear the direction

05:26.630 --> 05:28.247
flag and then the byte that we're

05:28.370 --> 05:30.010
looking for is zero.

05:30.020 --> 05:31.579
So we're going to move at zero

05:32.210 --> 05:33.739
and then we repeat going through the

05:33.740 --> 05:35.809
string, right. Each time Eddi

05:35.810 --> 05:37.459
is going to keep getting incremented

05:37.760 --> 05:39.589
until we find that null byte

05:40.470 --> 05:42.319
and then once we have found it, so

05:42.350 --> 05:44.089
this will terminate when we have

05:44.090 --> 05:45.649
found that or we go through four

05:45.650 --> 05:46.650
billion.

05:49.050 --> 05:50.549
Four billion different addresses to

05:50.550 --> 05:51.929
look for it, which is highly

05:51.930 --> 05:52.930
unlikely.

05:53.220 --> 05:54.445
So at this point now Eddi

05:55.170 --> 05:56.885
is going to point to the no bite in

05:57.000 --> 05:58.889
the string and so we can

05:58.890 --> 06:00.829
copy that address into Yak's

06:01.200 --> 06:02.670
and then subtract the original

06:02.790 --> 06:03.689
address.

06:03.690 --> 06:05.454
And that will give us the length and

06:05.880 --> 06:07.459
that adds in the null.

06:07.470 --> 06:09.300
And so we want to decrement six

06:09.390 --> 06:10.390
by one to subtract

06:11.220 --> 06:12.220
out the null.

06:12.390 --> 06:14.069
And so this is a very

06:14.490 --> 06:16.107
simple way in order to figure out

06:16.170 --> 06:18.059
how long a string is now, it

06:18.060 --> 06:19.769
is more instructions than calling

06:20.220 --> 06:22.199
the standard library, but

06:22.470 --> 06:23.989
it allows you to understand how

06:24.420 --> 06:26.086
we can search through a string and

06:26.220 --> 06:27.899
then find where that bite is in

06:27.900 --> 06:29.189
order to figure out the length of a

06:29.190 --> 06:30.190
string.

06:31.710 --> 06:33.029
So today we talked about some

06:33.030 --> 06:35.009
different string operations and

06:35.010 --> 06:36.779
some different string instructions

06:36.810 --> 06:38.850
that are included in assembly.

06:40.300 --> 06:41.469
In the future, we're going to have

06:41.470 --> 06:42.989
an example that's using strings

06:43.450 --> 06:44.724
and we'll go through and I

06:45.370 --> 06:47.036
believe we're going to uppercase a

06:47.050 --> 06:48.459
bunch of letters and so we go

06:48.460 --> 06:50.028
through that example in a future

06:50.470 --> 06:51.470
lecture.

06:52.140 --> 06:53.369
And then here's our quiz, what

06:53.370 --> 06:55.019
registers are used as a source and

06:55.020 --> 06:56.970
destination for string operations?

06:59.110 --> 07:01.040
ISI is the source and Aidi

07:01.120 --> 07:02.120
is the destination.

07:03.380 --> 07:04.849
If you have questions, you can email

07:04.850 --> 07:07.009
me Milham Jayate, you Ankita Edu,

07:07.010 --> 07:08.389
and you can find me on Twitter at

07:08.390 --> 07:09.230
Milhouse 30.

