Everybody welcome to next video.
So in this video and I'll be talking about arithmetic.
So addition subtraction multiplication division and a incremental increase in decremental increase.
So all knowing all this is gonna be important when you're manipulating numbers in assembly so the first
thing we're gonna do is take a look at addition.
Now we're gonna be working with the lower levels of the register such as a L for X low and then a h
for high or a X for half the register.
We've gone over this in different in the registers video so if you need a recap go ahead and go over
there we're not gonna be recapping it specifically here.
Now first we want to do is understand how addiction works so additions and assembly works with the add
command.
Now what you do after that is you give it a register or a value.
In my case it's a out and then you're gonna give it whatever value you want to multiply the register
of a L or the sub register of all by.
In my case I'm going to do 10 someone to 0 x 10.
Now above this we need to put something in L because l doesn't have anything in it right now.
So if we move into Al we'll do 0 x 5 right.
So we don't really need to do anything else.
So if we go ahead and run this this is a shortcut command I just tagged all the commands one after another
and just execute it.
We can see it does nothing.
So this compiled linked it and then ran it does nothing because what you're actually need to see is
in the registers themselves because when you do a an add command the result of the ADD is going to be
in the E X register.
So this is where I can introduce you to GDP which is the Linux debugger.
So this course isn't going to go over how to use GDP at a very high level or at a very low level.
What we're gonna do is we're going to touch on specific acts but aspects of GDP that we're going to
be using to showcase what's actually happening in memory.
So to run GDP it's GDP and then the application.
So in this case made.
So right now if we were to just click run it would run the executable.
But we need to create something called a breakpoint to stop execution from continuing past the start.
So as soon as we hit here in the code we wanted to stop there so we can actually see what's happening
right.
So before we do this we need to set the disassembly flavor to Intel because what we're writing right
now is Intel based assembly because if we do this symbol of what to do.
We didn't actually run the code yet.
So disassemble start we can see that looks a little bit different than what we're working with.
So what we need to do is set this assembly flavor to Intel.
So if we set that same disassemble underscore start which is our symbol which is our label here as we
can see we go ahead and run that.
Now we can see we've got our move l 0 x 5 move L or add a l 0 x 10 and then we've got our exit conditions.
Great.
So now what we want to do is create a breakpoint.
So what this is going to do is tell the code when it's executing when it gets to this particular point.
It wants us to stop.
So we'll do brick underscore start.
So this puts the starter at this memory address which happens to be the very first command under under
the label start.
So you go ahead and do run.
We'll see.
Perfect.
It started the application and then and then it breakpoint.
So again if we do disassemble we can see that the arrow right here indicates that we are currently at
this instruction.
Now there's a few things I want you to look at.
So if we do info registers we will see X EDC x x x ESB EVP ISI VDI VIP X as an example.
So you can see all of this happening right now.
Yeah.
X has nothing.
So what we want to do is we're gonna be passing fifty five to AOL which is the lower half of X so to
step one instruction which is what this is.
This move instruction we're going to step into that and have it run but we're not going to have it run
everything else we want it to stop right afterwards.
So that works with Step I.
So we go ahead and do that.
We can do in four registers are able to disassemble first.
So if we disassemble we'll see the register has or the arrow is moved which indicates that we've executed
this command and we're waiting to execute this one.
So if we do in four registers you'll see right here.
Yeah.
X has fifty five which is the decimal equivalent to eighty five.
So let's do another step and we'll do another disassemble so we can see here.
We've passed the ad and now we're back on to the next move.
So if we go to infill registers we can see we've added 10 to this which equates to 0 x sixty five which
is 1 0 1 so we can see that the value of the result of our add instruction has worked correctly so to
exit we'll just do it.
And yes we want to quit.
That'll kill the application.
So what happens if we have something too big so let's say we have move a.
We'll do zero x 20 right or let's do
let's make a completely full we'll do move into X we'll do zero x eight Fs.
So that's basically full that's full all the bits are set to 1 and that's it the whole registers.
So that gives us a full registers what happens if we add to this full register.
Well if we just did add let's say let's say add Al we wanted to do 0 x 10.
Right.
So if we did this which we can we can run it now just to kind of show you an example of what is going
to do
what we need to do is actually tell it to use the Kerry flag.
Now we've talked about flags before and I've mentioned Kerry flag before but what's gonna happen is
ad we'll just attempt to add it like normal.
However there is an overflow that's happening here.
There's the registers too big.
So what you need to do is do 80 C so add Kerry.
So this is going to do is it's going to tell the CPA you that there is a Kerry and the application was
too there was too much data in X for it to just add normally.
So it sets a Kerry flag.
However if we go ahead and run this we see nothing happens to.
And that's perfectly fine but what we need to do is actually set the Kerry flag so there's a few different
ways to do this with STC which is set the carry.
So we actually set the Kerry flag then we have CLEC which clears the cap flag so that way in case it's
been set we unset it and then we have a CMC which is complement to the Kerry flag.
This means if it's on currently inset it will turn it off and if it's off it'll turn it back on.
So this is important.
So what we're going to want to do is we want to set the Kerry flag before we do ADC which is the ad
with Kerry.
So again if we go and run this we see nothing's wrong.
Perfect.
So let's go ahead and open GDP we'll do we'll set suddenly flavor until then we'll do break start and
we'll run it.
We'll do info registers.
Right.
So we see nothing's there.
And we do this symbol perfect.
So we got our move.
So we'll do Step II and we'll take a look at registers.
We can see we've got our f there all the way across the screen.
If we disassemble we can see we're at set the carry flag so to view flags we're going to invoke registers
e flags so as we can see we have just so the IMF flags set.
Which is fine.
Well we want to see is the C F flag which is the carry flag.
So if we do step I can see under disassemble we've executed the instruction and then e flags we see
we've got to see f here which is our carry flag.
Perfect then we want to look at our regular registers and we see nothing's happened yet but if we do
step I now under disassembly we should see that we have added the ADC and if we do it for registers
we see that the last 10 bits was overwritten or the last the last bits were overwritten because we're
doing the lower bits of Al on X and we've added zero x 10.
So it's important to understand that when you're doing addition with large characters or large sets
of data you make sure that you set the carry flag when you need to in most cases it's you're going to
be able to know if the data is too big or not to add because in at least in the red team's perspective
you're going to be doing this yourself with specific restrictions put in place by the machine or whatever
exploit you're trying to do.
Now we've got our edition setup right.
So we've gotten quite out of here.
Now the next thing we want to look at is subtraction.
Clear this.
So with subtraction we're going to set say Xerox 20 and into Al
and we will do we'll do is we'll subtract 5 0 x 5.
So the way we do this is sub SUV and we tell it what register we want to subtract with and then 0 x.
Right.
So go ahead and compile this.
Well that's not 0.
That's zero.
Well let's go right now.
Perfect.
So Gordon GDP break start said so some new leaf flavor intel and Ron so if we do disassemble.
Perfect.
Glenn step by and then info registers so we can see we've got 30 to 12 step.
I again we should have subtracted that.
Perfect.
So we've got one B so that's all subtraction works and subtraction also works with the carry flag.
Now not going to go.
I'm not going to compile this and run it.
However you knew didn't you need to set the carry flag like this and then to subtract it with the carry
flag it's as b the that's all you're gonna want and then you're going to do whatever register you want
and then by whatever value you want.
So that's how you do subtraction.
So now that we've done addition subtraction we're going to want to work on multiplication.
But what's important here is when we need to write this table out for you guys.
So when we multiply a l by anything with a bits the value is going to reside in a X. That's important.
And then when we do when we multiply x by 16 bits the results are gonna be in D X and X and we multiply
x by any 32 bit value.
The answer is going to be an X and X.
It's important to keep this in mind when working with multiplication there is a similar chart for division
which will go over and a little bit but just keep this in mind when we're going through this.
Now the first thing we want to do is we're gonna move a value into Al right.
So we'll do let's see we'll do 0 10 again.
Now for multiplication there's something specific we need to do we need to move the second value into
the B L register that we want to multiply with.
So in this case to be L and then we're going to do zero X..
Now we've got that set we want to multiply B L by whatever's in a out because the E X register is what's
being used to being multiply against whatever we want.
So in this case we'll do me well which is for multiplication and then the register we want to multiply
by.
So this is essentially doing at all times.
All
let's go ahead and view that in the debugger.
Perfect.
So go ahead and be step by step.
I again.
So if we disassemble we can see we are at the point of multiplication going to do in four registers.
So we can see we have 10 in X and 5 x which is exactly what we expect to hear.
So when we do our multiplication we expect our answer to be in your step by we can see here that if
we do disassemble we have performed the multiplication and if we do our in four registers we then have
our answer.
But what's interesting to note is X does not change.
It still holds the value which is why I said it's important to understand what's in the registers at
all times.
So you can know what to expect.
So if you planned on using x for something you'd have to know that you left that value in there and
then it's not zero by default so that's got our multiplication now we don't need the carry off like
set in this particular case because of the table here and what registers it gets bumped to so next we're
going to quit out of this and then we're going to take a look at the division now division has again
a similar chart so if we have a X and we divide that by 8 bits the answer will be an Al and the remainder
will be an age.
Now if we did D X and X and divide that by 16 bits the answer would be an X.
The remainder index.
And again for the last part it's 80 X and e x divided by thirty two bits.
The answer is in E X with the remainder index.
So does it make more sense in just a minute when we actually write the code for it.
But keep in mind keep in mind in this chart now the source code above is going to have all of this information
and all the comments and a little bit nicer of a description than I'm doing right now.
So the first thing we do is going to move and do a x.
We're going to move 0 x 1000 number going to move into c x which is the register we're gonna use to
perform the division.
So in C X we're going to move your x 5.
Now to perform division what we're going to want to do is do D Ivy divide and then the register.
So this is going to divide C X by whatever happens to be an X. So we will go ahead and compile this
and run it.
So we'll do break start.
So it's this assembly
flavor 2.0 we set our break already.
We're going run then we'll do just assemble we could see round the first one or two step by step by
then we'll do info registers so we can see right now.
In C X we have five and E X we have 1000 so in this case it would be four thousand ninety four ninety
six divided by five so if we do disassemble again we can see right there.
We're going to do a step by go ahead and info registers for more time so we can see we have our answer.
So we've got our answer of 0 x 33 but in EDI X because we were doing X we see our remainder wasn't D
X which we can see right here
so there's one more thing that I want to go over so we're gonna make the assumption that x is zero which
it will be when we first start the application.
What I want to show you is how to increment in and decrement the particular register.
So if we have X at the zero so we'll just do we'll just set it to zero by default even though it should
already be there if we want to increase the value of registered by 1.
We're going to I NC.
And then the register in this case x to decrease it.
We want to do deck X so this will increase the registered by 1 and decrease the value registered by
1.
So let's go ahead and do that in the debugger.
Go ahead and compile it and run it.
Break start.
Set this up simply
Labor internal clock run and we'll do this.
Symbol perfect.
So in this case we will do we're already setting it to zero.
So we'll do step by then.
Info registers so we can see right now.
We are at the first increment so if we go ahead step I we look at our registers.
We can see now we've got a to 1 we disassemble we'll see we're on the deck.
So if we do step by again and disassemble we've now documented and if we do it for registers we'll see
that now it's set to 0.
It's important to understand when increasing and decreasing a register because when we start working
through loops will we be using the counter register which is e c x and then we're going to want to decrease
or increase the counter as we will depending on the count of the loop.
So this is how arithmetic works take time mess around with different values try to string some things
together based on the answer of one try to implement PEM dos for how the application should flow when
it comes to going through a formula.
So play around with it.
Get used to manipulating the numbers in an assembly.