Assemble it yourself
Be the assembler for one instruction: add xD, xN, imm, a 64-bit add with an immediate. Standard input gives D, N and the immediate; build the 32-bit word the assembler would produce and print it. With the stdin box as given the program prints:
probe check: ok
add x3, x4, 100 = 0x91019083
The add (immediate) format, from bit 31 down:
| bits | 31 | 30 | 29 | 28-23 | 22 | 21-10 | 9-5 | 4-0 |
|---|---|---|---|---|---|---|---|---|
| field | sf = 1 | op = 0 | S = 0 | 100010 | sh = 0 | imm12 | Rn | Rd |
Bits 31 to 22 never change here, so the word starts as 0x91000000 (one movz with lsl 16 can load it). Shift each field up to its place with lsl and combine them with orr. Write that as a subroutine, encode, because it runs twice:
- The starter holds a real
add x1, x2, 42at the labelprobe. It never runs, but its four bytes can be loaded like any other word. Encode Rd = 1, Rn = 2 and 42 yourself and printprobe check: okwhen your word matches the assembler's,probe check: mismatchwhen it does not. - Then encode the input and print the instruction and its word.
A register number is 0 to 31, and in this format 31 means sp (print_reg already prints it that way). The immediate field has 12 bits, so it holds 0 to 4095. Anything outside those ranges, negatives included, prints cannot encode that and returns 1.
What is checked
- both lines for the input in the stdin box, and for more inputs you cannot see
- the exit status (0, or 1 when the input cannot be encoded)
- the word is built with
lslandorr
specification
lslorrWe run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.