- Tubelator AI
- >
- Videos
- >
- People & Blogs
- >
- Exploring Assembly Language Functions and Stacks: Key Concepts (Smruti Sarangi)
Exploring Assembly Language Functions and Stacks: Key Concepts (Smruti Sarangi)
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.
Instantly generate YouTube summary, transcript and subtitles!
Install Tubelator On ChromeVideo Summary & Chapters
No chapters for this video generated yet.
Video Transcript
Now, it's time for us to look at some advanced concepts, namely functions and stacks.
So what we have discussed up till now will take us till a certain point, but it will
not allow us to write fairly difficult or complicated assembly programs.
As a result, it is necessary to introduce a couple of slightly more advanced and complicated
concepts.
important of that being functions. And to support functions, we need a software structure
called a stack. So, what is a function? So, let us consider a C program. A function is
essentially a block of code that achieves a certain specific task. So, we give a function
a set of arguments and then, for example, if there is a function to find if a number
is a prime number or not, then the number is an argument to this function and the result,
a Boolean result of whether the number is prime or not is what the function returns.
So similarly we should be able to write functions in assembly language as well, similar to a
high level programming language.
So, if we think about how functions are, will actually be represented in memory, it is something
like…
like this as shown here, that it is a, we take a region of memory, put in all the instructions
of the function here. So I will just write ints to be short. So all the instructions
we put them in a continuous region of memory and this region of memory which starts at
this point, the beginning, the first byte, so this is the beginning of the function and
say any function. So foo traditionally is the name of a function when we give an example.
So in this case, this continuous region of memory stores all the instructions or the
function. The data is stored somewhere else, we will come to that. And any function will
be identified by the address, the starting address or the first instruction. So to call
a function, what do we need to do? So let us just go back to the previous slide. If
the starting address is A, we set the program count...
to A. So this is equivalent to jumping to the first address of the function. So
once we do that, we can start executing the function and after the function
completes, we need to store the location of the program counter at which we need
to return to. So we consider a traditional execution of a program. We
keep on executing. We go and execute some function. We execute the function and we
come back. And then we start at exactly the same point. So we need to store the location
of the point at which we return from the function. This is known as the return address. So this
also needs to be stored. So essentially what do we need to store? We need to store two
things. We need to know what is the starting address of the function which is a in this
example and then we need to jump back to a point.
which is the next instruction after the function call.
And this is known as the return address.
So let's take a look at the notion of the return address once more. So let's assume that we have a caller.
So caller is the function that invokes another function.
So the caller calls a function and so we have a function called instruction.
And let the address of this the call instruction be p.
So then we call a function which basically means that we jump to the beginning of the function.
We execute all of its instructions. Then we have the ret instruction which is the return instruction
where we again come back and we come back to the instruction which is just after the function call.
In simple risk sensing,
we assume that each instruction is 4 bytes, we start at address p and we return back to
address p plus 4. This is the PC, the program counter of the return address. And so let
me just maybe expand this example with some more details. So let us assume that the starting
address of this instruction is a. So the call function is pretty much will change the value
of the PC equal to A, we'll start executing this instruction. The return address is P