Hello. This is Dr Miller, and this is Episode 10.3 of Assembly.
Today, we're gonna 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're using GCC 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 in t a X,
push that onto the stack and then call Sterling and we just let sterling handle all of that. And so that's something that somebody has already written
basically scans through the string, figures out where the null byte 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 gonna look at maybe some of the low level string functions that we have. So here are some string instructions.
So a couple things to start off with.
So when we're getting ready to scan a string, we can talk about the direction flag
so C L D is clear. The direction flag. And that means when we are going to use string operations, we're gonna increment index registers.
We also have set direction flag. And if we call with, we use that
a pneumonic or instruction, it's going to set the direction flag. And then we will deck Ament index register. So typically will use CLD for that.
And then when we're looking at these, we have two different registers. So we have yes, I the source register and Edie I 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 bite word and D word. And so load string bite is a l is gonna equal yet the value at Yes, I And then we'll increments. Yes, I if the direction flag is set to increment
and we increment that by one with a word we implemented by two. And with a d word wing commended by four. So this is the load a bite and it's gonna automatically use a l and use Yes, I or a X and s I or e accent? Yes, I
and then store string bite So, Edie, I is gonna equal a l.
So it will just automatically copy that into the location at pointed to by E D I. And then in cumin you d I buy one
or two or four, right? And we're a copy a X, so we'll copy two bites or copy E X, which is four bites.
So store string, bite store, string words store string 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 used a movie
so we got move string bite. So e d I is equal to Yes, I see the value a t s eyes copied into E. T. I. And here we're copying one bite because it's string bite.
We also have string words. So beady eyes again equal to Yes, I we're gonna copy a word
and then we'll increments each of these by two. Here we increment by one Harry increment by two. And here we increment by four.
And so move string d word.
We can also scan through a string
so we can compare a l and the value at Edie I as a bite. And then we'll set the flags so we can set the flags to see if we find that and will basically just keep going through the string
We can also scan forward and we're gonna use a X. So here we have a l, A, x and E X for bite word in D word.
And each time is going to use E d I or our destination register.
We also have compare string bite, so compare Yes, I and E D I. So compare those two and then set flags. And here we're doing a bite.
We do a comparison inward. We do a word on there's a type of compare string d word and will use the D word and we'll set the flags,
and we also have the repeat commands. So, for repeat, we will terminate when e. C X is zero. If we just say, repeat.
Otherwise we have repeat equal or zero, and so
we can terminate when either e c X zero or the zero flag is zero or we got not equal. And so then we go until the zero flag is one or terminate when E C 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 have been talking about and we can put them together.
So we can, um here, we're going to use can f in order to read a string so we don't know what the string is going to look like.
So we use can f we define a format for that and then we push, um,
are we get our, ah, location that we want to scan it into, we push yaks and then push the format and then we'll call scan up. And so now they will have a string in it, and we don't have to worry about that. But here we want to find the string length.
what we do is we start off when we use e g I as our base register. So we're gonna use a scan function
so we go ahead and copy a or the address of a into ET I so that I can be used,
and then we're gonna set e c X to negative one, and this is going to tell it how long to run. So we wanted to run until we find a no,
we're gonna clear the direction flag. And then the bite that we're looking for is zero. So we're gonna move a l zero
and then we repeat going through the string, right? Each time he d I is going to keep getting incriminated until we find that no bite.
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, Edie, I is going to point to the no bite in the string.
And so we can copy that address into yea X and then subtract the original address and that will give us the length. And that adds in the knoll. And so we want to decker mint. You see x by one
to subtract out the no.
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 script string and then find where that no bite is in order to figure out the length of the 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 gonna have an example that's using strings, and we'll go through. And
I believe we're gonna upper case 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?
Yes, I as a source. And Edie eyes the destination.
If you have questions, you can email me, Miller, MJ at you and Katie to you, and you can find me on Twitter at Milhouse 30.