Jumps Flashcards
(10 cards)
What are jumps in low-level programming?
Instructions that move the program counter to new addresses
Can be unconditional (jmp) or conditional (e.g., jg - jump if greater)
Fundamental to controlling program flow
Used to implement loops, conditionals, and other control structures
What is the goto statement in C, and why is it controversial?
Unconditionally transfers control to a labeled statement within the same scope
Criticized by Edsger Dijkstra as creating “spaghetti code”
Makes program flow difficult to read and maintain
Generally discouraged except for specific use cases like error handling and cleanup
According to Dijkstra, why is the goto statement problematic?
Creates code with unclear control flow
Leads to “spaghetti code” that is hard to read and maintain
All programming constructs can be written without jumps
Acceptable only for clean up and exit scenarios
How is goto sometimes used for error handling and resource management?
Create a central error handling label (e.g., errexit)
Use goto to jump to this label when errors occur
Allows for centralized cleanup of resources
Releases memory, closes files, and performs necessary shutdown operations
What are setjmp and longjmp, and how do they work?
setjmp: Saves the current program state (registers) to a buffer
longjmp: Restores the program state from a previously saved buffer
Allows jumping between different parts of a program
Useful for error handling and non-local jumps
Can return to a previous point in code with a specified return value
What are coroutines, and how are they different from subroutines?
Coroutines can be suspended and resumed at specific points
Allow concurrent execution without true parallelism
Can yield control back to the caller
Useful in scenarios like game development for
maintaining frame rates
Can split computationally heavy tasks across multiple frames
What is the difference between concurrency and parallelism?
Concurrency:
- Multiple processes sharing resources
- Time slicing
- Context switching
- Processes take turns executing
Parallelism:
- Multiple processes running simultaneously
- On separate CPU cores
- True simultaneous execution
How are coroutines used in computer games?
Break down computationally heavy routines
Prevent a single routine from blocking the game loop
Maintain stable frame rates
Allow complex computations to be spread across multiple frames
Switch between game loop and computation routine
What determines the state of an executing program?
Virtual address space
Code (executing program instructions)
Data/BSS (static variables)
Stack (function return addresses, parameters, local variables)
Heap (dynamically allocated variables)
Registers, including:
- sp (stack pointer)
- fp (base pointer)
- pc (program counter)
How do coroutines manage stack frames?
Coroutine stack frames remain on the stack
Can yield and resume execution
Maintain their state between suspensions
Careful management required to avoid stack corruption
Warning: Calling additional functions can complicate coroutine management