Learning Computer Architecture
10 chapters
1. CPU Architecture and Instruction Sets
[Verse 1]
Deep inside the silicon heart, there's a story to unfold
Control unit calling shots, like a conductor bold
ALU does arithmetic, logic operations too
Registers hold data close, cache keeps it fresh and new
[Chorus]
Fetch decode execute, the cycle never ends
Load store jump and branch, on these we all depend
RISC or CISC design, each path has its own way
Instructions flow like music through the CPU today
[Verse 2]
Pipeline stages working smooth, like an assembly line
Instruction register holds the code, program counter keeps in time
Memory hierarchy levels, from registers to RAM
Address bus carries locations, data bus moves what we can
[Chorus]
Fetch decode execute, the cycle never ends
Load store jump and branch, on these we all depend
RISC or CISC design, each path has its own way
Instructions flow like music through the CPU today
[Bridge]
Opcode tells us what to do, operands show us where
Immediate or register mode, addressing everywhere
Branch prediction looking ahead, hazards we must avoid
Superscalar execution makes parallelism deployed
[Verse 3]
Machine code speaks in zeros ones, assembly makes it clear
MOV and ADD and CMP, mnemonics we hold dear
Stack pointer manages calls, base pointer frames the scene
Interrupts can break the flow, but handlers keep it clean
[Chorus]
Fetch decode execute, the cycle never ends
Load store jump and branch, on these we all depend
RISC or CISC design, each path has its own way
Instructions flow like music through the CPU today
[Outro]
From transistors to instructions, layers building high
The architecture symphony plays beneath the software sky
2. Registers and Memory Addressing
[Verse 1]
Deep inside the CPU's heart
Live the registers, playing their part
EAX holds data, the accumulator's might
EBX base pointer, keeping things right
ECX counts down when loops are in play
EDX extends when multiplication's the way
[Chorus]
Move it, load it, store it away
Registers dancing where the fast data stays
Direct and indirect, indexed with care
Memory addresses floating through the air
From register to memory, memory to reg
The CPU's ballet, don't skip a leg
[Verse 2]
Stack pointer ESP keeps track of the frame
Base pointer EBP plays memory's game
ESI source index, EDI destination
Moving blocks of data across the nation
Eight bits, sixteen, thirty-two wide
General purpose registers side by side
[Chorus]
Move it, load it, store it away
Registers dancing where the fast data stays
Direct and indirect, indexed with care
Memory addresses floating through the air
From register to memory, memory to reg
The CPU's ballet, don't skip a leg
[Bridge]
Immediate mode, the value's right there
Register mode, no memory to spare
Direct addressing, straight to the spot
Indirect pointing where the data is hot
Base plus offset, displacement in time
Indexed arrays in perfect rhyme
[Verse 3]
Cache line boundaries, alignment matters
Memory hierarchy, no time for chatters
Load from memory into register space
Store back the result, keep up the pace
Instruction pointer knows where to go
Flags register high, flags register low
[Chorus]
Move it, load it, store it away
Registers dancing where the fast data stays
Direct and indirect, indexed with care
Memory addresses floating through the air
From register to memory, memory to reg
The CPU's ballet, don't skip a leg
[Outro]
Registers remember, memory holds true
The dance of the data flows right through
From silicon paths to program's design
Addressing modes keeping everything in line
3. Basic Assembly Instructions
[Verse 1]
When data needs to travel from memory to register space
MOV instruction copies, puts values in their place
From source to destination, it's the fundamental flow
MOV EAX from memory, watch the registers grow
[Chorus]
MOV to move and copy data
ADD to sum and calculate
SUB to find the difference
CMP to compare and validate
These four commands are building blocks
Assembly's foundation rocks
MOV ADD SUB and CMP
Core instructions you need to see
[Verse 2]
ADD takes two operands and puts the sum inside
First plus second operand, no need to divide
Register plus immediate or memory location
Addition is the heartbeat of computation
[Chorus]
MOV to move and copy data
ADD to sum and calculate
SUB to find the difference
CMP to compare and validate
These four commands are building blocks
Assembly's foundation rocks
MOV ADD SUB and CMP
Core instructions you need to see
[Verse 3]
SUB subtracts the second from the first operand
Minuend minus subtrahend, now you understand
Flags get set when overflow or zero is the case
Status register tells you what happened in this space
[Bridge]
CMP compares without changing operands at all
Sets the flags to tell you if numbers rise or fall
Zero flag for equal, carry flag for less than
Sign flag shows negatives in your program's plan
[Chorus]
MOV to move and copy data
ADD to sum and calculate
SUB to find the difference
CMP to compare and validate
These four commands are building blocks
Assembly's foundation rocks
MOV ADD SUB and CMP
Core instructions you need to see
[Outro]
Master these four operations first
They're the foundation, quench your thirst
Every complex program starts this way
MOV ADD SUB CMP every day
4. Control Flow and Branching
[Verse 1]
In assembly language we take control
Jump instructions help us reach our goal
Unconditional jumps will always go
To the address that we choose to show
JMP takes us anywhere we need
No questions asked, it's guaranteed
[Chorus]
Branch and loop, decide and flow
Test the flags and then we know
Jump if zero, jump if not
Conditional logic hits the spot
CMP and TEST will set the way
For the branches we obey
[Verse 2]
Conditional branches check the state
Compare two values, then we wait
For the processor flags to tell us true
Is it equal, less, or greater too
JE jumps when values are the same
JNE continues the branching game
[Chorus]
Branch and loop, decide and flow
Test the flags and then we know
Jump if zero, jump if not
Conditional logic hits the spot
CMP and TEST will set the way
For the branches we obey
[Bridge]
Loops are branches that come back around
With counters keeping track of what we've found
While loops check the condition first
For loops count until the burst
Do-while loops will run at least one time
Making repetition so sublime
[Verse 3]
JL jumps when less than is the case
JG goes when greater shows its face
The carry flag and overflow
Tell us which direction we should go
Labels mark our destinations clear
The assembler keeps them always near
[Chorus]
Branch and loop, decide and flow
Test the flags and then we know
Jump if zero, jump if not
Conditional logic hits the spot
CMP and TEST will set the way
For the branches we obey
[Outro]
From simple jumps to complex trees
Control flow brings us to our knees
But master branches, loops, and more
You'll open up the coding door
Assembly logic, clean and bright
Makes every program work just right
5. The Stack and Stack Operations
[Verse 1]
Memory tower reaching high above
Last in first out, that's the way it works
Stack pointer climbs then descends with love
Every function call, the pointer lurks
Base and top define our working space
Frame by frame we build our calling trace
[Chorus]
Push it up, pop it down
Stack grows tall then shrinks around
LIFO rules the memory game
Push pop push, it's all the same
Stack pointer knows the way
Where to go and where to stay
[Verse 2]
Function calls need local variable room
Stack frame opens with a new address
Arguments and locals find their home
Return address saved for success
Activation record holds it all
Until the function ends its call
[Chorus]
Push it up, pop it down
Stack grows tall then shrinks around
LIFO rules the memory game
Push pop push, it's all the same
Stack pointer knows the way
Where to go and where to stay
[Bridge]
ESP and EBP dancing in the night
Stack pointer moves, base pointer holds tight
Prologue sets the stage for execution
Epilogue cleans up with resolution
Overflow means we've gone too far
Underflow leaves us with a scar
[Verse 3]
Register saves and parameter pass
Stack discipline keeps our data clean
Every push needs its matching class
Pop to balance what we've seen
Frame management is the key
To recursive harmony
[Chorus]
Push it up, pop it down
Stack grows tall then shrinks around
LIFO rules the memory game
Push pop push, it's all the same
Stack pointer knows the way
Where to go and where to stay
[Outro]
From high to low addresses fall
Stack pointer manages them all
Last in first out, the golden rule
Stack operations are our tool
6. Function Calls and Calling Conventions
[Verse 1]
When your program needs to call a function far away
The CPU must save its state before it goes to play
Push the return address right onto the stack
So when the function's done, it knows just how to get back
[Chorus]
Stack it up, stack it down
Parameters flying all around
Calling conventions keep it clean
Best assembly you've ever seen
Push and pop, call and ret
Stack-based communication, don't forget
[Verse 2]
Parameters travel different ways depending on the rule
System V or Windows calling, each one has its tool
Registers hold the first few args, the rest go on the stack
Right to left they're pushed down tight in a parameter pack
[Chorus]
Stack it up, stack it down
Parameters flying all around
Calling conventions keep it clean
Best assembly you've ever seen
Push and pop, call and ret
Stack-based communication, don't forget
[Bridge]
Caller saves or callee saves
Who protects the register waves?
EAX holds the return value tight
While ESP keeps the stack frame right
Function prologue sets the scene
Epilogue keeps memory clean
[Verse 3]
The base pointer marks our frame, a reference we can trust
Local variables live below in memory we adjust
When the function's work is done, restore the stack with care
Clean up parameters passed, leave nothing hanging there
[Chorus]
Stack it up, stack it down
Parameters flying all around
Calling conventions keep it clean
Best assembly you've ever seen
Push and pop, call and ret
Stack-based communication, don't forget
[Outro]
From caller down to callee
The stack connects them perfectly
Function calls in harmony
That's systems engineering mastery
7. Bit Manipulation and Logical Operations
[Verse 1]
Down in the depths where the zeros and ones live
Every bit has a story to give
AND operation keeps what's common and true
OR brings together me and you
XOR flips when they're different inside
NOT inverts every bit far and wide
[Chorus]
Shift left multiply, shift right divide
AND for masks, OR to combine
XOR to flip, NOT to invert the line
Bits are dancing in perfect time
Left shift doubles, right cuts in half
Bitwise magic, do the math
[Verse 2]
When you need to check a single bit's state
Use AND with one, don't hesitate
To set a flag, OR with one will do
Clear a bit, AND with zero's true
Toggle switches with XOR's might
Flip that bit from dark to light
[Chorus]
Shift left multiply, shift right divide
AND for masks, OR to combine
XOR to flip, NOT to invert the line
Bits are dancing in perfect time
Left shift doubles, right cuts in half
Bitwise magic, do the math
[Bridge]
Rotation keeps the bits in play
Circular motion, none drift away
Left rotate brings the high bits low
Right rotate makes the pattern flow
Masks reveal what you want to see
Filter data efficiently
[Verse 3]
Count the ones with Brian Kernighan's way
AND with minus one, bits fade away
Two's complement flips then adds one more
Negative numbers at the core
Sign extension fills the space
Higher bits maintain their place
[Chorus]
Shift left multiply, shift right divide
AND for masks, OR to combine
XOR to flip, NOT to invert the line
Bits are dancing in perfect time
Left shift doubles, right cuts in half
Bitwise magic, do the math
[Outro]
From the lowest level up we climb
Every bit keeps perfect time
Optimization starts below
Where the binary rivers flow
8. Performance and Timing Optimization
[Verse 1]
Every instruction has its price to pay
Clock cycles counting down throughout the day
The pipeline flows like water through the lanes
But hazards block the path and cause delays
Dependencies create a waiting game
While branch predictions try to stake their claim
[Chorus]
Time it right, count each cycle
Pipeline flowing, keep it vital
Stalls and bubbles, avoid the trouble
Write your code without the struggle
Predict the path, cache your data
High performance, pure elata
[Verse 2]
Superscalar execution runs in parallel
Multiple units working, stories they will tell
But resource conflicts bring the system down
When registers are busy all around
The scheduler decides what runs today
While out-of-order tries to find a way
[Chorus]
Time it right, count each cycle
Pipeline flowing, keep it vital
Stalls and bubbles, avoid the trouble
Write your code without the struggle
Predict the path, cache your data
High performance, pure elata
[Bridge]
Loop unrolling saves the day
Reduces jumps along the way
Align your data, sixteen bytes
Memory access, no surprises
Prefetch ahead, don't wait to read
Assembly code that plants the seed
[Verse 3]
Branch misprediction costs you twenty more
While cache misses hit you to the core
Write predictable patterns in your flow
Let the processor know which way to go
Minimize the jumps and keep it straight
Deterministic timing, worth the wait
[Chorus]
Time it right, count each cycle
Pipeline flowing, keep it vital
Stalls and bubbles, avoid the trouble
Write your code without the struggle
Predict the path, cache your data
High performance, pure elata
[Outro]
Every cycle matters in the end
Performance optimization is your friend
From instruction timing to the final call
High-performance systems, optimize them all
9. Memory Layout and Stack vs Heap Fundamentals
[Verse 1]
In embedded systems where resources are tight
Memory layout matters, we must get it right
Four segments divide our precious RAM space
Text code and data each find their place
Static lives forever, globals take their stand
While heap grows upward on programmer's command
But stack moves downward, frame by frame it goes
The gap between them shrinks as program flows
[Chorus]
Stack is fast, heap is vast
Stack's automatic, heap will last
LIFO order, stack's the way
Malloc heap for longer stay
Remember well this memory dance
Stack for speed, heap for expanse
[Verse 2]
Function calls push frames upon the stack
Parameters and locals, then they travel back
When function returns, the frame disappears
Automatic cleanup, no memory fears
But heap allocation needs your careful hand
Call malloc to reserve your piece of land
Remember every byte you claim today
Free it up or memory leaks will stay
[Chorus]
Stack is fast, heap is vast
Stack's automatic, heap will last
LIFO order, stack's the way
Malloc heap for longer stay
Remember well this memory dance
Stack for speed, heap for expanse
[Bridge]
Stack overflow when recursion runs too deep
Heap fragmentation makes your allocator weep
Choose your weapon based upon your need
Stack for structure, heap when data's freed
Stack size is limited, set at program start
Heap can grow larger, playing the long part
Embedded systems need this balance true
Memory-mapped registers depend on you
[Verse 3]
Real-time systems cannot wait around
Heap allocation's timing can't be bound
But stack operations happen in fixed time
Deterministic behavior, performance sublime
Know your memory map from low to high
Text then data, then heap reaching for the sky
Stack starts high and grows toward the ground
In this architecture, efficiency is found
[Chorus]
Stack is fast, heap is vast
Stack's automatic, heap will last
LIFO order, stack's the way
Malloc heap for longer stay
Remember well this memory dance
Stack for speed, heap for expanse
[Outro]
Memory mastery sets the expert apart
Stack versus heap, now you know each part
In systems engineering, this knowledge reigns
Efficient allocation, maximum gains
10. Performance and Timing Optimization
[Verse 1]
When instructions march through silicon gates
Every cycle matters, timing calculates
Fetch decode execute in lockstep time
Pipeline stages must be kept in line
Load delays can stall your perfect flow
Branch predictions help your program go
[Chorus]
Count the cycles, watch the clock
Pipeline bubbles make you stop
Hazards lurking in the queue
Data forwards, bypassing through
Superscalar, out of order
Performance tuning makes you smarter
[Verse 2]
Cache misses cost you hundred cycles more
Memory hierarchy opens distant doors
Register allocation saves the precious watts
Dependency chains create those bottleneck knots
Loop unrolling spreads the workload wide
Prefetch hints bring data to your side
[Chorus]
Count the cycles, watch the clock
Pipeline bubbles make you stop
Hazards lurking in the queue
Data forwards, bypassing through
Superscalar, out of order
Performance tuning makes you smarter
[Bridge]
Instruction level parallelism flows
When compiler schedules what processor knows
Latency hidden by throughput gain
Predictable patterns break the chain
Assembly crafted with careful thought
Microseconds saved are battles fought
[Verse 3]
Branch penalties steal your precious time
Conditional jumps disrupt the rhyme
Profile guided optimization steers
Hot paths illuminated crystal clear
Retire units drain the reorder queue
Speculation gambles on what might be true
[Final Chorus]
Count the cycles, watch the clock
Pipeline bubbles make you stop
Hazards lurking in the queue
Data forwards, bypassing through
Superscalar, out of order
Performance tuning makes you smarter
Makes you smarter, cycle master
Back to Home