Playing with Assembly to make my inner child happy!
- Some Assembly Required
- Understanding Windows x64 Assembly - For help getting this running on Windows since the guide/code was written for Mac
- Control flow is with compare and jump. Symbols:
cmp
,jump
,jl
1 * 2^0 = 1
0 * 2^1 = 0
0 * 2^2 = 0
1 * 2^3 = 8
1 * 2^4 = 16
0 * 2^5 = 0
1 * 2^6 = 64
1011001 (base 2) = 89 (in base 10)
Base 2 - binary
Base 10 - decimal, our typical number system
Base 16 - hexadecimal or hex for short
Using Programming calculator is really nice!
AND - always false unless both inputs are true
OR - always true unless both inputs are false
NOT - flips the input
NAND - always true unless both inputs are true, opposite of AND
NOR - always false unless both inputs are false, opposite of OR
XOR - true if the inputs are different, false is they're the same
XNOR - true if inputs are the same, false, if they're different, XNOR is the opposite of XOR
- CPU - central processing unit, can only do a few things (1) Read data (2) Write data (3) perform simple math calculations via logic gates, everything in the CPU is just numbers, CPU also have a set of instructions physically built into the chip used for
opcodes
, a 64-bit CPU can hold 64 bits of data in it's registers which is 8-bytes - RAM - Random Access Memory, short-term (ephemeral) data that has a much larger capacity than the CPU's registers
- Register - CPU's small storage, generally you get around 16 general use registers depending on the CPU, there are more than than but they are only used internally by the CPU
- Instruction Categories - (1) opcodes,
add
(2) numeric value,4
(3) letter,"A"
(4) register,rax
(5) memory address,0x12345678
- Processor clock - a material that oscillates electricity running through the CPU at a certain frequency giving the CPU a "clock tick" which keep the CPU in sync as each tick is an execute of another instruction, today's CPU are measured in gigahertz (GHz) which are approximately one billion cycles per second
Fetch
- grabs data from RAMDecode
- translates the data into an instruction CPU can understand, the first part of the data is the opcode and the rest are argumentsExecute
- performs instruction, if arithmetic or logic instruction then performs extra work in the ALU (arithmetic-logic unit)
- opcode - unique identifier for an action the CPU can execute,
add
which is really a number4
(not actually 4) - arithmetic-logic unit aka. ALU - performs math or logic and returns value to register
- bit - one unit of data, a 1 or 0
- byte - a group of bits, the smallest collection of bits, 8 bits, fun fact it's 8-bits because of ASCII fitting all characters in the 255 size (technically a
nybble
is smaller but it isn't used often) - word - group of bytes, single word is 2 bytes, double word is 4 bytes, and quadruple word is 8 bytes
- qword - shorthand for quadruple word
Usually, 32-bit processor registers start with e
(eax) whereas 64-bit processors start with r
(rax)
General purpose but some instructions automatically set these:
- rax - accumulator
- rbx - base
- rcx - counter
- rdx - data
- rdi - destination
- rsi - source
General purpose:
- r8 - user defined
- r9 - user defined
- r10 - user defined
- r11 - user defined
- r12 - user defined
- r13 - user defined
- r14 - user defined
- r15 - user defined
Auto-set:
- rbp - stack base pointer
- rsp - stack pointer
- rip - instruction pointer
- eflags - status / condition
A place to store data for later use, last-in first-out data structure.
- Push - place a register value in the stack on top of all the previous values
- Pop - retrieve a value from the top and put it in a register
Conventions for x86-64:
- Arguments get pushed on the stack to "pass" into the "function"
- Arguments get popped off the stack and into registers to access inside of our "function" (stored in r8-r15)
- "Return" values get saved in rax
- General purpose registers can be "callee-owned" or "caller-owned"
- mov dest, source - store data in a register
- add dest, source - addition, dest + source = stored in dest
- sub dest, source - subtraction, dest - source = stored in dest
- mul dest, source - multiplication, dest * source = stored in dest
- jmp label - moves the instruction pointer via a label
.addNumbers
- cmp dest, source - compares two values and stores results in eflags register
- jl label - stands for, jump if less than, by looking in the eflags register
- call label - how assembly implements functions combined with
ret
- ret - returns to line after call
- push source - store data on the stack and increment the stack pointer
- pop dest - retrieve data off the stack and decrement the stack pointer