chevron_left chevron_right
Login Register invert_colors photo_library
Thread Rating:
  • 0 Vote(s) - 0 Average


filter_list Tutorial So you want to learn assembly ( Part 1 )
Author
Message
So you want to learn assembly ( Part 1 ) #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:
  • 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]

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:

  1. We will have 3 registers: EBX, EBP and RIP, the last one being the instruction pointer.
  2. The ALU can perform just a MUL --> multiplying 2 numbers
  3. 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 ! Biggrin


-=-=-=={ 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           |  0xFF


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 ! Biggrin


-=-=-=={ 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
[Image: iQDVDdD.gif]

Reply

RE: So you want to learn assembly ( Part 1 ) #2
Bumping this because maybe someone will find it useful Biggrin
[Image: iQDVDdD.gif]

Reply

RE: So you want to learn assembly ( Part 1 ) #3
I shall quote @phyrrus9.

This Is his forte, so I'm sure he'll find this of Interest.
[Image: AD83g1A.png]

Reply

RE: So you want to learn assembly ( Part 1 ) #4
(11-10-2018, 05:08 AM)mothered Wrote: I shall quote @phyrrus9.

This Is his forte, so I'm sure he'll find this of Interest.

This is more targeted for newcomers that want to learn assembly and don't know where to start, so without any doubt he already knows this stuff.
[Image: iQDVDdD.gif]

Reply

RE: So you want to learn assembly ( Part 1 ) #5
(11-10-2018, 08:47 AM)Cr3aTor Wrote:
(11-10-2018, 05:08 AM)mothered Wrote: I shall quote @phyrrus9.

This Is his forte, so I'm sure he'll find this of Interest.

This is more targeted for newcomers that want to learn assembly and don't know where to start, so without any doubt he already knows this stuff.

Yes, judging by your Introduction of the CPU & Registers (which Is quite basic), It's obvious It more so targets those with minimal knowledge.

Be It the absolute basis or the utmost advanced, phyrrus9 enjoys this type of content, hence the quote.
You've documented, elaborated and formatted It quite well. Good work.
[Image: AD83g1A.png]

[+] 1 user Likes mothered's post
Reply

RE: So you want to learn assembly ( Part 1 ) #6
(11-10-2018, 10:09 AM)mothered Wrote: Be It the absolute basis or the utmost advanced, phyrrus9 enjoys this type of content, hence the quote.
Oh, I hope he is going to have a great little read then.


(11-10-2018, 10:09 AM)mothered Wrote: You've documented, elaborated and formatted It quite well. Good work.
Thanks for the kind words.
[Image: iQDVDdD.gif]

Reply

RE: So you want to learn assembly ( Part 1 ) #7
(11-10-2018, 11:31 AM)Cr3aTor Wrote:
(11-10-2018, 10:09 AM)mothered Wrote: Be It the absolute basis or the utmost advanced, phyrrus9 enjoys this type of content, hence the quote.
Oh, I hope he is going to have a great little read then.


(11-10-2018, 10:09 AM)mothered Wrote: You've documented, elaborated and formatted It quite well. Good work.
Thanks for the kind words.

You're welcome.

Thanks for taking the time to contribute a HQ thread.
[Image: AD83g1A.png]

Reply

RE: So you want to learn assembly ( Part 1 ) #8
While I understand this is geared towards beginners and written by someone that hasn't had to dig deep enough to know the distinction between these, I'll offer them as background material to think about.

(11-04-2018, 02:03 PM)Cr3aTor Wrote: today I will be talking about assembly: what it is, how to use it

Be aware that the term assembly isn't quite as unique as this thread may imply. Assembly refers to an entire class of "languages" that describe machine code. Historically every model of processor had a widely different instruction set (machine code), and therefore had a very different assembly dialect. More modern processors try to use common ISAs, and so they look similar, but they are not. There isn't enough actual assembly in this thread to tell you which ISA, but it looks pretty close to both AMD64 and 686.

(11-04-2018, 02:03 PM)Cr3aTor Wrote: 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.

This list should also include the following:
  • Floating point or fuzzy math unit (if CPU is newer than 1987)
  • coprocessors
  • memory management unit
  • instruction queue and pipeline
  • caches

(11-04-2018, 02:03 PM)Cr3aTor Wrote: 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.

This isn't quite true. Registers are the ONLY memory your CPU has access to. They're almost always the same width as the main bus, and these are crucial to the operation of your processor, as almost all of them (and all modern ones) only do operations on registers. AMD64 ISA includes instructions that act on RAM, however in the first set of pipeline stages, these are converted to a stream of microcode instructions that will retrieve the value, store it in a register, do the operation on the registers, then store the register back into RAM.

(11-04-2018, 02:03 PM)Cr3aTor Wrote: 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.

This is ISA specific. AMD64 uses modern ARM-like register naming (R0-Rx), ARM used R0-R15, AArch64 uses X0-X31, SPARC uses a bunch of different letters all with a number attached to them. This is absolutely something you need to know intimately when you begin.

(11-04-2018, 02:03 PM)Cr3aTor Wrote: 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.

This was true up until the late 80s, when computer processors became more complex, multicore, used caching, branch prediction, and longer pipelines. The "Control Unit" being referenced here is simply the small amount of logic that controls pipeline flow (assuming OOX doesn't exist, we're not getting into that here). If you're more curious, go look up a pipeline diagram for your specific processor. As an example, here's the PL from a relatively old Intel CPU (note, every core has one of these)
[Image: sandybridge_pipeline.jpg]


I'm not going to get into the very platform specific stuff below this, for all intents and purposes it's close enough.

[+] 4 users Like phyrrus9's post
Reply

RE: So you want to learn assembly ( Part 1 ) #9
Thank you for taking your time to read and correct me, as said in the thread I'm by no means an expert and still learning.
So thanks for the additional information which is helping me and other users.
[Image: iQDVDdD.gif]

Reply






Users browsing this thread: 1 Guest(s)