Jumps Flashcards

(10 cards)

1
Q

What are jumps in low-level programming?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the goto statement in C, and why is it controversial?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

According to Dijkstra, why is the goto statement problematic?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is goto sometimes used for error handling and resource management?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are setjmp and longjmp, and how do they work?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are coroutines, and how are they different from subroutines?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between concurrency and parallelism?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are coroutines used in computer games?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What determines the state of an executing program?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do coroutines manage stack frames?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly