13 hours 15 minutes
Hello. This is Dr Miller, and this is Episode 12.7 of Assembly.
Today. We're gonna given s I M D example.
So, yes, I am d example.
So here's our problem to be solved. So we're going to read a string from the user, and then we're going to read a key from the user, and then we're gonna encrypt that data.
So to start off, we're gonna have a couple different pieces of data here.
We're gonna have several different four minutes,
so I have a
that we're gonna have to formats. One is for reading in a string. Thea other one is for reading in a hex.
I need a name. These appropriately
citizens also a string.
We're also going to allocate some empty space in the BSS.
So this is blank space, but were allowed to read and write to it.
I want to find our array. It is just
space for 64 bites and I dont be used at the end.
We're also going to use a couple of functions. So, for example, renews can f in print off
here. We'll get into our main function.
so in order to get things set up. We're going to have to save our registers.
So we'll set up our stack.
And then we're gonna allocate quite a bit of space a sub s P s p
just to give ourselves some room to work with.
Okay, at the end. What we want to do is we want to fix our stacks or sub
from the frame point of the number four, cause we added to it,
and then pop
frame, pointer and link register.
So now those were set back up.
So what we're gonna do now is we're going to create a pointer to our memory that we're going to use for the frame pointers so we'll go ahead and sub
We're saving toe are one will use the frame pointer and we'll subtract 180.
So this is some number less than this 200 But of course, greater than our four.
So that's gonna be our pointer. We're putting it in our one because
in our zero, we're going to have our format,
and then we'll call scan off.
So that will scan this string here,
and it'll store it on the stack, Um,
with the offset frame pointer.
Um, plus 180.
So down the stack. 180 fights.
So that should save our data there. And then
what we want to do is we want to figure out how long the string is, so you can go and look up documentation on a function called Starlin.
But we'll go ahead and use that.
we need to
store into our zero our frame point or minus or plus 180
and then we'll call branch of link scan. If, er sorry, stir Lynn.
So that will tell us how long are string is? So it's going to give us a result inside of our zero, which is our return result.
And for now, I'm going to copy that into our nine because I may end up using our zero in the future.
All right, now I'm going to set up a loop. So,
um, with this percent x up here, I'm gonna have to read each one of these. And so these are gonna be my key, my encryption keys, But I want to read the same number of encryption keys as I have characters in my string, which is why I called the
So what we can do here is we're going to set up a loop, so move our eight,
So our aid is gonna be our offset.
We'll give it the number zero, and we'll increment that each time.
And we'll just do a compare of our eight and are sorry or eight
and our nine and see if they're equal.
And we're gonna jump to this label down here that we're gonna make so
down here, we're gonna have a label called done reading.
And we'll do stuff after that
if we don't. Well, so this is the place where you come back up to so we'll call this top
and then at the bottom of our loop were going to say brands to top.
So always jump back up there and then do our comparison.
All right, so
we're done reading. But now we need to set up our pointers. We're going again. Call scan F
and so are one is gonna have to be our pointer to our memory. So we're going to
sub are one
frame pointer and here we're gonna copy this data into a different location.
So, listen, was that 1 80? I gave us
80 bites to go into her at 100.
and then so are offset is stored in our eight. So are a We're gonna increment each time so we could go ahead and to add or eight
our eight, the number one.
So our aid is being added each time, and it starts off zeros. We have offset zero.
But when I have to add that to our pointer in our one, So add are one
So add those two together
to get the We've got our correct offset stored in our one. Now, we need our format in r zero,
okay? And then we can call scan. If
so, that should allow us to read in. Right. And each time we're gonna loop through because we do this at the bottom.
And so we'll go ahead and loop through in here and,
um, read that number of keys that we need for our data.
All right, so now we're done reading,
and then we're gonna want to do our exclusive or for our encryption, so we gotta load it into our registers. So
I'll start sub r zero frame pointer
So load in need the text key that they entered
and then we'll do V l D E r
So we'll go ahead and load that into D zero
at our zero,
and then we're gonna use,
um, are one
for the next one.
So our encryption key is at 100
the l d r
64. Well, hadn't do D one,
and then we're going to get the data at
Okay, so now we can do our exclusive. Or so the EU or
D two d
Doesn't matter the order d one and d zero.
All right, so now we've encrypted it, but now we need to save it so that we can print it off.
That's what we have our array for.
So when do L d r r one
get the address of our array
we will go ahead and,
um, do RVs tr
We'll save d two at the location pointing to an r one slot Syria that we loaded in.
So I should save our data. We also will want to move a zero in there so
move or three
the number zero and then I'll do ah str
are three into.
We'll do our one. So we're just going to read in a maximum of eight bytes is
what we're doing just for this example, so that we can fit it in.
So we'll set by eight automatically to
a zero when we'll actually set a bunch of lights here. So we'll set three by four, bites to zero.
All right, so now we're ready to print. LD r r one
array LD R R zero
Our format that we want to use the print and then
All right, so
couple of typos here. So I got my ask. Yes.
And then I think I forgot my colon after my label here.
so if I enter
I'll just do Matt to start, and then my encryption key is zero x won t rex three.
And you can type these one at a time. We'll do him here in a second,
so I got l B. P s.
So now if I run it again and type l B. P s
and then I enter my encryption key one
I'll get mad. So here I would. My encryption key is right here. And so I took Matt. I encrypted it. I got this value here
and then the only way to decrypt it is to know what the
key here is. And so I go ahead and
type that in again, and I get my original text back.
So today we used s I m d to do example of doing encryption. Using, exclusive or
looking forward, we'll talk about some mode and then conditional execution.
If you have questions, you can email me, Miller MJ at you and Kate I e to you, and you can find me on Twitter at Maha sturdy.