5.29 · exercise
Machine code detective
A 32-bit instruction word arrives on standard input in hex. If it is an add or subtract between registers (the shifted-register format), take it apart and print the instruction it encodes. With the stdin box as given the program prints:
sf=1 op=0 S=0 shift=0 imm6=0
Rm=2 Rn=1 Rd=0
add x0, x1, x2
The format, from bit 31 down:
| bits | 31 | 30 | 29 | 28-24 | 23-22 | 21 | 20-16 | 15-10 | 9-5 | 4-0 |
|---|---|---|---|---|---|---|---|---|---|---|
| field | sf | op | S | 01011 | shift | 0 | Rm | imm6 | Rn | Rd |
sfis 1 for x registers and 0 for w registersopis 0 for add and 1 for sub;Sis 1 when the instruction sets the flags, soop * 2 + Sindexes themnemstable (add, adds, sub, subs)shiftpicks lsl, lsr or asr (3 is not allowed), andimm6is how farRmis shifted first; leave the shift off the printed line whenimm6is 0- in this format register number 31 is the zero register, printed
xzrorwzr
ubfx wD, wN, lsb, width copies width bits starting at bit lsb into the bottom of wD, which is exactly what each field needs. A word whose bits 28 to 24 are not 01011, whose bit 21 is 1, or whose shift field is 3 is some other instruction: print not an add or sub (shifted register) and return 1.
What is checked
- all three lines for the word in the stdin box, and for more words you cannot see
- the exit status (0, or 1 for a word of another format)
- the fields come out with
ubfx
specification
stdin8b020020
stdoutprints the right output
exitexits with the right code
sourceuses
ubfxhiddenright output and exit code on 7 more inputs you do not see
We run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.