Assembly with C

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
or

Already have an account? Sign In »

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:02
Hello, This is Dr Miller and this is Episode 14.12 of Assembly.
00:07
Today we're going to use assembly and see together.
00:12
So for this we're going to use GCC and Nazem and so Nazem has the ability to generate object files for us
00:20
and then inside of our Nazem code, we're going to declare a function and we'll make it global, which means it will be exported and usable
00:27
within GCC. And then GCC can take his many object files as we have
00:32
and it will create an output file.
00:34
And so when we give a list of them, the last one is the one that has to have the main method in it.
00:39
And then we have to also include a function declaration. And so this will allow us to understand,
00:46
um, what function exists even though it may not exist in the file that we're using.
00:52
So when we get ready to compile, we use the Nazem minus f and so we're doing it on Lenox. So the output format is elf.
01:00
We have our Essam file and then or generate this object file and then with GCC. We include that object file before we include our main file.
01:10
So what's our problem? To be false solved. So what we're gonna do is write a strangling function, and so we're going to call it lends string.
01:17
Um, and we're going to create that in our assembly file, and then we'll have the signature like this. So it'll take a pointer to a character, and then a maximum, and it's gonna return an end, and then we're going to use that function inside of our C file.
01:33
All right,
01:34
so what I'm going to do is we're going to use this assembly file,
01:41
but we're going to change our name. So here the function that we have is Len String.
01:49
No, do it right.
01:53
Okay.
01:53
And so the lens string is going to be our function
01:57
that we're going to create.
02:00
And so down here instead of this, we're going to say Len
02:07
String,
02:08
and so I'm gonna
02:09
remove some of this code here.
02:14
So, for example, my push A and my pop A I'm going to get rid of those.
02:20
All right?
02:22
And so a couple of registers I'm going to use. So I'm going to use the string scanning function,
02:28
So go ahead and we're going to end up using pushing e d I
02:32
and e c x
02:37
eso At the end, I went to restore those So Poppy z x
02:42
bob e d I,
02:44
and we got our leaving a return it And
02:51
Okay,
02:53
so,
02:53
um, we're going to use E c X.
02:57
We're going to use E X for too low this character that we're gonna scan for, So we're going to use the S C A S B to scan for a bite.
03:06
So go ahead and move into the a X
03:08
zero, which is the character the knoll that we're going to search for. Then we're gonna move into E D. I. So we have a pointer as the first argument, and so we'll grab that from E B. P
03:22
plus eight. Remember that our enter set up Ari BP BAE Stack,
03:28
we're going to move into E. C. X. So our maximum or the maximum, that we're going to scan
03:32
UBP plus zero c.
03:39
We'll clear the direction flags that we can use. This
03:43
I repeat, not equal.
03:46
And so what this will do is it's going to keep going until the equal flag gets set with the command that follows. And so we're going to use S C A S B to scan for a string bite
03:59
so that a bunch of different
04:00
string functions that we can use in order to search for characters because strings were something that were heavily used an assembly. So they added a bunch of commands in there.
04:11
And so when we get done with that, the pointer of e d. I is going to point to the location where that character is, and so we're gonna reload
04:20
into easy X,
04:23
the original location.
04:25
So
04:26
that's going to be E B P plus eight. So that was our pointer that was on the stack.
04:31
We're gonna let him move into E x
04:35
e d I. So we have our
04:38
current pointer.
04:41
We're going to subtract
04:45
yea x from EEC X. So we'll subtract the two pointers e c x being the smaller pointer. Yea x
04:51
being the larger one.
04:54
Um and then we're in a deck. Ament, yea x by one because this is pointing to the character. And so the length is gonna be that number minus one. And so we have to subtract one from it,
05:03
and then we have our epilogue of our function.
05:09
So now we're going to create our
05:11
see file,
05:17
and so
05:19
we'll call this main dot c
05:21
So we're gonna do our standard includes
05:30
so standard input,
05:36
Standard live
05:42
and the string library.
05:46
And then we're our function is called Len String.
05:51
It takes a character pointer, see?
05:55
And a hint, Max,
05:58
we're just telling the compiler. But by the way, we have a function.
06:01
Here is our main
06:08
Harold. Define our size for what we're going to do. I'm gonna use Malik in order to allocate myself a bumper buffer
06:19
here, I'll do a cast just to be safe. This is a char pointer
06:29
and we're allocate that many bites
06:31
and then again and see, we're using scan F.
06:36
I use the percent s to search for a string,
06:41
and I'm gonna store that in input.
06:46
And then I'm gonna compare,
06:48
um, are two strings So and Len physical twos
06:54
stir. Len, this is the standard call
07:00
and then
07:01
won't get our call.
07:04
Ours is called Len String,
07:12
and then I'm gonna give it the value of size so it has a maximum. That was we ended up using any C X
07:18
and then we'll go ahead and print our result,
07:23
so format it nicely. So I got my first length.
07:29
I'll put my second length there,
07:31
and then we'll put our string,
07:33
put a nice new line in there
07:41
and give us our arguments.
07:46
Okay,
07:47
so now we're gonna want to compile them. So it's a Nazem
07:51
minus f elf.
07:55
And then ours is 14. $12 sn and so called. $14 12 0
08:03
no, I need to give the minus. Oh, come in there.
08:09
And then GCC minus
08:13
and
08:16
32
08:20
14 12 0 and then main off. See,
08:26
it looks like I have, ah, syntax here, here
08:33
in my cast.
08:37
And then let's see
08:41
our print f
08:48
Oh, I forgot the d there, all right.
08:58
And so then it scanned our string, which was hello. And it has five characters in it so printed off the sterling version
09:05
and our version. And so you can see that we can easily,
09:09
um, create a assembly file
09:13
where we basically export our function name, and then we need to just make sure that we use our function arguments appropriately. So
09:24
here I knew I had a knee BP based stack, which is what Lennox typically uses, and so I could use E. V P plus eight and 12. His argument won an argument to
09:33
for that.
09:35
And then we just ended up using a string command that is built into X 86 Assembly.
09:46
So today we saw how we can use assembly and see together
09:50
to use the best of both worlds so that we can write code in whichever language is the easiest for us.
09:58
Looking forward, we're gonna talk about interrupts and Siskel's.
10:01
If you have questions, you can email me Miller, MJ at you and Kate I e. To you, and you can find me on Twitter at Mahal's 30.
Up Next
Assembly

This Assembly language course will provide background and information related to programming in Assembly: the lowest level programming language which is useful in reverse engineering and malware analysis.

Instructed By