So you want to learn assembly ( Part 1 ) 11-04-2018, 02:03 PM
#1
-=-=-=={ Introduction }==-=-=-
Greetings all, today I will be talking about assembly: what it is, how to use it and some external resources to learn more. I hope this will help the newcomers to get a feeling about it and know where to look for in the future.
Disclaimer: I'm by no means an expert in assembly, thus all I'm going to present it's based on self-research and experience along the years. Therefore, any productive criticism is welcomed.
-=-=-=={ You need to know this in order to continue }==-=-=-
The CPU
========
The CPU is one of the most important components that computers have. Thus, understanding how it works and how it communicates with other components it's an essential part in order to learn assembly. So let's have a look at what is composed of:
1. Registers
Let's start by looking first at the registers.
Registers are just a small piece of memory inside the CPU that are used to quickly accept, store, and transfer data. Because they are so close to the CPU, they are super fast at doing their job. There are different types of registers as well, that indicate different things. For example the RSP and RBP are some of those for the 64bit architecture:
They can be numbered starting from 0 like the memory or they can be named.
Don't bother to much for now with what they're doing and how to use them, I will explain it more later. For now, just remember that registers are a small piece of memory inside the CPU that helps it to execute, store and transfer data.
2. The ALU
The Arithmetic-Logic Unit is basically just performing arithmetic and logic operations. It can add, subtract, multiply, divide, xor, and, or, etc. That's pretty much what a computer does most of the time.
The ALU performs those operations with the values stored in the registers or values taken from memory. For storing them it uses the registers or it stores the values in a memory address. The available options actually depends on the processor. Yeah, that was pretty much it for the ALU.
3. The Control Unit
The last part of the CPU that we are interested in is the Control Unit. The Control Unit is anything else inside the processor except the registers and the ALU (branch prediction unit, cache management, bus signaling, pipeline management, etc). However, it has some functionality that we are interested in and that's pretty mandatory.
One of those being to control the CPU external interface. In other words, the state of all those little pins that go out of the CPU chip. Using these pins, the processor can talk to the memory to read and write values and can also interface to different types of hardware…
Basically when the ALU needs some data from memory for performing an operation, it asks the control unit to activate the right pins on the processor to command the memory chip to read or write a given memory position. To better visualize this I found this picture on the internet that might help:
![[Image: machine-cycle.jpg]](https://www.computerhope.com/jargon/m/machine-cycle.jpg)
Here are some other functions of the Control Unit:
-=-=-=={ Machine Code }==-=-=-
Machine code is binary (1's and 0's) code that can be executed directly by the CPU. If you were to open a machine code file in a text editor you would see garbage, including unprintable characters.
So in the old days, some tech savys thought of a way to be easier for us, humans, to understand and write machine code. Therefore assembly was created. You can think of it as a mapping between 0's and 1's to words.
However, keep in mind that the CPU doesn't understand assembly, thus we write assembly code and with the help of an assembler or a compiler, we convert the assembly to machine code.
-=-=-=={ A simple fictional processor }==-=-=-
Ok, now you should have a basic understanding of how processors work, so let's make a fictional one. We will call it from now on HAM-201.
Now, in order to proceed I need to step back for a little and start explaining what the Instruction Pointer ( IP ) is. This is a special register that indicates which memory address in the main memory contains the next instruction to run. Whenever the CPU executes an instruction, this register increments by one ( unless it's a jump/branch instruction ) therefore moving to the next address to execute.
Now let's take a look at the specifications for our processor:
Now we need to define what instructions it can run. In our case, this will look something like this:
As you can see we have 3 columns, the first one is containing the actual machine code, second one the assembly and the 3rd one is a short description.
Now let's go and build our first program !
-=-=-=={ Our first program }==-=-=-
Let's make a simple program that multiplies 2 numbers.
So, our program requires 8 bytes of memory, thus our RIP is going to need to increment 7 times, because we start from 0.
-=-=-=={ Conclusion }==-=-=-
That's it folks, I hope you learned something new and that you enjoyed the time spent to read this. I will come back with more tutorials for assembly if you guys want it. As we go down the road, I might start discussing about more complex things. Don't forget to rep me if I helped you somehow, and look at the resources section to learn more about the topics covered today.
Thanks again for your time, cheers !
-=-=-=={ External Resources }==-=-=-
https://www.differencebetween.com/difference-between-x86-and-vs-x64/
https://stackoverflow.com/questions/36529449/why-are-rbp-and-rsp-called-general-purpose-registers
https://www.quora.com/What-is-register-Explain-the-types-of-registers?share=1
http://ecomputernotes.com/fundamental/input-output-and-memory/what-is-registers-function-performed-by-registers-types-of-registers
http://ecomputernotes.com/fundamental/introduction-to-computer/control-unit
https://www.techopedia.com/definition/8179/machine-code-mc
Greetings all, today I will be talking about assembly: what it is, how to use it and some external resources to learn more. I hope this will help the newcomers to get a feeling about it and know where to look for in the future.
Disclaimer: I'm by no means an expert in assembly, thus all I'm going to present it's based on self-research and experience along the years. Therefore, any productive criticism is welcomed.
-=-=-=={ You need to know this in order to continue }==-=-=-
The CPU
========
The CPU is one of the most important components that computers have. Thus, understanding how it works and how it communicates with other components it's an essential part in order to learn assembly. So let's have a look at what is composed of:
- A set of registers
- An Arithmetic-Logic Unit (ALU)
- Some control logic.
1. Registers
Let's start by looking first at the registers.
Registers are just a small piece of memory inside the CPU that are used to quickly accept, store, and transfer data. Because they are so close to the CPU, they are super fast at doing their job. There are different types of registers as well, that indicate different things. For example the RSP and RBP are some of those for the 64bit architecture:
- RBP point to the base of the current stack frame
- RSP point to the top of the current stack frame
They can be numbered starting from 0 like the memory or they can be named.
Don't bother to much for now with what they're doing and how to use them, I will explain it more later. For now, just remember that registers are a small piece of memory inside the CPU that helps it to execute, store and transfer data.
2. The ALU
The Arithmetic-Logic Unit is basically just performing arithmetic and logic operations. It can add, subtract, multiply, divide, xor, and, or, etc. That's pretty much what a computer does most of the time.
The ALU performs those operations with the values stored in the registers or values taken from memory. For storing them it uses the registers or it stores the values in a memory address. The available options actually depends on the processor. Yeah, that was pretty much it for the ALU.
3. The Control Unit
The last part of the CPU that we are interested in is the Control Unit. The Control Unit is anything else inside the processor except the registers and the ALU (branch prediction unit, cache management, bus signaling, pipeline management, etc). However, it has some functionality that we are interested in and that's pretty mandatory.
One of those being to control the CPU external interface. In other words, the state of all those little pins that go out of the CPU chip. Using these pins, the processor can talk to the memory to read and write values and can also interface to different types of hardware…
Basically when the ALU needs some data from memory for performing an operation, it asks the control unit to activate the right pins on the processor to command the memory chip to read or write a given memory position. To better visualize this I found this picture on the internet that might help:
![[Image: machine-cycle.jpg]](https://www.computerhope.com/jargon/m/machine-cycle.jpg)
Here are some other functions of the Control Unit:
- Regulate transfers of information between memory and I/O.
- Fetches and decodes instructions from microprograms.
- Responsible for correct instruction execution between a processor's many sub-units.
- Control unit converts received information into sequence of control signals, and transfer to computer processor.
- It controls data flow inside the computer processor.
-=-=-=={ Machine Code }==-=-=-
Machine code is binary (1's and 0's) code that can be executed directly by the CPU. If you were to open a machine code file in a text editor you would see garbage, including unprintable characters.
So in the old days, some tech savys thought of a way to be easier for us, humans, to understand and write machine code. Therefore assembly was created. You can think of it as a mapping between 0's and 1's to words.
However, keep in mind that the CPU doesn't understand assembly, thus we write assembly code and with the help of an assembler or a compiler, we convert the assembly to machine code.
-=-=-=={ A simple fictional processor }==-=-=-
Ok, now you should have a basic understanding of how processors work, so let's make a fictional one. We will call it from now on HAM-201.
Now, in order to proceed I need to step back for a little and start explaining what the Instruction Pointer ( IP ) is. This is a special register that indicates which memory address in the main memory contains the next instruction to run. Whenever the CPU executes an instruction, this register increments by one ( unless it's a jump/branch instruction ) therefore moving to the next address to execute.
Now let's take a look at the specifications for our processor:
- We will have 3 registers: EBX, EBP and RIP, the last one being the instruction pointer.
- The ALU can perform just a MUL --> multiplying 2 numbers
- It will have an instruction MOV to assign values to registers
Now we need to define what instructions it can run. In our case, this will look something like this:
Code:
OPCODE | Assembly | Description
--------------+--------------------+-------------------------------------------------------------------------
0xdd xx | mov ebx, xx | Copies the value in xx into the ebp register
0xde xx | mov ebp, xx | Copies the value in xx into the esp register
0x01 xx yy | mul xx, yy | Multiplies the values of registers xx and yy and stores the result in the xx register.
0x90 | nop | No operation, does nothing
0xFF | halt | Stops the CPU
---------------------------------------------------------------------------------------------------As you can see we have 3 columns, the first one is containing the actual machine code, second one the assembly and the 3rd one is a short description.
Now let's go and build our first program !

-=-=-=={ Our first program }==-=-=-
Let's make a simple program that multiplies 2 numbers.
Code:
Assembly | Machine Code |
+--------------+-----------------+
mov ebx, 10 | 0xdd 0x0a
mov ebp, 4 | 0xde 0x04
mul ebp, ebx | 0x01 0x0a 0x04
halt | 0xFFSo, our program requires 8 bytes of memory, thus our RIP is going to need to increment 7 times, because we start from 0.
-=-=-=={ Conclusion }==-=-=-
That's it folks, I hope you learned something new and that you enjoyed the time spent to read this. I will come back with more tutorials for assembly if you guys want it. As we go down the road, I might start discussing about more complex things. Don't forget to rep me if I helped you somehow, and look at the resources section to learn more about the topics covered today.
Thanks again for your time, cheers !

-=-=-=={ External Resources }==-=-=-
https://www.differencebetween.com/difference-between-x86-and-vs-x64/
https://stackoverflow.com/questions/36529449/why-are-rbp-and-rsp-called-general-purpose-registers
https://www.quora.com/What-is-register-Explain-the-types-of-registers?share=1
http://ecomputernotes.com/fundamental/input-output-and-memory/what-is-registers-function-performed-by-registers-types-of-registers
http://ecomputernotes.com/fundamental/introduction-to-computer/control-unit
https://www.techopedia.com/definition/8179/machine-code-mc








![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
























![[Image: sandybridge_pipeline.jpg]](http://pages.tacc.utexas.edu/~eijkhout/istc/html/graphics/sandybridge_pipeline.jpg)