Inline Assembly Example
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:01
Hello. This is Dr Miller, and this is Episode 14.11 of Assembly.
00:07
Today. We're going to given in line assembly example.
00:10
So in line assemblies Example.
00:13
So our problem to be solved is that we need to capture the time we're going to use the time stamp counter.
00:18
And so we're gonna use the RD TSC Assembly Command, and this command store is the result in E, d, X and E X.
00:26
And so we're going to save that to a Q word, and then we're gonna print the result. And so what we're gonna do is we're actually gonna write to programs today,
00:35
So the 1st 1 is our assembly version of the code.
00:38
So in that
00:39
we're going to just give an example of how to do all of this. So well,
00:44
I use a Q word, and that's for our result.
00:50
You've got our format
00:54
we're going to use, so this is a long
00:58
unsigned, so we're gonna use
01:00
that format, so l l u
01:06
we're also going to use print half
01:10
so down here, we're going to call the R D T S C
01:17
command, and again that's going to save it in e D, X and V X.
01:21
And so here I'm going to move into bbx our
01:26
address of our where we want to store the data.
01:30
And then we're gonna move into that location and ebx the value of V A X. It's that has the bottom half ad for
01:40
TVX because it's eight bites and then move into location ebx
01:46
e d. X. So that's our second register.
01:49
And now we got to set up this stuff for print F.
01:52
So we're gonna move into yea x A plus
01:57
four.
02:00
Push the X and then move into e x
02:05
A. It's reloading the data from our variable a
02:08
pushing those onto the stack so that we can call print F move
02:15
format,
02:15
push the format
02:19
and then we'll go ahead and call
02:22
print F.
02:23
And then we have to correct our stacks. Add yes, p
02:28
12.
02:30
So this should
02:31
give us what we want.
02:38
All right, so it looks like we have an air. Let's see if we can find that
02:45
I forgot to put
02:46
ex turn in here.
02:50
All right, so let's rebuild that.
02:58
All right, so we can see that we get the time.
03:01
So now let's look at how we would do this in C.
03:08
So some of it's going to be easier. Some will be more difficult.
03:15
So we'll include the standard Io library.
03:20
So we can do print off
03:23
the standard live directory
03:28
for some normal functions, and we have our end
03:31
main,
03:38
and then we have to declare our variables. So we're gonna use unsigned
03:43
long, long to do our cue word.
03:46
Go ahead and set time 20
03:51
and then we're going to,
03:53
um,
03:53
do our assembly. So I'm gonna use sm
03:58
volatile.
04:00
And then this is where we're going to put our code.
04:03
Have a couple more pieces here. So go ahead and get those set up.
04:09
Um, and then we got the end of our code there.
04:12
All right, so this is gonna be
04:14
output
04:16
input
04:20
clubbers.
04:23
All right? So to emulate what we did,
04:25
we're going to do r d t C
04:30
again. We got to put new lines in here if we're going to use them,
04:33
and then we'll make another line
04:39
putting quotes so that we make this a little bit readable
04:44
for what we're doing.
04:46
So we're gonna end up with our output is going to be
04:49
um
04:51
we're going to use a memory just for this.
04:55
And the variable is going to be time. So it's our time, Variable, We're not gonna have any input and we'll figure out what our clubbers are.
05:03
So we should know that from the RTs see, it's gonna use yea x
05:10
and you d x
05:12
so we know that all clobber those
05:14
and I'm gonna end up using some or in here.
05:16
So first of all, because its memory address, what I'd like to do is I'd like to load that memory address
05:23
into a register, So I'm actually going to use e b x toe load in
05:28
my output, which is percent zero again. Put a new line in here.
05:32
I think it'll be easy to
05:35
Ah, yank and copy. So, Yankee, why for Yank p for put metal copy online and put it.
05:43
And so now we need to save our values. So we're gonna go ahead and do move
05:46
into the value pointed to by EBX, right? Because we got an address for it here.
05:51
And the reason we have to use Elia's cause by default, right time is gonna be put on the stack
05:59
and so we want to load that memory address in.
06:01
So go ahead and save the X there.
06:05
And then we can go down here and
06:12
copy again. So let's add for TB X like we did in our main one.
06:17
So the ad be x
06:23
four
06:26
and then again, we'll have to save our data,
06:29
so move into the value pointed to by the new part of ebx
06:34
VD X rights. That's the top half of our register that we have.
06:39
And so now we have gone ahead and modified ebx. And so you want to tell it that by the way, EBX
06:45
has been Colomb Erred has been destroyed.
06:49
Then at the end, we're gonna print off our time and we're just use regular print f
06:58
what time is, and then use my same format.
07:02
Got a nice new line in there and we get it.
07:11
Okay,
07:14
so now we should be ready to build that. So,
07:20
no,
07:23
let's get it here. So we want to do minus M 32. So for 32 bit
07:29
on minus man as, um equals Intel to say that we're using Intel syntax
07:34
we're gonna do may not see.
07:38
Looks like I spelled
07:40
vole a tall wrong.
07:45
There we go.
07:50
Oh, and then I forgot my argument for my print up here.
07:54
All right, so we got to put our time
07:58
and there
08:03
and by default think it's created in a doubt out. So I'm gonna run my two side by side,
08:09
and so I'll do love and, uh
08:16
0.11
08:24
and I didn't quite get him lined up, but we can see that they are pretty close to each other,
08:28
right? So one of the things with the RTs r d t s c. Is that it's counting clock cycles from the very beginning. So if your clock is running at four billion cycles a second, you get a somewhat accurate measurement of time,
08:43
depending on a lot of different things that have changed with
08:46
see, if you use going up and down in speed and
08:50
multi processors sort of make that interesting.
08:54
But we can see that there are some benefits to each one of these. So, for example,
08:58
if I look at
09:01
my assembly code, it's probably a lot easier to understand how to load this memory address in and then do this right. So the
09:09
the actual time stamp was a lot easier to do in here,
09:13
But then when we go to print off, so this might be a lot more work
09:16
then if we go back to our
09:20
main program and look
09:22
well, if I want to call print f right, I can very easily do that in one line. And it's a lot simpler,
09:28
um, than the in line assembly. And so the in line assembly.
09:31
It is somewhat difficult because that, well, these new lines in and we have to
09:37
make sure that
09:39
we figure out what we're collaborating, what we're doing with this and that. And so each one of them has its own benefits in its own drawbacks. And so you just have to choose the correct one
09:48
for how you are programming.
09:54
So today we saw on in line assembly example and looking forward, we're gonna do some or combination of assembly and see together and maybe a little bit more easier
10:03
a manner. So if you have questions, you can email me Miller M J u N K I e t you and you can find me on Twitter at Melas 30
Up Next
Similar Content