Segments and Functions

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 2.3 of Assembly.
00:07
So today we're gonna learn about segments and executing functions
00:12
segments
00:14
so different segments have different meanings inside of the assembler. So we have the dot data section
00:20
and this is used for stringing global variables.
00:23
It's initialized data, and then it's reading, right, so we can read data from it and we can write data to it.
00:29
We also have the BSS. So the BSS is for un initialized variables,
00:35
and it is both reading, right, additionally, but it doesn't take up space. And so you might say, Why do I want one of the others? The data is for something that we
00:44
would care about, and we want to actually give it a value the BSS is gonna be for for buffers. Maybe so. If you were going to read a file from disk,
00:52
you might put that in the BSS and then read the data from the disk instead of wasting space in your binary
00:59
with useless data.
01:00
And then we have the text. And so that is where our code goes. And this is reading execute, meaning that the computer can read it, and then it can run it. But it's not gonna ever change it,
01:11
and that's what your programs should be set up now. People who are attacking different things might change it so that
01:17
the text segment is read, write execute so they can write additional instructions. Or some interpreters might modify their tech segment so that it is
01:26
read, execute and right. So there's a different
01:30
ways that you can change that. But this is generally how our assembly is going to work.
01:38
So in the data section, we can define different pieces of data, so we can, as we talked about before, define a bite, define a word or define a d word,
01:47
and this can take up one bite. This will take up two bites, and this will take up four bites.
01:52
And then for convenience, we can define a whole bunch of bites. Right, So this is a string. So each one of these is gonna be a bite. And so it's a lot easier than trying to figure out what is the ask e or hex value of each one of these. We just type them in, and then the assembler will take care of that
02:10
functions.
02:14
So we're executing a function. We use the demonic call for calling a function,
02:20
and then the operation is a function name that we're going to call.
02:23
So, for example, we could define a string and then put that address in yea X, and then we'll call this function called Print String,
02:30
which prints what is pointed to by E. X. And we got to make sure that we have a null terminated string. So let's go ahead and do that.
02:44
So I've already created a project Gen make here 2.3, went into that directory and already built it just so that everything was ready.
03:00
I'm going to insert mode
03:04
and I'll define a string.
03:12
Now, one of the things that often times my students forget is to put in a zero in here. So this will neural null terminate our string.
03:20
So no means that this has the actual zero byte inside of it.
03:27
And so, as it said, what we're gonna do here
03:30
move into yaks
03:32
a
03:35
call print
03:38
string,
03:39
go ahead and write and quit
03:42
type make. Hopefully, everything worked
03:50
so we can see a printed off hello world. But it didn't print off a new line just slightly knowing.
03:54
So let's go ahead and add a new line in here. So a new line is the character. 10.
04:05
Go ahead and add that
04:08
run make.
04:11
And now we got a new line at the end of our our program.
04:17
All right, so we just executed a function that was called Prince String, given that null terminated string that we had in there.
04:26
So inside of the
04:28
template, we have a bunch of the functions that have been created that we can easily use. So we have Read INT, which reads an integer from the user
04:36
and then stores that result in the X. We have print INTs, which will printed Manager. That's in E X.
04:44
We have print and now, which will print Ah, a new line. And then we have read characters. This reads a single character from the user
04:50
and stores it in the X, and it's actually in the lowest half. So ale of that register.
04:58
We also have print string, which we saw in the previous slide.
05:04
So we're gonna do a little example here, so we're gonna go ahead and print a prompt for the user to enter two numbers.
05:11
Well, read those in and then we will add them together and go ahead and print the result.
05:20
So I'll go ahead and use the same program that I had before.
05:35
I'm gonna take off the new in here because I think it looks better this way.
05:44
So I'll go ahead and move my prompt in t a X
05:48
Culprits string
05:50
and then I'll want to read that number from the user
05:56
so you can look in the slot in the previous part of the lecture.
06:00
But we're gonna go ahead and call, read
06:02
int.
06:04
So now the result is in the X, so I'm gonna go ahead and move that to EBX.
06:11
So I'm just copying the data that's in E. X into ebx.
06:16
Now, once again, I will move the X prompt
06:23
call print
06:25
string,
06:27
read another number,
06:31
and so I have one of my numbers in E b X. And after this, I should have the other one inside of yea X.
06:39
So now I can go ahead and say Add e x e v X,
06:46
so that will
06:46
add the value that's in the E X to the value. That's an E B X and save the results into E. X.
06:54
And now I can call my
06:56
print in function,
06:57
which will print whatever is in the X
07:01
and I'll add a new line at the end.
07:10
Go ahead and make.
07:12
I didn't mess up.
07:15
So let's pick two numbers. Let's say
07:18
98
07:20
and
07:23
12.
07:26
So our result got printed on 110
07:29
right? And we could have done a couple other things to make it maybe a little bit nicer. So,
07:33
for example, let's suppose we wanted to print an additional prompt
07:38
saying that the result And so I'm gonna have to save yea X after this reading, so I might have to do movie see ex.
07:46
Yeah, so save my results And I have
07:49
number one
07:54
in the Vieques
07:57
number two in
08:00
C X.
08:09
So let's let's do our result.
08:20
If you are not an insert menu type, exit will delete characters.
08:28
Oh,
08:33
okay.
08:35
So move my
08:41
print off my string.
08:46
And now down here, I needed to
08:50
move into the x e c x. So now I have moved E c X into E X and then added the two together and then called my
08:58
print in printing line.
09:03
See if we messed up?
09:05
No.
09:07
So I had 98.
09:11
12.
09:13
My results is 110.
09:20
So in summary, we talked about segments. We talked about executing functions. And then I gave an example of how we can execute functions to solve a very simple mathematical problem. I'm just reading two numbers from the user and then adding the results together.
09:37
So in future lectures, we're going to talk about extending numbers. So extending the sign on signed numbers that are stored in two's complement
09:46
and then doing multiplication and doing division.
09:50
If you have questions, you can contact me at Miller MJ at you and Kate I e. To you,
09:54
and you can find me on Twitter at Milhouse 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