Hello, this is Dr. Miller and this
is Episode fourteen point eleven of
Assembly.
Today, we're going to give an
in-line assembly example,
so inline assembly, for example,
so our problem to be solved is we
need to capture the time and we're
going to use the time stamp counter.
And so we're going to use the TSC
Assembly command and this command
store as a result in Edison
X.
And so we're going to save that to
a keyword and then we're going to
print the result.
And so what we're going to do is
we're actually going to write two
programs today.
So the first one is our assembly
version of the code.
So in that we are going to
just give an example of how to do
all of this. So we'll
use a key word, and
that's for our result.
We've got our format
and we're going to use.
So this is a long,
unsigned.
And so we're going to use
that format.
So allow you
we're also going to use print F
and so down here,
we're going to call the key
is C
command. And again, that's going to
save it in Eidson Yak's.
And so here I'm going to move into
SBX, our
address of our
where we want to store the data
and then we're going to move into
that location at SBX,
the value of X.
And so that has the bottom half add
for
tiebacks because it's eight
bytes and then move into
location SBX
idex.
So that's our second register.
And now we've got to set up the
stuff for our print f
so we're going to move into yak's
A plus
for.
First X and
then move into X
A, right, so we're loading the data
from our variable A,
pushing those on to the stack so
that we can call print F mov
format, push
the format
and then we'll go ahead and call
print F
and then we have to correct our
stacks. Ad is P
twelve.
So this should
give us what we want.
All right, so it looks like we have
an air. Let's see if we can find
that.
I forgot to put
extern in here.
All right, so let's rebuild
that.
All right, so we can see that we get
the time,
so now let's look at how we would do
this in C.
So some of it's going to be easier,
some will be more difficult.
So we'll include the standard
library so
we can do print of
the standard
lib directory
for some normal functions.
And we have our int
main.
And then we have to declare our
variables, so we're going to use
unsigned,
long, long to do our keyword
and set time to zero
and then we are going to
do our assembly.
So many is HSM
volatile?
And then this is where we're going
to put our code.
We have a couple more pieces
here, so go ahead and get those set
up
and then we got the end of our code
there.
All right. So this is going to be
output
input
clobbers.
All right. So to emulate what we
did, we're going to
do d, t,
c again.
We've got to put new lines in here
if we're going to use them
and then we'll make another line,
put in quotes.
We make this a little bit readable
for what we're doing.
So we're going to end up with our
output is going to be
we're going to use a memory just for
this and the variable
is going to be time.
So that's our time variable.
We're not going to have any input
and we'll figure out what our
clobbers are.
So we should know that from the RTC
it's going to use X
and X,
so we know that it'll clobber those
and then I'm going to end up using
some more in here.
So first of all, because it's a
memory address, what I'd like to do
is I'd like to load that memory
address into a register.
So I'm actually going to use SBX
to load in my
output, which is zero.
Again, put a new line in here
and it'll be easy to
yank and copy.
So yanking Y y for Yank
P four, put little copy on line
and put it.
And so now we need to save our
values. So we're going to go ahead
and do move into
the value pointed to by SBX.
Right, because we got an address for
it here.
And the reason we have to use is
because by default,
right. Time is going to be put on
the stack.
And so we want to load that
memory address in.
So we'll go ahead and say the X
there and then we can
go down here and.
Copy again, so let's add
four to ABC's like we did
in our main one.
So if you add SBX.
For.
And then again, we'll have to save
our data,
some move into the value pointing
to by the new part of Ebix.
So that's the top half of our
register that we have.
And so now we have gone ahead and
modified SBX.
And so we want to tell it that, by
the way, Ebix
has been clobbered,
it's been destroyed.
And then at the end, we're going to
print off our time and we're just
going to use regular print F.
So time is
and then he's my same format.
What a nice new line in there
and we get it.
OK.
So now we should be ready to build
that so.
You know, I get
it here.
So we want to do minus M
30, too, so for 30 to bit
and minus marzen
equals Intel to
say that we're using intel syntax
and we're going to do mean that.
C looks
like I spelled Voll
Attal wrong.
Here we go.
Oh, and then I forgot my argument
for my friend up here.
All right, so we've got to put our
time
in there.
And by default, it's created an
eight out, so I'm going to run my
two side by side
and so I'll do a lot and.
That 11.
And I didn't quite get them lined
up, but we can see that they are
pretty close to each other, right?
So one of the things that the
artists are the
TSC is that it's counting clock
cycles from the very beginning.
So if your clock is running
at four billion cycles, the second
you get a somewhat accurate
measurement of time,
depending on a lot of different
things that have changed with
CPU's going up and down and speed
and multiprocessor
sort of make that interesting.
But we can see that there are some
benefits to each one of these.
So, for example.
If I look at
my assembly code, it's
probably a lot easier to understand
how to load this memory address in
and then do this.
So the the actual time
stamp was a lot easier to do in
here.
But then when we go to print F,
so this might be a lot more work.
And if we go back to our
main program and look well,
if I want to call print F right.
I can very easily do that in one
line and it's a lot simpler
than the inline assembly.
And so the inline assembly
is somewhat difficult because that
will these new lines in and we
have to make
sure that we
figure out what we're clobbering,
what we're doing with this and that.
And so each one of them has its own
benefits and its own drawbacks.
And so you just have to choose the
correct one for how you
are programing.
So today, we saw an inline assembly
example and looking forward,
we're going to do some more
combination of assembly and sea
together and maybe a little bit more
easier manner.
So if you have questions, you can
email me millirems at Kincaide
Edu and you can find me on Twitter
at my house 30.