What you learn today
- Pin number from a register
- Register via register
- Loop over pins
In the control cabinet four sensors hang on d0 to d3. Writing four almost identical blocks is tedious, and with six pins it does not get better. Better: the pin number sits in a register, and a loop goes through them.
dr0: a pin from a register
dr2 means: the pin whose number is currently in r2. If r2 holds a 3, dr2 is the same as d3.
move i 0
pin:
l temp dr0 Temperature # dr0: pin with the number from r0
add i i 1
blt i 4 pin
This works wherever a pin may appear: l, s, ls, sdse, bdns and so on.
dr0 contains the register number, not the alias. If i is an alias for r0, the pin is still called dr0. There is no dri. If someone moves the alias to another register, dr0 has to follow.
rr1: register via register
The same exists for registers. rr1 means: the register whose number sits in r1.
move r1 10
move rr1 42 # writes 42 to r10
add r1 r1 1
move rr1 43 # writes 43 to r11
That way you build a small table in the registers, for instance the temperatures of four pins in r10 to r13. Several r work too: rrr1 first reads r1, takes the value as a register number, reads there and takes that value as the number.
If a register number lies outside 0 to 17, the chip reports OutOfRegisterBounds. A pin number outside 0 to 5 also leads to an error.
Loop over pins
Put together, this gives a loop that checks every pin, reads it and puts the value into its own register:
move i 0
pin:
add target i 10 # target registers r10 to r13
move rr1 0 # start value in case the pin is empty
bdns dr0 next
l rr1 dr0 Temperature
next:
add i i 1
blt i 4 pin
Here i is an alias for r0 and target an alias for r1.
For tables with more than a few entries the stack is better. Registers are scarce, and rr eats them quickly. The stack comes in week 18.
Diagram
Exercise
Task
Gas sensors may hang on d0 to d3, some pins are empty.
- In a loop over the pins read the temperatures into
r10tor13(pind0→r10and so on), usingdr0andrr1. - An empty pin gives 0 in its register.
- Write the highest temperature (in K) as
Settingto the housing (db), 0 if no sensor is there.
Hints
Hint 1
i is r0, so the pin is dr0. target is r1, so the target register is rr1.
Hint 2
Inside the loop: add target i 10, move rr1 0, bdns dr0 next, l rr1 dr0 Temperature.
Hint 3
Collect the maximum with max highest highest rr1.
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