5.30 · exercise

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:

bits31302928-232221-109-54-0
fieldsf = 1op = 0S = 0100010sh = 0imm12RnRd

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:

  1. The starter holds a real add x1, x2, 42 at the label probe. It never runs, but its four bytes can be loaded like any other word. Encode Rd = 1, Rn = 2 and 42 yourself and print probe check: ok when your word matches the assembler's, probe check: mismatch when it does not.
  2. 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 lsl and orr

specification

stdin3 4 100
stdoutprints the right output
exitexits with the right code
sourceuses lsl
sourceuses orr
hiddenright 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.

loading editor...

regfile

N clearZ clearC clearV clear

x0–x30 are the integer registers.

X0arg00x0000000000000000
X1arg10x0000000000000000
X2arg20x0000000000000000
X3arg30x0000000000000000
X4arg40x0000000000000000
X5arg50x0000000000000000
X6arg60x0000000000000000
X7arg70x0000000000000000
X8ind0x0000000000000000
X90x0000000000000000
X100x0000000000000000
X110x0000000000000000
X120x0000000000000000
X130x0000000000000000
X140x0000000000000000
X150x0000000000000000
X16ip00x0000000000000000
X17ip10x0000000000000000
X18pr0x0000000000000000
X190x0000000000000000
X200x0000000000000000
X210x0000000000000000
X220x0000000000000000
X230x0000000000000000
X240x0000000000000000
X250x0000000000000000
X260x0000000000000000
X270x0000000000000000
X280x0000000000000000
X29fp0x0000000000000000
X30lr0x0000000000000000
SP0x0000000080000000
PC0x0000000000400000
console

Output prints here as your program runs.

Press step or run under the editor, or feed stdin from the box below.

not assembled

Open in playground