25. Cooperative Scheduling
Take turns between tasks and distinguish policy from CPU instructions.
Open this lesson →
1. Predict and trace
Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.
LOAD 1
CALL taskA
CALL taskB
HALT
taskA: PRINT
SLEEP 500
RETURN
taskB: LOAD 101
PRINT
RETURN
Queue input: None
Initialize memory: All bytes initially zero
2. Build and check
Read a number of rounds from 0 through 3. Start task A’s counter at 0 and task B’s at 100. Each round, call A to increment and print its counter, then B to increment and print its counter.
Required instruction types: INPUT, LOOP, CALL, RETURN
Starter program
INPUT
COPY R0 R3
LOAD 0 R1
LOAD 100 R2
LOOP R3
CALL taskA
CALL taskB
RETURN
HALT
taskA:
# Advance R1, print, return.
RETURN
taskB:
# Advance R2, print, return.
RETURN
Your program and notes
3. Explain the machine
Is CALL/RETURN turn-taking the same as hardware preemption?
26. Partitioning Work Across Cores
Divide independent jobs and combine their results.
Open this lesson →
1. Predict and trace
Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.
STORE 1 0
STORE 2 1
STORE 3 2
STORE 4 3
LOAD [0]
MUL R0
COPY R0 R1
LOAD [2]
MUL R0
ADD R1
PRINT
HALT
Queue input: None
Initialize memory: 0: 1, 1: 2, 2: 3, 3: 4
2. Build and check
Four job values are seeded at memory 0–3. Worker even computes value[0]² + value[2]²; worker odd computes value[1]² + value[3]². Call both workers. Print the even total, odd total, and combined total in that order.
Required instruction types: CALL, MUL, LOAD, ADD
Starter program
CALL evenWorker
PRINT
CALL oddWorker
PRINT
LOAD R1
ADD R2
PRINT
HALT
evenWorker:
# Save even total in R1, return it in R0.
RETURN
oddWorker:
# Save odd total in R2, return it in R0.
RETURN
Your program and notes
3. Explain the machine
Does calculating two worker totals sequentially prove parallel execution?
27. Interrupts and Handlers
Handle an event and restore the interrupted foreground value.
Open this lesson →
1. Predict and trace
Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.
STORE 4 240
LOAD 17
CALL handler
PRINT
LOAD [241]
PRINT
HALT
handler: PUSH
LOAD [240]
ADD 1
STORE 241
POP
RETURN
Queue input: None
Initialize memory: All bytes initially zero
2. Build and check
Read a foreground integer, then an event byte. Store the event at address 240. Call a handler that writes event + 1 to byte memory 241 while preserving foreground R0 with PUSH/POP. Print the restored foreground value, then the stored event result.
Required instruction types: INPUT, PUSH, POP, CALL, STORE
Starter program
INPUT
COPY R0 R1
INPUT
STORE 240
LOAD R1
CALL handler
PRINT
LOAD [241]
PRINT
HALT
handler:
# Save R0, process the event, restore R0.
RETURN
Your program and notes
3. Explain the machine
What must a handler restore before foreground resumes?