Hello, this is Dr. Miller and this
is Episode ten point three of
Assembly.
Today, we're going to talk about
string functions.
So we'll start with the length of a
string.
So we have the standard library
available to us because we are
using.
And so we can use functions like
Sterling.
And so if we're given a string A
and we want to figure out how long
it is right. We can copy that
address.
We talked about addresses and
pointers, anti-tax,
push that onto the stack and then
call Strollin.
And we just let Sterling handle
all of that. And so that's something
that somebody has already written.
The basically scan through the
string figures out where the
null bite is and then
returns how far it had to scan
in order to get there.
And then we go ahead and correct the
stack.
So we're going to look at maybe some
of the the low level string
functions that we have.
So here are some string
instructions.
So a couple of things to start off
with, so when we're getting ready
to scan a string, we can talk about
the direction flag so
sealed is clear, the direction
flag.
And that means when we are going to
use string operations, we're going
to increment index registers.
We also have set direction flag.
And if we call if we use that
mnemonic in our instruction, it's
going to set the direction flag and
then we will decrement index
registers. So typically we'll use
called for that.
And then when we're looking at
these, we have two different
registers, so we have Essi
the source register and Etai the
destination register and we'll see
those come into play.
So, for example, we've got the load
instructions, each of these has
a different prefix we got by word
and deed word and so load string
byte is Al
is going to equal the
value at S.I and then we'll
increment S.I if the direction
plug is set to increment
and we increment that by one with
a word, we increment it by two
and with a D word, we increment
it by four. So this is to load a
bite and it's going to automatically
use Al and use Essi or
and IACI or X and Essi.
And then store string bite,
so Eddi is going to equal A-L,
so it'll just automatically copy
that into the location at pointed to
by Eddi and then increment Eddi
by one or two
or four.
Right. And we're going to copy.
So we'll copy two bytes or recopy,
which is four bytes.
So store string byte store string
words. Drawstring D word.
We also have the move or
copy, right, because it's not
actually removing the data
from one location.
So it's really a copy, but we use
the Amobi, so we've got move
string byte.
So Eddi is equal to Essi.
The value at IACI is copied into
Eddi.
And here we're copying one byte
because it's string byte.
We also have string words.
So Ediz, again, equal to S.I.
We're going to copy a word
and then we'll increment each of
these by two here.
We increment by one here.
We increment by two.
And here we increment by four.
And so move string dewormed.
We can also scan through a string
so we can compare Al and
the value at Eddi as
a bite, and then we'll set the
flags so we can set the flags
to see if we find that and
basically just keep going through
the string if we
want.
We can also scan for a word
and we're going to use X.
So here we have Al X.
and X four byte
word and delord and
each time is going to use Eddi or
our destination register.
We also have compar string byte, so
compare Essi and Eddi.
So compare those two and then set
flags. And here we're doing a bite.
We do a compare string word.
We do a word.
There's a typo. Compare String D
word and we'll use a D word and
we'll set the flags.
And we also have the repeat
commands, so for repeat,
we will terminate when X
is zero, if
we just say repeat, otherwise
we have repeat equal or zero.
And so we can
terminate when either X is zero
or the zero flag is zero
or we got not equal.
And so then we go into the zero flag
as one or terminate when X
is zero.
So let's look at an example of an
assembly string length.
So here we can take all of
those commands that we've been
talking about and we can put them
together so
we can
hear we're going to use ganef in
order to redistrict so we don't know
what the string is going to look
like.
So we use ganef,
we define a format for that, and
then we push
ah, we get our location
and we want to scan it into we push
yak's and then push the format
and then we'll call SCANA.
And so now A will have a string in
it and we don't have to worry about
that. But here we want to find the
string length.
So, for example, what
we do is we start off and we use
Eddi as our base register.
So we're going to use a scan
function.
So we go ahead and copy a
or the address of a into Eddi so
that it can be used and then we're
going to set X to negative one and
this is going to tell it how long to
run. So we want it to run until we
find a null.
We're going to clear the direction
flag and then the byte that we're
looking for is zero.
So we're going to move at zero
and then we repeat going through the
string, right. Each time Eddi
is going to keep getting incremented
until we find that null byte
and then once we have found it, so
this will terminate when we have
found that or we go through four
billion.
Four billion different addresses to
look for it, which is highly
unlikely.
So at this point now Eddi
is going to point to the no bite in
the string and so we can
copy that address into Yak's
and then subtract the original
address.
And that will give us the length and
that adds in the null.
And so we want to decrement six
by one to subtract
out the null.
And so this is a very
simple way in order to figure out
how long a string is now, it
is more instructions than calling
the standard library, but
it allows you to understand how
we can search through a string and
then find where that bite is in
order to figure out the length of a
string.
So today we talked about some
different string operations and
some different string instructions
that are included in assembly.
In the future, we're going to have
an example that's using strings
and we'll go through and I
believe we're going to uppercase a
bunch of letters and so we go
through that example in a future
lecture.
And then here's our quiz, what
registers are used as a source and
destination for string operations?
ISI is the source and Aidi
is the destination.
If you have questions, you can email
me Milham Jayate, you Ankita Edu,
and you can find me on Twitter at
Milhouse 30.