Everybody what's going on this is Caleb with Debb slopes dot com and in this video we're going to be
talking about how does code work and not how does like a programming language work.
I mean how does code work from the code we write.
How does that actually do what it does.
How does it get down into the computer.
How does it get down into the C.P. you and to actually be processed and perform certain actions or behaviors.
How does code work.
So let's dive right into it with something we'll understand really well.
If you have any experience with programming at all actually so let's begin with source code.
So imagine you've written a source code file a very simple program even hello world that can print out
Hello world.
Right.
That's a program that you yourself wrote.
Now the language that you used whether it's Python javascript swift Objective C C Sharp whatever you
used that's a human readable programming language and we call these high level languages because they
are so easily interacted with by humans.
And they've been they've been abstracted very far away from what a computer processor can actually understand
because obviously a computer processor thinks very differently than a human.
So that's in contrast to low level languages which we'll get to in a moment but high level languages
easy for humans to work with and abstracted far away from the processor.
Low level languages will talk about soon.
So basically you write your source code and then when it comes time to actually run your code and to
build it and to use it we send it into something called a compiler and compilation is basically the
process of taking source code passing it to the compiler and the compiler acts sort of like a translator.
OK.
And what it's going to do is it's going to translate our source code into a lower level language like
machine code or assembly.
Those are two examples of low level languages.
Now I always have this picture of like the United Nations with all those translators with their earpieces
and everything and I think of the compiler as one of those people sitting in the chair helping our source
code be understood by another party right by this CPQ.
And so we need to talk about machine code and assembly what the heck even is that ok.
If you were to look at it you would probably say the same thing.
What the heck is this.
There are different machine code and assembly but they're similar in that they're both low level languages.
OK.
Now basically they both deal with programming the processor directly.
Machine code is usually a stream of binary data.
OK.
Telling the processor to turn on and turn off certain transistors in order to control certain aspects
of the computer or the program that you're using assembly is a low level language.
It's slightly higher the machine code it's not exactly binary.
OK.
There is a layer of abstraction above machine code called assembly but basically assembly utilizes what
are called registers and we'll talk about that more in a second.
But basically they store instructions that control hardware features on the processor directly.
OK so it's like a layer above machine code but it's also not very easily readable and understandable
by humans.
OK thankfully the compiler does a lot of the heavy lifting for us as developers now.
Assembly does have a particular syntax to enable the running of an assembly program so that it knows
when to begin when to and when to jump to a different register pass data perform calculations just like
you would in an ordinary program with no swifter javascript or whatever machine code doesn't have that
machine code literally is binary.
I mean a can be hexadecimal but for the most part it's binary ones and zeros.
OK so thankfully our source code gets translated into assembly language then into machine code and then
finally at the very end it's compiled into an executable code or an app.
If you're building you know apps for the iPhone or even apps for the Mac.
Now I said that we were going to talk about registers and we are this is a really important foundational
piece of the assembly language.
Now basically in assembly you're given between 8 and 32 global variables of a fixed size.
OK so they're global variables they're accessible from wherever and they have a fixed size.
Those are registers OK.
They're simple variables that can hold bits of information and shift them around in the in memory.
Ok now we can work with these to basically store information perform calculations.
We can store particular instructions on how we want the react and respond.
But a real program is going to need a lot more than 30 to one byte variables right.
That's not a lot of data not a lot of information.
So whatever actually does not fit in the registers is stored in memory K or RAM on your computer it's
sort of like like a temporary placeholder in order to put something while you're doing other work.
I kind of like to think of it like you know if you're cooking on on your countertop and you're out of
space you might set some things over on the other counter while you're working.
And then when you're ready for it you might bring it in and use it and then bring other things back
and set them.
You know you want to be able to have extra space to store things.
And that's what RAM is.
Right.
And obviously if you have a little bit of RAM your computer tends to be slower because it runs out of
space and then has to basically handle removing things and putting things in with less space the more
space the less less clogged your system becomes.
So basically what doesn't fit in the registers fits in memory and things and memory are accessed that
particular addresses sort of like we have like a digital neighborhood in our computer that you can access
and machine code basically tells it where to store it where to put it.
And yeah.
So after the compiler does all the heavy lifting and converts our source code into assembly or machine
code it's then compiled into an executable.
Or if you're developing for iOS or Mac an app.
Ok now this executable or this app actually lives on disk on your computer on your hard drive.
K so it's not in memory.
It's stored on your hard drive.
Now it's basically just a box full of machine code and assembly language that's been converted down
from your source code.
So when we run the application the computer is directly told using that machine code what it should
do.
But the cool thing is that we can interact with it physically like we can click a button we can push
a keyboard key.
And that basically can use our input in order to actually tell the C.P. what to do how to display certain
information how to move this here make this noise.
It all boils down to assembly or machine code.
Very very cool stuff.
Now not every application is an app you can click on and interact with sometimes we have to interact
with it through the terminal.
And that's where an executable can be run in a shell.
OK.
Now a shell is just basically a program written by humans that can run other programs and you know you
see your terminal there command line you can type in commands and when I push enter on the keyboard
the cool thing is the operating system is going to basically say hey we need to switch from the shell
program to this computer program that we built.
Right.
Our source code file and it automatically does that for us which is what makes operating system so powerful
and so smart is that they can automatically determine the context for a particular application.
Create new processes and switch them back and forth based on our input.
This is called context switching.
And it's a really powerful feature of an operating system working together with the CPQ.
And so basically from source code we pass it into a compiler.
It's turned into assembly machine code which is basically binary.
OK.
And that then is then compressed down into an executable file which can either be run with direct interaction
from a user like an application or through the terminal in a shell and RCP who is basically going to
take that code and it's going to interact with it and switch back and forth intelligently between all
of the different inputs and everything.
Thanks to our operating system which is a really really cool thing.
So this in essence is how code works are our code gets converted down into binary through multiple layers
of abstraction and that basically allows you to make a program and that's how code works.
It's converted down thanks to compilers and we can run it.
And then the operating system works together with you in order to display what our code is supposed
to do.
Now of course you can write assembly code yourself and do that directly.
Not many people do that though.
And you also can do that with machine code.
But again not one not many people do that because a lot of the things that machine code and assembly
language do can really easily be done with human created programming languages.
It might just be a little less memory efficient and little more resource intensive but very very cool
stuff.
We have a lot to be grateful for as developers that we don't have to write in assembly code or machine
code anymore.
Very very cool stuff.
So let's head over to the next video and let's continue learning.
This is Caleb with devah slopes dot com.