Hello, this is Dr. Miller and this
episode, fifteen point four of
assembly today
we're going to write a 64 bit
function, so
64 bit function.
So what's our problem to be solved?
Well, we're going to write a function
that's going to dump the registers
and it's going to print out all of
the 64 bit registers
that are for general purpose use.
And then we're going to use the fast
call conventions.
So here I've already started a
project, so for this
we can look at the files, so
we're going to have our
iodized include.
So in this, it's saying that we are
going to write some functions.
So we're going to write the dump
regs function.
And I've already written a couple of
other functions that
we can use.
And so inside of IO
assume
I did take the time to
type in this long
string here. So this is going to
have registers.
So we've got RSX, we
got the zero X in the front
and then we've got percent and
then we're saying we want leading
zeros, we'd like 16 digits.
We're going to do a long hex
for each one of these.
And so I've got each register.
So RSX Urbex
asks RDX.
I sprinkled a few new lines in here
so that we can read it, put a couple
of extra new lines here at
the end and then terminated with
a zero.
And then when we're going to start
this, we're going to start down here
in our dump regs function.
So the first thing is to set up our
stack and we're actually going to
use the stack quite a bit because
here we don't want to change any of
the registers. And so we're going to
have to save all of our registers
as we go.
So the first thing is we'll start
with our prolog.
So, again, we have
our four hour prefix and not E,
so we got RB and then we're going
to move our
V.P.
RSP.
So our stack pointer is that RSP
the thing at the end of our epilog,
we're going to move RSP
are BP
and then Pop are
BP.
So we've got our Prolog in our
epilog set up and
we're going to need our BP in order
to reference A,
R, BP and B RSP
when we get going here.
So at the beginning, we're
going to push a bunch of our
registers on so that we can restore
them when we're done and I'm going
to put offsets here.
So we got push RSX
and that's at minus eight.
So
when we're looking at RSX,
right, we've got our saved our BP
and then we also have our
we're going to call this function.
And so we're going to have a return
address. And so this is at
our BP
minus eight.
And I'll keep writing references so
that we can keep track of them when
we're going to use them later.
So push our backs,
push are C
X.
So this is at minus 10.
So remember that
we are dealing with sixty four bit
and so
we're going to.
Be at 16 here,
and so 16 is 10 and Hecks,
and this is at minus 18
Hick's and I got to type
the right letter here,
Bush RDX
and that's at
20 Hecks.
Again, this.
But my mind is their
push RSI
minus.
Twenty eight
Burchard Devi.
That's at minus 30
Hecks.
Whoosh
and then I'll do our eight.
So that's it. Thirty eight
or nine,
that's it, minus.
40.
OK, and then
I'm going to set this up and
what we're going to do here is we're
actually going to
be pushing on some of the arguments
that we're going to use at the very
end. And so
we're actually going to do RSP at
this point and then Arbi and then
we'll do the other 10
through our 15.
So since I've already saved our
eight, I can move
into our eight
hour BP and
then I can add to our eight
are B.
10X, so the reason
that I'm going to add 10 Hecht's, so
I have eight
for the Pushtun version
of our BP
and then I have eight for
the return address.
And so I want to get the stack here
at this point.
So I have to add 16
or 10 and Hecks, and
now I can push on
our eight and this is going to
represent RSP.
Now, I also want to do our bpy,
so move
our eight
disvalue at our BP.
So that'll give me my old RB
because RB points to its old
version of itself, so it
offset zero and then I can push
are eight.
Now, the reason why I did this is
I'm actually going to use these.
These are at the very end.
If we look here, we got RB and RSP
and so I'm going to do
them backwards because that'll
be the easiest way to do it.
So then I need to go through
our 15, our 14 or 13
or 12.
So I need to save all of those on to
the stack and I'll use
them actually to restore when I'm
all done. So push
are 15.
Persch are 14.
Bush are 13.
Our 11.
OK, and then as far
as parameters go, this is actually
parameter number nine
and so one is
a.
Oh, sorry.
10,
11, 12,
13,
14, and then we got 15
and 16 for these two.
This is
our VPI.
All right, so now I've saved them
all on to my stack,
one of the things we're going to
want to do is restore them.
So I'm going to do that at this
point. And then we're going to set
up a couple of extra
ones that we've already done
here. So I got to pop
them in the reverse order.
So, Pop, are.
10
or 11
or 12
or 13
or 14.
OK, so these are the registers
that I just got done pushing on,
I also then need to add
to RSP.
Eight times to
know no, sorry, that RSP
10.
All right, so up here, I pushed on
two parameters.
So that's 10 Hecks and I don't need
the values of them.
So I'm just going to
correct the stack there.
And now I need to pop off all my
general ones that I push on again.
So Pop are nine
popma
sorry. Are eight
or nine
or eight.
RDX
or SCIEX
are Becs,
OK, so
this should be the reverse order
that I had done them before.
You just double check here.
I got eight, nine and then nine,
eight.
OK.
So we've set up these parameters
basically nine through
16 we needed
and then set the rest of them up.
So the easiest way to do that
is to basically move into registers
the that we're going to care about
on the scroll up so we can see
our offsets here.
So I'm going to use our eight
because I am going
to use that at the very end
or one of the end ones.
So it's an easy one and it's shorter
than some of the others.
So move into our
eight.
So for this one, I want to do
parameter eight.
And so parameter eight is
going to be our nine.
And so RSP,
so we're going to reference our our
our BPE when our efforts
stack pointer and we're going to
subtract
40 Hecks.
So if we look for our nine, we see
that that that's at 40 Hecks below
our bpy.
So we'll go ahead and move that into
our eight and then just say
push are eight.
OK, so the next one we're going to
do is
seven.
And if we looked at our format,
string number seven is going to be
our eight.
So again, we move into our eight
RB DP
minus.
So our aid is going to be at
thirty eight, some minus
30, Aztec's
push are eight.
OK, the next one, then we're
going to do
is six, and that's going
to be Ardai.
So move into our 8:00
hour, BP
minus.
So that's at 36.
And then push out great,
so here we just keep moving
these in, so that way we can push
them onto the stack because again,
all of the things that
aren't going to fit in our first set
are going to have to go on the
stack. And so here is our last
one that we have to do this way.
So we're going to do five
and it's going to be Auris I.
So if we look up there,
move into our eight.
So RSI is at minus
twenty eight.
R, B,
minus 20 ATX.
And then we push it.
So that takes care of
and it help if I can spell these.
All right.
So now that we have them all on to
the stack, then we can do our
our last four parameters
in the way that we need to do them.
So the fourth parameter goes into
our eight.
And for that, I'm going to use
RDX,
so that's a parameter for
move into our.
SCIEX.
Well, it actually ends up being asks
for that one, so that's parameter
three for parameter
two, it's RDX.
And the argument that I had put on
there is going to be our
big X,
so I have RDX, R6
Urbex and then
our X move
into ARCI.
So that's the location registered
that we have to have and we need RSX
and that is parameter one
for our print
and then we use
our DI for
our format string.
So this is called the register
dump
and then we for using print f we
need to move into RSX,
the number zero.
And then we should be able to
call that function.
So we will be able to
call Trent F.
With reference to the penalty.
And then we did push on
some parameters here, and
so we're going to go ahead and
correct those.
So if we see here, we pushed on
one to.
Three, four,
three, four,
one, two, three, four.
So add to
RSP
eight times four,
so we'll just add into
it we can do the math there and so
that should make it all work
out.
So the hard part was making sure
that we got our format string
appropriate for all of those
inputs.
I ended up leveraging
the fact that we needed to save all
these registers.
So that way we had
less time spent doing it.
Otherwise I'd have to push every
single one of the registers
and then copy all their contents
back onto the stack again.
And so these registers
here being pushed onto the stack are
going to be used inside of the
printer because it's easier
to do that than to do
this complicated step of copying
something from the stack and then
pushing it back on.
It's just a simpler way in order to
do that.
OK, so
that should be our function.
See if I made any
mistakes.
Oh, what a day
that I mistype it here.
Yep.
So here, that's printer F.
And I might have
lower case D, right?
All right, so now we've built our
project, and so what I want to
do is go into fifteen
point four
RASM
and so here we
can call dump rigs.
So some fun things
to do is to pick some registers
and move X
one move
or be X.
To
three, and then
we end up calling dump rags.
All right,
and so then we end up with one
guy or one or two or three,
print it off and you can see we have
things like BP and RSP.
Again, if we set those values
to specific numbers,
then we would see those
appropriately set up.
And so you can go in there
and, you know, change
some of these to test it out.
Move our
X.
Oops.
Here we go.
And so we got our our feed beef
in there, so that was
an example of how you could rewrite
some of those functions and
there are a lot of different
functions that need to be rewritten
in order to be used for
64 bit codes.
So if that's something you're interested,
you can work on that.
So today we went through and we
wrote a 64 bit Don't brigadistas
function and learned how
to make sure that we pushed
everything on to the stack
appropriately so
that we could call that printf and
then use the with reference
to for the party.
If you have questions, you can email
me millirems.
You indicated to you and you can
find me on Twitter at Apneas House
30.