What you learn today
- Extract and insert fields
- At most 53 bits
- Pack values
The day before yesterday you got fields out with srl and and. Two instructions do that in one line – and the opposite way too.
ext: pulling a field out
ext r? a start length takes the field starting at bit start with length bits out of a and puts it right-aligned into the register:
ext r0 %1101_0110 4 4 # bits 4–7: %1101 = 13
ext r1 %1101_0110 1 2 # bits 1–2: %11 = 3
That equals srl plus and with a mask of length ones.
ins: putting a field in
ins r? value start length writes the lower length bits of value into the register from bit start on and leaves all other bits alone. The register is target and starting value:
move r0 $FF # %1111_1111
ins r0 0 2 3 # bits 2–4 to 0: 227
ins r0 5 2 3 # bits 2–4 to %101: 247
If value is too big for the field, ins cuts it off: only the lower length bits go in.
At most 53 bits
A field must not reach beyond bit 52: start + length at most 53, length at least 1. Otherwise the chip stops with an error (PayloadOverflow, ShiftOverflow or ShiftUnderflow). That is the 53-bit limit of the decimal number from Monday.
Packing values
With fields you pack several small values into one number. An example for a pump station:
| Bits | Field | Range |
|---|---|---|
| 0–3 | state | 0–15 |
| 4–11 | pressure in 10 kPa steps | 0–255 |
| 12 | alarm | 0–1 |
13 bits for three values: one register or one stack slot instead of three. Next week you use that to send values between chips.
A field only takes whole, non-negative numbers. 101.3 becomes 101 when inserted, and −5 lands in the field as a bit pattern of all ones. Round beforehand and keep negative values out.
Diagram
Exercise
Task
packed (r0) holds the pump station's status word (table above). Without srl and and:
state= bits 0–3press= bits 4–11 times 10, i.e. in kPaalarm= bit 12new= the same packet, but with state 9
Hints
Hint 1
ext state packed 0 4 gets the state.
Hint 2
Pressure: ext press packed 4 8, then mul with PRESS_STEP.
Hint 3
New packet: first move new packed, then ins new 9 0 4.
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