stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 278

Day 278 of 365 · Expert · Month 10: Bits and data

Bit fields: ext and ins

ext pulls a bit field out, ins puts one in: extracting and inserting fields, the limit of 53 bits and several values in one status word.

Lesson 16 min Week 40

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:

BitsFieldRange
0–3state0–15
4–11pressure in 10 kPa steps0–255
12alarm0–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.

Warning

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

The register bank of the chip r0 42 r1 31 r2 10 r3 -2.5 r4 0 r5 0 r6 0 r7 0 r8 0 r9 0 r10 0 r11 0 r12 0 r13 0 r14 0 r15 0 sp = r16 ra = r17 16 free registers, one number each sp: stack pointer · ra: return address
A field is a range of bits with a start bit and a length. ext reads it, ins writes it.

Exercise

Task

packed (r0) holds the pump station's status word (table above). Without srl and and:

  • state = bits 0–3
  • press = bits 4–11 times 10, i.e. in kPa
  • alarm = bit 12
  • new = 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

Esc
↑ ↓ selectEnter open/ opens search anywhere