Thursday, August 5, 2010

A cup of tea with your processor


A couple of weeks ago I went through one of those cruel technical examinations that every newly graduated engineer has before getting his/her first job. 

It wasn’t that bad, Object Orientation: easy! C Language: bring it on! Circuit Analysis: checked! They even asked Elemental Physics: piece of cake… All was fun and games until Assembly came on the ring.

Ohhh shi…


Note for the new guys: Assembly is a low level language used to program microcontrollers and processors.

It turned out that they really needed an assembly badass for that position, so after leaving in despair, I decided to do a little research on the subject and learn about this so called dark arts of programming, hoping  to never experience that shit again.


The story behind the story.

63 years ago the world's first electronic digital computer was turned on, they called it ENIAC, shit was huge, had more than 800 km of cable inside, weighted about 330 tons and check this out: contained more 17,000 bulbs in its cherry belly (how many vintage guitar amplifiers is that?). This copper monster was capable of doing about 100,000 operations per second (kind of lame right now but fucking awesome in the 40’s). 

Anyway, what was really great about ENIAC was that you could actually program that shit. Yeah, even though they needed to reconfigure every single bulb and rewire more than a few kinky circuits in a process that took several days to several people, a programmable computer was a huge step in the olden days. 

"Yeah baby!"

After a while, scientist and engineers got tired of switching bulbs, and one day a crazy Hungarian dude designed a system that could take a crapload of binary code as a set of instructions to create a program, soon,  the first computer language was born... Machine Language

Still, Machine Language was boring as hell and hard as fuck, it didn’t take long for Assembly to first saw the light of day in the early 50’s. 

Note:  A common mistake is to call it assembler language; actually an assembler is what is used to translate assembly language into machine code. 


Why should you give a fuck about Assembly?

Well… you don’t really have to; you can do pretty much everything in C. It’s true that an assembly program will use the memory in a more efficient way but  the time and effort you may put in it probably won’t be worth it, today’s processors are so fucking awesome that you cannot really tell the difference.

Protip:  However, If you are on the road of electronics many employers will ask you for assembly skills (I learned that the hard way).



Hello World

Before this post gets too long I’m going to try and explain an assembly program in a really basic way so we all get the neck.


First of all, when a line start with a "." it means that we are about to tell something important to our processor. For example, ".model small" is like telling it: “easy boy, we won’t use a lot of memory”

The stack is used to store temporary data, it isn’t used in this program but you still need it, you could say that those are processor manners. (since the purpose of this program is to make an .EXE file, the stack is a must)

.data 
message db "Fucking Magnets, How Do They Work?" ,"$"

It's like: “Hey dude, I’m done with the stack stuff, we’re starting the data segment now, be cool and define some bytes, these bytes should contain the information between the brackets and don’t forget that "message" is how we will identify this byte-string thing”.

As you may have guessed, ".code" means that the code segment is about to start and that the data segment has just ended.

"main proc"is, as in any other programming language, the declaration of a procedure that ends in "main endp", let’s say that here is where you put the shit that you want your processor to do.

And then the tricky part begins, remember how we told it to remember that "message" would be the identifier? Well, "seg message" is the number of the memory segment where "message" is in, we need to know this number in order to know where the fuck is that byte-string thing.

"ax" is a register, and registers are like empty bottles where you put stuff. The instruction mov commands our processor to move "seg message" into our "ax" bottle, then we move what we just put in "ax" into the Data Segment register ("ds").  It has to be this way, "mov dsseg message" is not allowed (processor manners again).

I think you get the idea

"mov ah, 09" means to load the "ah" register with a nine.. shit start making sense right?

Ok, we have the number of the segment where message was in order to locate it, well, we also need the offset within the data segment of the bit-string thing to be able to reach it (kind of a x,y coordinates), we do that with "lea dxmessage", (lea: load effective address).

Don’t stop reading you lazy bastard, we’re almost done.

"int 21h" is an interrupt (I may talk a little bit about interrupts in another post) that tells our processor.. “Do stuff man!”. The first thing the processor does is to check "ah". There is a nine remember? , the nine means that the procedure shall write our bit-string “Fucking Magnets, How Do They Work?” to the screen.

mov ax4c00h
int 21h

Finally, these two instructions are pretty much the same, we load 4c00h in our "ax" register ("ah" contains the higher byte of the "ax" register, that would be 4c) and then the same int goes and checks "ah".  This time it contains 4c, that means “exit program”.

And that's pretty much it... I intend to write several post going deeper on assembly and its nasty secrets,  in the meantime if you want to learn more, I highly recommend this site:



No comments:

Post a Comment