fbpx
  1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. People & Blogs
  6. >
  7. Exploring Assembly Language Functions and Stacks: Key Concepts (Smruti Sarangi)

Exploring Assembly Language Functions and Stacks: Key Concepts (Smruti Sarangi)

Available In Following Subtitles
English
Variant 1
Posted on:
Dive into advanced concepts of functions and stacks in assembly language programming. Learn how functions achieve specific tasks and the importance of software structure like a stack for supporting functions. Discover how functions are defined and represented in memory for writing complex assembly programs.
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:34
Now, it's time for us to look at some advanced concepts, namely functions and stacks.
0:41
So what we have discussed up till now will take us till a certain point, but it will
0:46
not allow us to write fairly difficult or complicated assembly programs.
0:51
As a result, it is necessary to introduce a couple of slightly more advanced and complicated
0:58
concepts.
1:00
important of that being functions. And to support functions, we need a software structure
1:06
called a stack. So, what is a function? So, let us consider a C program. A function is
1:14
essentially a block of code that achieves a certain specific task. So, we give a function
1:22
a set of arguments and then, for example, if there is a function to find if a number
1:27
is a prime number or not, then the number is an argument to this function and the result,
1:36
a Boolean result of whether the number is prime or not is what the function returns.
1:42
So similarly we should be able to write functions in assembly language as well, similar to a
1:48
high level programming language.
1:50
So, if we think about how functions are, will actually be represented in memory, it is something
2:00
like…
2:00
like this as shown here, that it is a, we take a region of memory, put in all the instructions
2:09
of the function here. So I will just write ints to be short. So all the instructions
2:17
we put them in a continuous region of memory and this region of memory which starts at
2:22
this point, the beginning, the first byte, so this is the beginning of the function and
2:27
say any function. So foo traditionally is the name of a function when we give an example.
2:34
So in this case, this continuous region of memory stores all the instructions or the
2:40
function. The data is stored somewhere else, we will come to that. And any function will
2:46
be identified by the address, the starting address or the first instruction. So to call
2:53
a function, what do we need to do? So let us just go back to the previous slide. If
2:57
the starting address is A, we set the program count...
3:00
to A. So this is equivalent to jumping to the first address of the function. So
3:09
once we do that, we can start executing the function and after the function
3:13
completes, we need to store the location of the program counter at which we need
3:20
to return to. So we consider a traditional execution of a program. We
3:24
keep on executing. We go and execute some function. We execute the function and we
3:29
come back. And then we start at exactly the same point. So we need to store the location
3:37
of the point at which we return from the function. This is known as the return address. So this
3:45
also needs to be stored. So essentially what do we need to store? We need to store two
3:49
things. We need to know what is the starting address of the function which is a in this
3:55
example and then we need to jump back to a point.
4:00
which is the next instruction after the function call.
4:03
And this is known as the return address.
4:14
So let's take a look at the notion of the return address once more. So let's assume that we have a caller.
4:21
So caller is the function that invokes another function.
4:26
So the caller calls a function and so we have a function called instruction.
4:31
And let the address of this the call instruction be p.
4:36
So then we call a function which basically means that we jump to the beginning of the function.
4:43
We execute all of its instructions. Then we have the ret instruction which is the return instruction
4:51
where we again come back and we come back to the instruction which is just after the function call.
4:59
In simple risk sensing,
5:00
we assume that each instruction is 4 bytes, we start at address p and we return back to
5:06
address p plus 4. This is the PC, the program counter of the return address. And so let
5:15
me just maybe expand this example with some more details. So let us assume that the starting
5:22
address of this instruction is a. So the call function is pretty much will change the value
5:28
of the PC equal to A, we'll start executing this instruction. The return address is P
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome