Hello, my name is Stephan, and in this lecture we will embark on a journey into the realm of assembly
programming, and our focus will be on understanding conditional jumps and branching.
Crucial concepts.
Low level programming.
And to illustrate this and we will dissect the code that called the jumped ASM.
Actually, we also develop it and I will explain this code line by line.
So here, let's open our CSM project.
The project here.
Uh, we will start from zero.
Uh, let's go to assembler here, create a new file.
Let's jump here.
And save it.
Jump jumping, that is.
And here, let's actually, uh, we will jump jumping that Assam at the comment here.
Jumping at Assam.
Exploring conditional jumps.
Conditional jumps.
And here we will create the extern printf.
This is for declaring the external function.
Uh, declare the external function printf.
And here we will create a section data here with tabs.
Here we will create a number one peak here 42 because this we are here defining a variable number one
with the value of 42.
42.
And here we will also declare.
Define a variable.
Number two with the value of 41 here.
So defining a variable.
Number two with the path, the value.
Value of.
With the value of 41.
And also we'll create the format string for comparison result one.
And we will also write our code for format string for comparison result two here.
So fmt.
If empty one DB here a number.
Number.
Number one here and.
Number two.
So this will recreate your code like this because it's actually prints like equal or greater and equal,
but it actually shows like this.
So this is easier and more understandable to view here.
But if we make this here and as you can see here, these are the separate characters.
This shows this together, uh, to one as of one symbol shows that this is the operand, right?
So logic, operand, operand and here ten and zero.
So here we are.
Format, string.
Format string for comparison.
The result one.
And also we will create the form of two Again.
DB here number one is less than number, one less.
And, uh, number two.
Number two.
Here again, we.
This is for formatting.
Format String.
For comparison.
Result two.
And here we start with the basics.
We are declaring the printf function that will help us display messages later.
And in this data section we define two variables.
Number one and number two which and format strings.
Also Fmt one.
Fmt two to prepare for our output messages.
And here we will also create the section text right now, section text Global Main.
We are declaring this for the global entry point of the program main here and that's it.
So now we will pass the beep here and after that.
It will save this.
Leaving the base pointer.
Maybe the base pointer here.
Move.
Khabib.
Actually, let's this.
RB.
And also RSP.
Now with this.
We are set.
Set up the base pointer.
And here, moving on this text section, we declare the global main function, which marks the entry
point of our program.
And we are also setting up the stack frame with the past rbpp.
Uh.
And move rwp rsp instructions ensuring that we have a proper execution environment.
And now let's proceed with this.
Here we will move the racks.
Move.
Tracks here.
The number one here we are loading the value of number one into the register racks.
But the value of.
Of number one into register racks and move rb x rb x.
We will write number two.
Here we are loading the load, the value of number two into a register.
RBCs again.
And here we start loading by values of number one and number two into registers.
The the racks here now holds value of.
42, and RBCs contains the value of 41.
Right.
So these registers are.
Where we will perform comparisons.
Now here we will compare the values in Rax and Rex.
In order to do that, we will use this instruction.
So.
Racks and RBCs.
And after that, here.
ING cmp for comparing the values in RACs and RBC's.
And here we will use the.
He here?
As greater.
Now with this.
Here we are.
Uh, jump.
To greater if Rax is greater than or equal to rb x.
Um, and next we use this.
Um, here again, I will explain this again from zero.
So this we are using this CPAp instruction to compare the values in racks and as I explained here.
So the interesting part is here this instruction.
So if racks is greater than or equal to RB x, we jump to a greater label.
Otherwise we continue with the next instruction here.
The chairs.
We will load the format string for the second message.
So move.
Audi Fm2.
This is for loading the format string.
Praying for for the second message move.
Zero.
This is.
This is for clearing racks here.
No xmm registers in.
All here.
And we will call the print.
Grand theft.
This is this calls the printf.
Number one.
Less than number two.
And GMP.
BMP exit.
So here this jump.
To the table.
So if the comparison condition didn't met here.
Uh, wasn't or wasn't mad.
Uh, we moved here, so we load the format, string Fmt into the RDA register and we clear rax since
no error mem registers are involved here.
Um, or registers involved here.
And we then call the printf function to display the number.
One is less than number two.
Following that, we perform unconditional jump to the X label here and now we will use this greeter
function here.
And that's it.
Move.
R the i fmt one.
Here we are.
Load loading the format string.
Spring for the first.
Message and again, move racks.
Zero.
And here we are clearing clear the Or.
Yeah.
Clear the racks again and we will.
We are.
We will call the print.
Print.
Here we are.
Call, call print printf to display.
Display number one, greater than or equal to number two.
So if the comparison condition didn't met we.
When the racks greater than or equal to are accident meant we arrive at the greater label.
Here we load the format string Fmt one into RDA register and clear Rax once more.
So we then call the print function to display the message number one now greater than or equal to number
two.
And after that we will also.
Create the exit label.
Exit label.
So here we will actually down here.
So here we will move.
RSP.
RVP here.
RVP Here we are restoring the stack pointer.
Uh, pop up here.
Here we are.
Restore restoring the bass pointer again.
Pointer.
And we are now we're going to return from the main function.
And.
Brett here and we will not add anything here.
Just go to command.
Write that return from the.
A main function.
And finally, we reach the exit label.
This is where we wrap up our program.
We restore the stack pointer, RSP and base pointer rb.
Oops, sorry.
Not RB rb here to their original values.
So we then use the pop rb p instruction to clean up the stack frame and read instruction.
The read instruction gracefully exits the main function and in conclusion, our journey through does
jumping that ACM has unveiled the mechanisms of conditional jumps and branching.
So we have explored how assembly code can make decisions based on conditions influencing program flow.
And these fundamental concepts lay the foundation for understanding more complex programming challenges
and optimizing the code.
So now when we click on this here, and as you can see here, we will get an error.
You are getting this error because instruction we wrote the 64 bit assembly code, but here as default,
the CSM comes with the 32 bit instruction.
Or selected.
And in order to fix that, we will go to settings here.
Um.
World here from settings world and here.
As you can see, we have mode here click on 64.
And assembler click on NSM because NSM best for this purposes.
And as you can see here, when we click on 32 bit x86, we are getting this M 32 here.
But when we click on 64 bit, as you can see it, it also changes here.
It also changed l 32 two from 32 to x l 64.
So now apply this here.
And click okay.
Now let's run our program again.
And as you can see here, we'll start it and build successfully.
And our output is number one, is greater than or equal to number two.
And the program finished.
Normally, execution time is 0.001.
So we can also go to our folder where we compile this assembly here, the CD jump.
LZ.
And as you can see here, we have jumping.
We can also create a separate Makefile and just compile it.
But in this case we will not do that.
And as you can see here, our code is works and when we.
Here on test the program executing program finished.
Normally the execution time is 0.0001.
And this is our output.
So remember, mastering this building blocks will empower you to delve deeper into the intricacies,
intricacies, world of assembly programming and keep practicing, experimenting and pushing your boundaries.
So the journey has just begun in this lecture here, and thank you for joining me in this enlightening
exploration.
Until next time, keep coding and keep expanding your programming, programming skills and happy learning
awaiting you in next lecture.