Hello, this is Dr. Miller and this
is Episode fourteen point ten of
Assembly.
Today, we're going to talk about
inline assembly, adding extra
arguments and then an inline
function call, so
inline assembly,
so the Ganu Compiler Collection
or GCSE has the ability
for us to generate
different types of code.
And JTC uses
AT&T syntax for both
its X eighty six and X 64 code.
But there are a lot of different
tools that will use standard intel
syntax.
So that includes Eita Binary, Ninja
Gaja, Nazeem and Mazloum.
And this is all because most of
those stemmed from Mazloum,
which uses Intel's syntax.
And so that's why in this class,
we have talked about using Intel
syntax for X Eighty-six code,
so in order to allow us to do that
type of syntax, there are a couple
of different ways that we can do
this.
So the first way is that we can
basically state that we are going to
use intel syntax inside
of our inline assembly.
And so to do inline assembly, we
use ASRM, put our princesse
and then we put quotes for the code
that we want to run.
And so here we've got a different
RASM statement for each thing that
we're going to do. So for example,
here we move zero into X
without the address of VAR
into X, we
think we can then move one into
the value pointed to by E,
E, X.
And then here we switch back to
AT&T Syntex.
And this is if you don't do this,
the compiler will have trouble
with the rest of the code generated
because it's all AT&T
syntax.
And so this is one way where
we can write some inline assembly
code within our C program,
but we can't have extra arguments.
So there are complex arguments
we can use for this.
And so we have our assembly code,
which we just saw, and this is
separated by new lines, but we only
did one in each line, so it wasn't a
big deal.
We then have a list of output
variables. So these are variables
that are modified and that's
variables inside the C program.
We have input variables.
So these are inputs to ours and
the compiler will set them up for
us.
And then we have the clobbers or the
list of registers that are modified.
So we also have this complex
variable substitution so we can have
variables inside of our assembly
code and those
are denoted by percent,
zero percent, one, two,
three, whatever, up to n however
variables that we have.
But it's important to note that the
output arguments are first and
so all of the outputs will be listed
giving their numbers and then the
inputs are given.
And so, for example, if we have two
outputs and three inputs,
the outputs will be zero
percent. One, those are the outputs
and then the inputs are percent, two
percent, three percent, four for the
three inputs that we would have.
Then additionally, we can have
different constraints.
And I have not listed all of them.
But the two that you might end up
using are four register and
M for memory.
And then we also have modifiers
so equals means that we might
be overwriting and then plus
is for reading and writing
and all other outputs
or operands assumed to be at least
read. And so if you're reading in a
variable as an input, then
it's assumed that that would be
read.
Additionally, we can use the
volatile keyword in our
assembly statement to make sure that
the assembly code is not
moved or changed by the
compiler, and this might be due to
different optimizations
based on loops that might have
unused variables, they might be
moved out or security checks.
So, for example, if you want to put
in an assertion that something
is not changed,
the compiler might remove that.
But it's a security check of
something that might is not supposed
to happen but may happen.
So here we can see a sort of more
complex example.
So first of all, we have the
volatile keyword and
then we have that we are going to do
are moving
in and such.
Now, one of the things here is when
we compile this, we know we use
minus Mazloum equals Intel.
So that way all the code is
generated. So we don't have to put
that until Syntex in our inline
assembly.
So here we are moving into X
percent one, and so
that is going to be our
input, we then
move the number ninety nine into X
and then we move
into the value pointed to
by our output.
So that's variable B and
so we can see that it will modify
variable B
to be the value of variable
A plus ninety nine
or the value is the value variable
A plus ninety nine.
And so we
can see that that is modified and
then we have our list of clobbers.
So the clobbers is the variable
that we have modified.
So in here we just modified X
and so we just tell the compiler by
the way, we're going to modify
X inside this code.
And so when we look inside of here,
we can see that if we forget
our clobbers, so in this example
here, we didn't put our clobbers
and we can see that we
are loading this into yak's
and then we're basically doing a lot
of math with yak's.
Well, the assembler slash compiler
doesn't know that we're modifying
Yak's inside of there.
And so it's actually modifying IEX
to be the other value of vacs.
And so it doesn't work.
And so if we add in the fact that
we're going to Klauber X, we
can see that then it used Eddys
X in order to store that variable.
So it's moving that variable into X.
And then everywhere that we
reference that variable,
we all get the right result.
And so you have to make sure if
you're modifying a register and
you're clobbering it, that you put
that in the list of clobbers
registers. Otherwise it's not going
to work.
So we can't have multiple inputs, so
we can have variables and variables
be right, and so those are going to
get moved into yak's and SBX,
we go ahead and add them together or
the result and see and
then we can use that.
And again, we have to compile with a
minus Mazloum equals.
And until.
And here we can see that example
code with the multiple inputs
and we'll see that they get loaded
into different registers.
It is a little bit hard to read
because you get one line of assembly
code that ends up being put
in the disassembly
view. And so you have to make sure
you carefully read it because each
line in here is not going to
correspond to the lines in here.
But we can see that it did load our
two inputs into registers, MDX
and ESX and then did
all of our moves.
And so it replaced them
appropriately and did the
setup for it so that our variables
get the right values
inside of them.
So a function pointer, Celeste.
One, we talked about function
pointers, and so here we're going to
give an example using that,
and so we have a function called
Print A, and so if we want
to go and go ahead and call that
function, right.
So here in assembly, we are
pushing the number 20.
We're moving our register,
our input,
which is the value of the
print function.
So that's going to be the address of
the print function.
So that's going to get moved
into yak's.
We can then call Yak's and then
this pop will erase the
return value of that,
but it'll fix the stack because we
did push one item on to
the stack for that function.
And so here we can see the
disassembly output of that
and so we can see that in the end
our disassembly
is going to pretty much mirror
the disassembly of calling the
normal function,
other than the fact that they are
lining the stack on 12
or 16 bye bye bye boundaries.
By doing this, subtract by 12
and then the correction by 16
in order to keep those
boundaries the way that they want
them.
And then we can do sort of a more
complicated one where we have
our clobbers, we were saving it to
a variable rate, so we
have VAR, which is going to get our
result.
So we call our function, which is an
X, and then we
go ahead and store that result in
zero, which is our output.
And so the value is going to get
saved inva.
So we're able to call our function,
modify the variable var
for our result and then
be able to exit the function
appropriately.
And so again, it's just making
sure you understand how the syntax
works of where the percents go and
what they relate to.
As far as if it's argument zero.
Argument one.
So today we talked about in line
assembly, we talked about adding
some extra arguments to those in
line assembly calls, and then we
looked at an inline function call
for assembly.
In the future, we're going to do an
inline example and then we're going
to look at external assembly
code.
So you have questions.
You can email me millirems, you,
Ankita, Edu, and you can find me on
Twitter at inless 30.