Saturday, March 12, 2011

How to be Hardcore: Part 1

This might just be me, but whenever I feel like being a hardcore programmer, I program in assembly. Yes, you read that right. Assembly. One step above machine code. So, in order to convert normal programmers into hardcore programmers, I'm going to make a series of tutorials. At the end of each section, I'll include a "tl;dr" (too long, didn't read) summary of the section. In today's episode, I'm going to give you some background information, focusing on the CPU instructions and the CPU itself.

Yes, this makes you cool

Instructions


The CPU is like a little chip that processes instructions, one at a time. In their highest level, these instructions can be something like "open port 51". However, for the CPU to understand it, the higher level function must be broken down into extremely low level functions called instructions. These instructions are pretty much as simple as add and subtract, and compare values.

tl;dr: The CPU runs on simple instructions.

Interrupts:

An interrupt is a special call to the CPU that tells it to do something. Usually, the CPU will read the interrupt number, which is designated using a hex value such as 0x80, and will look at values stored in the registers (basically CPU memory slots) for more information. For example, using an 0x80 interrupt on Linux or a 21h interrupt on Windows will result in a system call, where values from several registers will be read in order to ascertain the type of system call and the options to use.

tl;dr: Interrupts tell the CPU what to do, and uses information in registers for options.

Registers:

By now you're probably wondering, what in the world is a register? Long story short, a register is like temporary memory slots on a CPU for holding data while computing things. The easiest registers to understand for now are the General Purpose registers, named ax, bx, cx, and dx. Think of each of those as a box, and inside each of those boxes is two smaller boxes: the high and low part. Since each register is 16 bits on a 16 bit CPU, and 32 bits on a 32 bit CPU (makes sense, right?) the high and low parts allow you to use only parts of the register at a time, saving time and space. On 32 bit CPUs, the registers have an "e" appended to the front, signifying that they are "extended" registers, and hold another 16 bits. So, a 32 bit ax register would be written as eax, bx would be ebx, and so on.

tl;dr: Registers are temporary memory slots on a CPU and can be broken down into smaller parts.

Well, that's it for today. I figure that I don't want to bombard you, since it takes a little time to really get. Next time, I'll go into how memory is stored on actual memory (not registers) and how to set up an assembler on Windows. Linux already has an assembler (GNU's as) and a linker (GNU's ld) on default install. Yay GNU!

7 comments:

  1. What. I...I mean...0kay I guess.

    ReplyDelete
  2. Yes! I used to do assembler back in college. Great class. If I were one to toot my own horn, I'd add that I had an A in every test including the final. :)

    ReplyDelete
  3. Good work. Assembly seems complicated.

    ReplyDelete
  4. I only read the tl;dr and I already feel hardcore!

    ReplyDelete

Please leave a comment