RNRF 2020. 11. 28. 20:05

4. How a CPU works and Introduction to Assembler - bin.0x04

: Content
-> CPU registers
-> Arithmetic Instructions
-> Moves, Jumps, Branches and Calls
-> Stack with PUSH/POP

Tips. It is strongly recommended that you read this article.
-> https://sockpuppet.org/issue-79-file-0xb-foxport-hht-hacking.txt.html

: “hht hacking” of content
--[ assembly programming ]-----------

Assembly language is more intimidating than it is hard. I can sum it up 
for you very briefly:

* You're given 8-32 global variables of fixed size to work with, called 
"registers".

* There are special registers. The most important is the "Program 
Counter", which tells the CPU which instruction we're executing next. 
Every time we execute an instruction, we advance the program counter.
-> Instruction Pointer = Program Counter (IP = PC)

* Virtually all computation is expressed in terms of simple operations 
on registers.

* Real programs need many more than 32 1-byte variables to work with.

* What doesn't fit in registers lives in memory.

* Memory is accessed either with loads and stores at addresses, as if it 
were a big array, or through PUSH and POP operations on a stack.
-> Stack Pointer = SP, ESP, RSP

* Memory is to an assembly program what the disk is to a Ruby program: 
you pull things out of memory into variables, do things with them, and 
eventually put them back into memory.

* Control flow is done via GOTOs --- jumps, branches, or calls. The 
effect of these instructions is to alter the program counter directly.
-> jump - branch - call
-> jmp, jne, je, bne, be, call

* A jump is just an unconditional GOTO.

* Most operations on registers, like addition and subtraction, have the 
side effect of altering status flags, like "the last value computed 
resulted in zero". There are just a few status flags, and they usually 
live in a special register.
-> Zeroflag

* Branches are just GOTOs that are predicated on a status flag, like, 
"GOTO this address only if the last arithmetic operation resulted in 
zero".
-> branch (if) equal to zero == beq
-> jump (if) equal / zero == je, jz

* A CALL is just an unconditional GOTO that pushes the next address on 
the stack, so a RET instruction can later pop it off and keep going 
where the CALL left off.
-> This is an unconditional GOTO pushing the next address in the stack.
-> The RET command can later release the command and continue to the location where the CALL was aborted.

* Understanding concepts is worth a lot more than learning skills.


* Memory Hierarchy: Of Registers, Cache & Memory


* Software Techniques for Shared-Cache Multi-Core Systems