Hello, everyone, and welcome to today's video lecture.
In this session, we are going to dive deep into an assembly code example that calculates the sum of
numbers from zero to a given value.
Using a loop construct, we will walk you through the code step by step, discussing how registers,
loops and external functions work together to achieve the desired goal and result.
And here in previous lecture, we just have some typo here.
Let's quickly fix that.
I fix that when I shared the previous lecture code in attachment sections, but I want to fix that.
Uh, here.
Uh, also here.
And let's start by taking a look at the assembly code we will be exploring today.
As you can see on the screen, we have a complete assembly program that calculates a sum of numbers
using a loop.
So the loop construct is quite interesting as it utilizes the RC x register.
So the RC x register as a loop counter.
So automatically decrementing with each iteration.
So before we compile and run the code, let's break down the key components of the program.
So we have a number variable that is initially set to five.
This is the upper limit of our summation.
We are using the printf function to display the calculated sum and the program initializes the loop
counter rc x and an accumulator rax to perform the summation and the loop instruction decrements the
loop counter and repeats the loop by the until the counter becomes zero.
And now let's compile and run the code.
Open your terminal here.
Uh, go to the pod here.
We should have instruction.
Yes, the instruction flags.
And here we have project as actually just let's remove the previous Mac file.
And as you can see, we just only have this ESM file here.
Let's read it Project Dot RSM.
And as you can see, this is our codes that we wrote.
We wrote in previous lecture.
Now what we're going to do is we will create the.
Make file so mousepad.
Make file.
And in this make file we will first add the command like make make file for the project dot ASM and
project.
Here we will make this to.
Project all here and use, we will use the GCC output.
All right from here.
For project.
We will use the GCC.
GCC.
Project project that will use no Pi.
And project that all here.
Roar.
Project that ESM will use.
NSM here.
Elf 64 bit and some 64 bit here.
F 64 G Uppercase L Uppercase F.
Dwarf format.
And project that ESM and we will also the listing here project that.
Project list.
And that's it.
Let's close this now.
Just write the make.
And as you can see here, our program is.
Compiled and linked and assembled here.
And as you can see, we have this project, right?
And now.
Well, once compiled, we can execute the program by running the dot slash project.
And as you can see, the sum from 0 to 5 is 15.
So the that's it here.
This is not so basic because we will also change the numbers.
So now comes the exciting part.
Now let's see how changing number value affects the program's execution and currently this number.
Um.
Yes, this number.
Is set to five.
Now let's change to the larger value.
Uh, something, let's say.
10 million.
Ten.
Here.
And.
Now let's make again and make.
Project.
And as you can see, the sound from 0 to 10 million is something like that, right?
So now we save the saved the file compiled it, and now we can also observe the execution time.
So before we run the program, let's discuss an experiment.
So you can also use the time command in Linux to measure the execution time of the program.
For instance, the Time project.
And as you can see, execution time, the sum of sum from 0 to 1 million is something good.
And here let's actually.
This sum from 0 to 1 million, 1 million.
But we also have this five, right?
Let's actually check with this five again.
As we opened the Oh no, we can't close this because the Sims is open here.
So here, make again time.
Project.
We will note it down here.
The psalm from here.
As you can see here, the value gets bigger and our time is gets bigger, too.
Here.
Let's actually try 10 trillion.
Now let's compile it again.
Make.
Time.
Project.
And as you can see here, let's give us some example here.
Yeah, we are still waiting for it.
And as you can see here, it's almost 20s to 12 seconds here, not 20.
Now, we will this is for.
This.
Yes.
So this is for 10 million.
It did.
It couldn't calculate because we reached the limits of our registers.
Now.
Let's even add six zeros.
One, two, three.
Here.
Now let's test it with it.
I'll ask Claire make.
And time.
Project.
It might.
I think it might take, like, more than.
30s or 20s.
Let's see.
Also consider this like I'm running this operating system in my virtual machine.
My CPU is Intel i7.
The 10th generation.
Let's see.
Many times take here.
Is it?
No.
So here, as you can see here, the.
The sum from 0 to 5 is 15.
The real is 0.003 seconds.
The sum from 0 to 10 million is 0.0 16 seconds and the sum from 0 to 10.
Yeah, it it.
No, it was 10 billion, I think.
Yeah.
Ten.
Yeah, ten is some from uh, 0 to 10 billion is something like, uh, 11.944.
Seconds now.
We are still waiting for it.
So our program will give us give us some output here.
We just reached I think we reached the limits of our virtual machine processing unit.
So this, um, Kali Linux here is installed on the virtual machine here.
And as you can see here, it's 64 bit.
Yeah.
Love 11th generation Intel core i5.
11.
Four.
No.
11.
I5 11 400.
H.
The GPU and memory is I gave eight memory from my 16 gigabyte ram.
We are still waiting here.
We can also check the processes here.
And as you can see here, this project almost takes 25% of our CPU power.
And here we are still waiting for it.
Like were literally Chrome takes 0% and this takes 26% of our CPU power.
The sexual check.
Yeah, it might take, uh, the, uh, like days to compile complete this because my, uh, virtual
machine is pretty slow in this case.
Maybe we should try this on this native, uh, operating system here, but it doesn't matter.
As you can see, by changing the number value, we can explore how the program behaves with different
inputs.
So this exercise showcases the power of assembly language in understanding low level computer operations.
I hope you found this video lecture insightful and educational, and thank you for watching and feel
free.
Feel free to adjust the script as needed to match your style and preferences and good luck.