What you learn today
- Shift bits in a circle
- 54-bit width
- Running light
A running light: one lamp is lit, the next takes over, and after the last one it starts again at the front. With sll the bit falls out on the left and is gone. Rotating holds on to it: what falls out at the top comes back in at the bottom.
rol and ror
| Instruction | does |
|---|---|
rol r? a n | rotates a left by n places |
ror r? a n | rotates a right by n places |
In the game both rotate within 54 bits, not 64. A bit that goes past bit 53 on the left lands in bit 0.
rol r0 %0001 1 # %0010 = 2
ror r1 %0010 1 # %0001 = 1
rol r2 %1000 2 # %10_0000 = 32
The 54-bit trap
If you rotate bit 0 right by one, it lands right at the top in bit 53. As a number that is gigantic and completely useless for four lights. Going left it also takes 54 steps until a bit is back at the front. For small running lights you therefore need a small ring of your own.
Building small rings yourself
For a ring of four bits you bring the bits that fell out back yourself: rotate left, shift everything from bit 4 to the right, put it back in and cut down to four bits:
rol pattern pattern 1
srl tmp pattern 4 # bit 4 slides to bit 0
or pattern pattern tmp
and pattern pattern $F # keep only bits 0 to 3
Going right uses the same code: in a ring of four bits “1 to the right” is the same as “3 to the left”. With eight bits it would be 7 to the left, and instead of $F and 4 you use $FF and 8.
Running light
Each lamp gets a bit: lamp 0 is lit when bit 0 is set, and so on. You get the bit out with srl and and 1 – tomorrow that works in one line with ext.
A tick lasts 0.5 s. If you rotate every tick, the light moves two steps per second. It gets slower with a counter that only lets it rotate every n-th tick.
Rotating is not arithmetic. sll doubles a number until bits fall out on the left. rol only doubles as long as the top bit is free – after that it reappears at the bottom and the number suddenly becomes small.
Diagram
Exercise
Task
Build a running light over the four lights “LL0” to “LL3”. pattern starts at 1. Every tick the light moves on by one lamp:
- switch “Richtung” closed (
Open0) or missing: upwards (LL0 → LL1 → LL2 → LL3 → LL0) - switch open: downwards
- exactly one lamp is always lit
Hints
Hint 1
Step size with select: 1 upwards, 3 downwards (ring of four bits).
Hint 2
After rol: srl tmp pattern 4, or, and pattern pattern $F.
Hint 3
Lamp n: srl tmp pattern n, and tmp tmp 1, sbn ….
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in