And to clearly explain what machine code and assembly language are, how they relate to processor architecture,
and how programmers and hardware designers use binary instruction encoding to perform operations like
additions, subtraction, and memory access.
Now, this will be done in a way that is engaging, hands on, and suitable for learning.
Now let's begin with the fundamental idea here.
Computers can encode logic as data.
Now that might sound simple, but in reality it's actually what makes modern computing so powerful.
Early mechanical calculators could add and subtract numbers, but they couldn't change what they were
doing unless you physically rewired them.
Now computers can store instructions in memory just like they store the numbers and then read and execute
those instructions.
And this is called the machine code.
Now it's the set of binary instructions that the processor understands natively.
Now for example you can write on this for example binary.
Now 001110010
and zero.
Yeah this is just a bit right now with these zeros and ones we are basically telling the CPU to add
two to the value in R zero and store it in R one.
So instead of being logged into one task, a computer can change behavior instantly.
Now that's how we can install new software, update an application, or upgrade our operating system.
And all by changing this code that the CPU runs.
Now think about it.
How does a CPU know what to do when it sees a stream of ones and zeros?
Now this is where instruction encoding comes in.
So every processor has something called the instruction.
Set.
Instruction set architecture or AI.
So this is like a language manual for the.
C defining how every combination of bits should be interpreted.
Now let's say we are designing our simple CPU.
We will need to do define what instruction exists.
Like in this case we will add the add subtract.
And load.
And we need to assign a unique binary pattern like an opcode to each instruction.
And we need to decide how many bits to use and how to divide those bits among the parts of the instruction,
which is what?
Operation.
Then it will be, we'll say inputs.
And outputs.
So here what we will basically do is we will add we will give an add.
Here what let's say we want to give it.
Let's say yeah it could be a random number.
It shouldn't.
It is not obligated to use bytes or bytes or just a Signed characters here so we can use seven bits
0001110.
So this is for add.
And let's also use the sub which is subtraction.
0001111.
Now these binary patterns are called the machine instructions.
And when you write a program in binary that is what you are writing.
And writing binary by hand is tedious and error prone.
Now instead, programmers use something called the assembly language.
This is a symbolic version of machine code.
So each instruction has a mnemonic.
This is a short code word to represent that.
For example, this is ad for addition and sub for subtraction.
So here's how um our earlier this mnemonics would look in this case here.
So we have the operation.
It does uh let's say addition.
The opcode it has in this case it is 00011110.
And it has the mnemonic is the AD.
And so we are basically basically creating a dictionary for a CPU here.
So this is for us right.
The operation is for us operation.
It does.
For the this is for the programmer that will use this architecture that we are developing here.
And yeah.
So this is a opcode in bits.
And here this is a mnemonic.
How we want to represent in assembly language.
So we will also have the subtraction.
So in subtraction opcode we will again 01110 no 1111.
And this is what sub.
Assembly code is human readable but maps directly to machine instructions.
So assemblers convert this into binary.
The CPU can run.
Now think of a machine code as a music written in binary and assembly as a sheet music.
So you play the same tune, but one is easier for humans to read.
So we can say that assembly is basically the machine code.
So you may also ask here, where do numbers come from?
Huh?
So yeah, we are using numbers uh.
To use registers.
So when we add numbers we have to specify what to add and where to put the result.
So CPUs use registers to store temporary data.
And registers are small storage locations built directly into the CPU for fast access.
And in this example, let's say um, let's write this code here.
Add.
Register.
Register one.
Register zero.
Two.
So Register zero is the source.
The.
This two here is an immediate value.
Let's actually use different color.
And the R1 here is a destination register.
So this is where the the result is stored.
So registers are crucial because reading from memory is slow.
So using registers keep things fast and efficient.
And.
Another thing you need to learn here is the instruction format.
So we will put it all together here.
So let's assume we are building a 16 bit instruction.
Uh that means every instruction is exactly, uh, 16 binary digits log long.
So we'll break the binary, uh, down.
So but first I will open a new.
Blue board here.
Okay.
So what we will do here, we will arrange seven bits for opcode.
Uh, so the opcode is basically means what operation to do you remember from this previous lecture.
And we will save three bits for the immediate value.
This is a constant number let's say.
And we will save uh also three bits for source register.
So we will write this as r n and we will save three bits.
We will just have to speak more correctly.
We will use three bits for the destination register and in code we will write this as r d d for uh,
basically for the destination here.
So what we will do here is.
In OP.
We will have the zero seven bits.
Seven bit.
This is basically the OP and in the.
Constant number we will have three bit.
In the source register, which is r n, we will have again three bit, and in the destination register
we will have again three bits.
So let's manually encode this instruction.
So operation is add.
Two.
So let's break it down.
So remember from the previous lecture the add is.
00001110.
This two in binary is 010 and the are zero.
Let's say hypothetically is zero and R1 is 001.
So the full binary instruction is
001110010000
and 001.
And if we convert that into hexadecimal it is going to be
18C1X.
Yeah.
So this is basically how it is in hexadecimal.
Now let's do another example.
Let's say sub.
By the way you can change this places with each other.
So it doesn't matter the.
Arrangement of this here.
Basically we are creating a new architecture hypothetically.
So yeah again let's say 2R1 or R zero or.
Yeah let's do the same R one.
So here remember from the previous lecture the sub we assign that 0001.
So in this case 0001.
One and four ones once.
Here.
Yeah.
One.
And now it is time to this.
Yeah.
This is going to be again.
Same.
000.
And again here it's going to be 001.
And this is the thing for the second instruction.
So that's how it basically works.
Now of course the writing binary manually like this is impractical.
So most assemblers can convert uh, from, uh, this readable format into a final machine code.
So you can write a simple program that does not come this, that does this conversion automatically
from this, uh, assembler language to machine code.
So you don't need to worry about bits and bytes here.
Mostly.
Not mostly, but I can say like 20% of the times.
And you will see why I'm saying that.
Uh, so let's add also add the all the R instruction.
You, you remember a load instruction.
Um, so we need to access the memory as well.
Right.
So what will basically do is.
So yeah, so far we have covered the arithmetic instructions uh like the add and sub uh, which operate
on the data that's already inside the CPU's register.
But what happens when we need to use a value that's stored in a memory instead of in register?
Now this is where new kind of instruction Distraction comes into play, which is the load instruction
in our, uh, so-called our own, uh, instruction set architecture by typhoon.
Yeah.
Uh, now, um, let's assign the opcode for this instruction.
Uh, so also write this here and why we are using so much red here.
I don't like the red.
Yeah.
So we will use the load.
So for the load instruction opcode is going to be again let's use the seven bits.
Uh so one and 000.
So my mnemonic is going to be uh load.
So what we will basically do here is.
We have also give the opcode operation name.
And the mnemonic for this operation name is just for us to see what this instruction does.
So we will say our load instruction takes two operands.
Takes it takes two operands a register that will receive the data.
So destination.
And the register that contains the memory address to load from memory.
Right.
So it is.
So it's called the source.
Source rage.
So now let's define our registers.
Uh, just like before.
So we have a register, uh, let's say R0.
Let's actually use the different color.
So we have a register R zero is 000, R one is 001, R two is 010.
And r oops.
Yeah R2010 R3 is 011.
Now that's good.
So if you want to write the load.
R3R2.
That means the load, the value from the memory address stored in R two.
And put it into our tree.
So let's encode this instruction.
You won't need this in real life.
To turn this instruction to machine bits and bytes.
But this will be very helpful for you in your learning process.
Yeah.
Now, let's, uh, get the opcode.
You remember opcode was, uh, three zeros, one on and three zeros again from here.
Oops.
From here.
And um, again.
So here we have uh, 010 on the 010.
So and we also have 011 for the R3 and three bits is unused uh reserved for future.
So we will say just 000.
So this is let's say reserved.
Remember in our previous yeah it is deleted here.
Yeah.
In our subtraction addition we will use this three bits.
Um, but in our a lot we don't need that three bits.
So we will just write it zero saying that it is reserved for now.
Also, this is not a good practice to use a reversed uh, bytes in registers.
But you see We have instructions for these places.
CPU will not see this as a me thing, but yeah, it is good to keep in mind on this.
Bits.
They should be unique for each instruction here.
So you can't use these bits.
And this one and this bits on this one right.
Yeah.
And you will learn uh, that nitty gritties of assembler language uh, as you.
Advanced more in these lectures.
So, yeah.
Uh, so the final, uh, 16 bit instruction, uh, basically becomes, uh, this so you can write this
in binary and hex version, um, on, um, your own, uh, to reinforce and, and.
Yeah.
So, uh, we also have another question you may ask here.
What happened?
What happened during the load?
Basically, first it reads the value in register R2 which is R2 is
0100.
Now it treats that value as a memory address and it goes to the memory location.
It reads the value stored at.
For example here and it stores the value into the register r tree.
And this is called the indirect addressing.
So we are using the contents of a register to point to a memory location.
So basically R2 goes to 010.
So basically says the memory Hypothetically speaking.
Like here.
It doesn't say anything.
42, but so close to here.
And says the memory has value.
42 so.
And it is.
Load.
Load to our tree.
And we also have we can also write the opposite of the load which is what the store.
So we also want if we often also want to write values from a register back into a memory.
Now this is done using this store instruction.
Just I'm saying that hypothetically.
So this is just we are creating our own instruction set architecture.
Now so to be prepared for the next lectures.
And yeah, now this will store the value in R3 into the memory address pointed to R2.
Uh, and of course the it will have a different opcode.
Let's say, uh, for the opcode it will have 0001001.
And as a.
Store and store.
Store Reg.
And.
Yeah.
So you may ask, what is the equivalent of the load in the real arm architecture, uh, instruction
set architecture.
It is the all the r which you will learn in the next lectures.
Now, uh, let's review what we have learned today in this almost half an hour lecture.
So machine code is a binary language that the CPU runs Assembly is a readable version using mnemonics
uh, like uh add sub load.
And there's so much mnemonics here.
And every instruction is encoded as a series of bytes that follow specific rules.
And remember registers are fast local storage units that keep temporary values and memory access using
indirect addressing via instruction like LDR and in our our own architecture set we use this load.
And.
Uh, yeah.
In real CPU like ARM.
Uh, the hundreds of there are hundreds of instructions, but understanding a small subset like the
add sub and LDR gives you a solid foundation.
Now on the phone.
Uh, thank you for watching.
And I'm waiting you in the next lecture.