How the CPU works

An Intro

System Programming is much like black magic to most soft engineers, let alone front-end developers. System Programming involves C/C++ and ASM for reasons of performance. Most of us would take things for granted and totally ignore how things have been designed to work since the age of John von Neumann. I truly encourage everyone in the IT field should take their time to understand the brain of computers - the CPU.

I would like to demonstrate how popular CPUs execute instructions and how memory should be laid out after each instruction with AngularJS. You will also find how CoffeeScript can be useful when handling large data constructs, e.g. the allocation of memory in RAM.

See the Pen cpu-js by Khanh Hua (@khanhhua) on CodePen. Click on `RUN` or `STEP` and observe...


I wrote a very simple application in C, which consists of variable assignment, address inspection and string formatting. The application does not import any libraries to avoid cluttering the generated ASM code.

The source code

int main(int argc, char*argv[]) {  
    int a = 5;
    *int ptr_a = &a;
    printf("a = %d",a);

ASM Compilation (AT&T)

push   %rbp  
mov    %rsp,%rbp  
sub    $0x20,%rsp  
mov    %edi,-0x14(%rbp)  
mov    %rsi,-0x20(%rbp)  
movl   $0x5,-0x4(%rbp)  
lea    -0x4(%rbp),%rax  
mov    %rax,-0x10(%rbp)  
mov    -0x4(%rbp),%eax  
mov    %eax,%esi  
mov    $0x1c,%edi  
mov    $0x0,%eax  
callq  0xf0<printf@plt>  
mov    $0x0,%eax  


  1. Assign 5 to variable a
  2. Declare a pointer and assign the address of a
  3. Print a using format string "a = %d"

Final Words

I have seen debates on whether or not a programmer should know as many programming languages as possible. It depends on really how much you know about the many/few/one language. Personally I can code in C#, Vb, Java, Python, JavaScript, Erlang. And yet, it is crucial for me to have the fundamental knowledge about computers, networking, algorithms and others unrelated yet relevant to my careers. I hope this little demo has shown you something new!