Skip to content

Instantly share code, notes, and snippets.

@kezzico
Last active November 9, 2023 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kezzico/8cd5424097fde8b2ed3f6145f16c078b to your computer and use it in GitHub Desktop.
Save kezzico/8cd5424097fde8b2ed3f6145f16c078b to your computer and use it in GitHub Desktop.

Central Processor Unit (CPU)

A CPU, is made of billions of transistors. Little tiny switches that turn on and off. This clever device can be given instructions in the form of binary code. The CPU is the command center. It sets the bits. But if you know its language, you can become the CPUs master.

Master of the CPU universe

Each switch is called a bit. The on represents a 1, and the off represents 0. By combining these 1's and 0's together we can create numbers, or bytes. 8 bits to a byte. 256 different combinations from 00000000 to 11111111.

A 64 bit CPU can read 64 of them all at one time. 100111010101110110011001100110010101111110000000011100110110110.

The CPU has special bits of circuitry made from what are called 'gates'. These gates combined make the basic commands the CPU can accept. Such as add, subtract, multiply, shift, jump.

When the CPU is given the command to add two numbers, it uses an adder in its circuitry to output the result into a 3rd registry. 64 Bit CPUs have circuitry that can add many bits together all at once.

Adder Diagram

CPU Architecture

The set of instructions that are compatible with the CPU is called the CPU Architecture.

Here are some common CPU architectures: x86_64, ARM64, MOS 6502.

The instructions of a given CPU architecture are sometimes seen in a decompiled form called assembly language. Each CPU has its own assembly language. Its own set of instructions.

Assembly

Assembly language is a one to one representation of these instructions. There is direct manipulation of CPU registers, RAM, and resource requests are made using interupt codes.

The programmer must be aware of CPU specific concepts such as the stack pointer, which registers to use for floating point numbers, which register to use for ints, null terminators.

Hello world in x86 assembly

section .data
    hello db 'Hello, World!',0 ; Null-terminated string

section .text
global _start

_start:
    ; Output the string
    mov eax, 4         ; System call number for sys_write
    mov ebx, 1         ; File descriptor (1 = STDOUT)
    mov ecx, hello     ; Pointer to the string
    mov edx, 13        ; Length of the string
    int 0x80           ; Call the kernel

    ; Exit the program
    mov eax, 1         ; System call number for sys_exit
    mov ebx, 0         ; Exit status
    int 0x80           ; Call the kernel

hello world in ARM64 assembly

.global _start

.section .data
hello:
    .ascii "Hello, World!\n"
    len = . - hello

.section .text
_start:
    // Write "Hello, World!" to stdout (file descriptor 1)
    mov x8, 4           // syscall number for sys_write
    mov x0, 1           // file descriptor (1 = STDOUT)
    ldr x1, =hello      // pointer to the message
    mov x2, len         // message length
    svc 0               // invoke the syscall

    // Exit the program
    mov x8, 93          // syscall number for sys_exit
    mov x0, 0           // exit status
    svc 0               // invoke the syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment