Hello, my name is Typhoon.
And in this lecture we will delve into the fascinating world of assembly language programming.
In this lecture, we will explore the intricacies of some fundamental arithmetic operations within our
program.
Now get ready to witness the magic of incrementing decrementing shifting, multiplying and dividing.
And throughout this lecture, we will dissect each of these operations step by step, unraveling the
code line by line to understand how they work at their core.
And we will be working with registers, memory and functions to manipulate numbers and display their
results.
The assembly language offers us a such a unique perspective into the inner workings of computers, low
level operations, providing insights into how calculations are executed at the hardware level.
Starting with incrementing, we will see how a single instruction can add one to a number and transform
its value from there.
We will move to Decrementing where we will learn how subtraction plays a role in reducing values.
Shifting.
Our next venture allows us to multiply and divide by powers of two throughout bit manipulation.
And this demonstrates how computer architects use clever tricks to optimize certain operations.
And our journey continues with multiplication and division.
By diving into these operations, we will understand how computers perform these fundamental arithmetic
tasks and how we can harness their power in assembler language.
So fasten your belts as we embark on this enriching journey.
But also by the end of this lecture you will have a solid grasp grasp of incrementing decrementing shifting,
multiplying and dividing, working in assembly language.
Now let's dive in to explore the intricate dance between software and hardware that makes these operations
possible.
But first we will write the incrementing incrementing side of our program.
Here we will load the value of number one to rax.
So move.
Move rax.
Uh, number one inside braces.
Now we will use I and C racks.
This we use this for incrementing rex by one.
We will store the result in result I.
Move.
The result, I result.
And racks.
We will display the result here.
So display the result.
And after that we will move the.
Fmt int.
This is for a local address of format.
Uh, we are loading the address of format string to die and now we will load the address of message
to RSI.
RSI The address of messages.
I nci here.
Number one.
Number one, incremented.
I nci.
Move.
Move!
RDX.
Result, I.
Here we are loading the value of result i to RDX.
Now we will clear the rax register which is returning the value.
And now we will call the printf to display the formatted message.
Now we will develop the decrementing here.
So we will move load the value of number one to rax.
Move.
Racks.
The number one.
Uh, these here we in incrementing we use I and C and here we will use the c here rax this is for decrementing
rax by one.
We will use move here result i here rax we are storing the result in result i and now we will need to
display the results.
So display the result.
Now we will use the load load address of format string to move the i fmt int.
Right?
Yeah.
Fmt it mov rsi.
Uh, Daisy here.
There are string.
Start.
Let's see.
E.
A.
S.
C, d, a c.
And now.
We will use load the value of result I to RDX.
Move RDX.
Result I here and now we will move the racks zero to clear the racks register and we will call the printf.
And now we will need to shift.
Use the shift arithmetic left here.
Shift.
Left shift our metric.
Are.
Arithmetic left.
So here what we're going to do is we will load the value of number one to Rax.
So move rax number one cell rax.
Two.
I will explain all of this newly.
Here.
So move.
Result I here and Rex here we shift Rex left by shifting the Rex left by two bits, which is me, which
means multiplying by four.
Here we are storing the result in result I here and now we will need to display display the result.
Now we are.
We are.
We need to load the address of format string to r.d.e.
So move the.
Are the I fmt integer.
Move the RSI.
Sally, which we defined previously in data section.
Number one shift left to which is multiplying by four.
And move RDX here, which is, uh, for loading the value of result result i to RDX result I to RDX.
Now we will move the racks so we will clear the racks register which is returning the value and we will
lastly call the printf.
Printf.
And after that we will need to shift arithmetic.
We will we will need to write shift arithmetic right here.
So shift.
Arithmetic right now.
We will.
Right.
Move.
Tracks, number one, which is for loading, not bomber number one, and this is for loading the value
of number one to racks.
So s a R for shifting racks right by two bits, which is arithmetic shift, which is this.
This here is divided by four here shift of SA racks and two and move.
Result I here registered result in a result I now display.
Display the result.
Move.
RTI Fmt which is load which is assembly code here loads the address of format string to RTI.
MOV rsi sa i here.
This is followed the address of message to RSI.
Now we will load the value of result I to RDX, move the RDX to result I here and now we will move the
rax we will clear the rax basically call printf again print printf.
They're calling printf to display the formatted message.
We just need to now write the shift arithmetic.
Right with sign expression.
Uh, and as well we will write displaying the result as well.
And we will write, multiply, divide and we will end the program.
And then in next lecture we will try to analyze it.
So there's just 1 or 2 functionalities, uh, to be written in this lecture.
And after that we are done.
So what we're to we have already written the shift arithmetic.
Right now we need to write a shift arithmetic, right, with sign extension.
Here.
Shift.
Arithmetic.
Right.
With sign extension.
This is for now.
We will load the value of the move.
Now we will move racks to negative number.
Which is for loading the value of negative number to racks.
Remember, this is a negative number for now.
Now we will use a.
As a ah, this is for shifting the racks by two bits with sign extension, which is this is also called
the arithmetic shift.
And after that we will store the result in result.
I result.
Yes.
Result.
I basically so result.
Racks and now we will need to display the result here.
So move or die fmt int.
Which is followed.
Address of format string two.
Now we will move the RSI to.
Sorry.
This is for load.
The address of address of message to RSI.
Uh, remember, we have defined something like, uh, here.
Number one shift.
Right?
Two dividing by four with sign expression.
It.
It will understand it better when we create the output for this run the program.
And after that.
Uh, we need to.
Lord the value of result I to move.
MOV rdx is result i here and move now we will move the rax which is zero.
We will clear the rax register and we will call the printf as we always do.
And now we will also multiply it, divide it and that's it.
So multiplication will be much easier than division and you will see why.
So multiply.
So move.
Racks.
Number one.
Here we are loading the value of number one to racks.
We will email here, use email keyword.
Number two.
Here we are.
Multiply we are telling the assembler to multiply rax with the value of number two, which is this is
the 64 bit multiplication and we will use the move here to store the result in result.
I in order to do that we will use the result I here and after that we will tell rax and now we will
need to display the result here.
Display the result mov rda you already know how to display the result here.
Fmt int mov rsi.
MOV RDX.
Result II which is loading the value of result I to RDX.
And now we will clear the register which is returning the value and we will call the print F.
So that's it.
Now we will write our division divide.
Now we will call the move.
Number one.
Here we are loading the value of number one to racks.
And now we will call the.
MOV rdx zero clearing the rdx register, which is a reminder and i div here.
He was.
And.
Award.
Number two.
And after that, with this here we are telling the assembler assembly to divide the racks by the value
of number two, which is a 64 bit, uh, division.
And this is the equation in racks reminder in RDX.
And we will use the move here.
Move.
Result.
I Rex, this is the this is for storing the quotient quotient of in result I and now we will also modulo
modulo here.
This is for storing the reminder or restoring the reminder in modulo.
Here are the x.
Now we will need to display the quotient quotient.
So display the quotient and now we will need to load the address of format string to move the the i
fmt int mov rsi TV.
This is for loading the address of message to RSI and move the.
Now we are.
RSI side, the X here.
The result here is not the value of result to RDX.
Now move the rax zero.
Clear the rax register and call the.
Printf.
And now we need to display the reminder.
And that's all display.
Display the.
Reminder.
And here we will do the same logic here.
Move by Fmt and move.
RSI REM and mov rdx modulo.
And after that, we will move the racks.
Zero.
And after that, we will call the print f.
To display the formatted message.
And now we need to write three instructions as well.
Move rsp rbp for restoring the stack pointer.
Pop Rbp.
For restoring the bass pointer and we will return from the, uh, main function.