Computer Architecture Part 3: Control Flow and Stack
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:00
Okay, so now that we've looked at our basic assembly instructions in this session, we're going to examine control flow and the stack. Now, as we use high level languages, we tend to write programs based on a number of different behaviors, conditions or states. Now, as an example, we can programmatically print out
00:19
a message
00:20
any given number of times based on a counter
00:23
in a high level language. Our program flow. This is controlled by, if then else, and different looping statements otherwise known as branching.
00:33
Now an assembly, we use these instructions to transfer control of execution to end from different memory addresses.
00:41
Now the most common branching instruction we've seen assembly is the J. C. C. Family of instructions, otherwise known as jump instructions.
00:50
Jump instructions. They come in two flavors. Unconditional, where a jump is just simply taken and conditional, where there's some type of condition that needs to be met before the jump is taken
01:03
with an unconditional jump indicated by the JMP pneumonic in our image, the jump instruction will literally jumped the address held in the EA X operandi. Remember, this is known as a director dress. We can also use the jump instruction to branch to an indirect memory address. This is one at a specific memory location
01:23
that's either going to be set, retrieved and or calculated somewhere prior to the jump instruction.
01:27
Now, when we use the jump instruction, this has an effect on the E I P register. When a jump occurs, it sets the instruction pointer register to the next instruction address. We're gonna take a look at this when we talk about the program stack in a few minutes,
01:42
similar to the jump instruction. There are some other ones that modify the execution of our programas well, and these air called the call and read instructions shown on the right.
01:53
So when I call happens our program calls or branches to the instructions that are listed in the call instructions operandi. In this case, it's 402000
02:04
At the same time, the computer will keep track of the instruction it left off at before it took the branch. In our case, this is 401005
02:14
So now that we've branched at the address of hex 402000
02:19
the move instruction. This is going to set the EA X registered to beef YB and then execute the right instruction,
02:25
the right instruction. This is going to get the return address and set the instruction pointer to 41005 and then the program execution will continue.
02:37
Conditional jumps are ones which are taken based on some condition.
02:40
They're typically displayed with some combination of a compare instruction
02:45
or a logical test utilizing the test instruction. This isn't always the case, however, as the comparisons can happen with any bit wiser arithmetic operation. However, we do get these operations bundle with certain instructions. For instance, the compare instruction
03:02
implements a subtracting operation without storing the difference in the destination,
03:07
and the test instruction performs a bit wise ad
03:12
Now. What these instructions do implement, however, is a change in a specific bit of the E flag register. Based on the results. Now, we haven't really discussed the flags register, but for now, all you really need to know is that it's a register with 32 individual bit positions
03:30
that air used to keep track of bullying values
03:32
and because our jump instruction this doesn't store the result in an operandi. It uses a bit of the flag register as a switch as to whether or not to take the jump. And so here in our assembly code, we've got some jumps, the first line in our assembly. This compares the value in R B P minus four
03:52
20
03:53
The next line, which is J any says that if thesis comparison isn't true, that's what it stands for. Jump not equal j any, then jump to line 402000 and the execution continues by moving one into the memory address our VP minus. For now, we should really take a second to think about what this means
04:13
during our explanation of the code. We use the word if and we also use the word then
04:18
and one last note. We don't know what the value of r B P minus forests, but we do know that if it's not equal, we jump in, the execution continues. So what we can probably say is that this is comparing some type of variable in an if else statement. So, using the assumptions, we can craft a bit of pseudo code that has RFL statement
04:38
something like if X is equal to zero,
04:41
then move the value of five into X, making X equal to five.
04:46
If it's not, then we jump to address 402000 and set X equal to one.
04:54
Now, before we move on on the right here, we've got some comment. Jump instructions. I just want to briefly talk about them and tell you what the flags a register is set. So the first is jump of zero or jump. If equal. This is that if the result of the comparison is equal to zero, the execution will continue
05:11
and the ZF flag register is set toe one,
05:14
now similar to above the J and Z instruction is the opposite. If the result of the comparison is not zero or not equal, the execution will continue and the ZF flag is set to zero.
05:26
The last two instructions are less and greater than comparisons.
05:30
If the result of the comparison of the jail instruction is less, the execution continues and the SF flag is set to one.
05:39
If the result of the comparison is greater than in RG instruction than execution continues and the Z F and SF flags are set to zero
05:48
another control structure that allows us to jump back and forth between code is loops. So loops execute code until some condition is met. There are several types of loops, but the most common ones are the four in wild loops.
06:04
So, up to this point, we've seen how jumps move us forward in our code. However, loops allow us to jump backwards
06:13
in our image. We have a wild loop
06:15
now with Wild Loop has a general form. This consists of four parts Thean Initialization. This is setting our editor to zero
06:25
the wild condition. This is going to evaluate the condition while I is less than five. We've got the code now in this code, we really don't do anything but looping through the counter. And lastly, we have an update statement that is incremental. The variable we've set in our loop by one.
06:44
Now, from an assembly perspective, we can see that at first we set our initialization variable by moving zero into our VP minus four.
06:53
Then we use a jump to branch to our loop test label and do the comparison
06:58
Now. This is a bit like in many if like if the variable in R B P minus four is less than four, then jumped to the increment label.
07:08
Now the increment label. This adds one toe are variable. This is our looping counter.
07:13
Once this happens, we can now continue to the next line in our execution and move to the compare instruction.
07:20
This flow of the process execution continues until Iess five in the program exits.
07:27
Okay, so let's switch gears for a second and talk about some memory structures. So as we know when a program is stored on disk, it's divided into several sections, either of which contained coder data. The code sections contain instructions to be executed by the processor, whereas the data sections contain variables. Resource is thean port table and so on,
07:46
and when we execute a program, the process is loaded into an allocated memory space by the OS. In our simplified image here, you can see that the structure of the XY is loaded into memory, similar to how it's stored on the disk.
08:01
Also, as a process is loaded into memory, part of the process context contains a stack and a heap which are also loaded into memory. Thes air space is used to dynamically allocate memory, two variables and the like, and we won't be concerned too much with the heap at this time. But let's talk about the program stack.
08:20
A critical component of our programs are functions. Functions contain a block or blocks of code that performs a specific set of tasks. Typically, a program contains many different functions, and when a function is called, the CPU transfers control to a specific memory address which contains the code which is to be executed.
08:37
Control is transferred back to the caller
08:41
once execution completes.
08:43
The called function has different components, such as parameters code within the body of the function in variables, all of which are used within the context of the function. To facilitate the movement of data in and out of the memory allocated for our functions, we use the stack so the stack is a memory space where data is temporarily stored.
09:01
This could be anywhere in our main memory.
09:05
To use the stack, we add and remove data to and from it using the last in first out method. However, we can read any of the data that's contained in the stack at any memory address at any time to illustrate stack use. Just think of the stack as a group of cells called stack frame, and every time a variable in a function requires memory allocation,
09:24
you add a new four bites cell containing the value to the top of the stack.
09:28
This is called pushing a value to the stack, and it's implemented using the push instruction.
09:35
For example, let's say we want to allocate memory for the value three to the top of the stack and assembly. The push instruction is used to place the value on the top of the stack. As a result of this push, the stack will grow in size. When we push values, the stack grows down towards lower memory addresses.
09:54
Now we can put additional values on the top of the stack. For example, we can push four and then four will be added to the top of the stack.
10:01
Now, to get data off the top of the stack, we need to pull it using the pop instruction. As a result from this operation, the stack is going to decrease in size. So to keep track of where the stack begins, I e. The top of the stack. We used the stack pointer register
10:18
stack pointer register will increase or decrease in size as we push and pop data off the stack.
10:24
All right, so I hope that you enjoyed our assembly review. We didn't get to all the topics. But don't worry, because we're going to be filtering them in as we do our static analysis. So let's go ahead and wrap up this module with a brief summary.
Up Next
Instructed By
Similar Content