Machine and Assembly Language Programming
Assembly Language Instructions: Meaning, State, and Encoding
Assembly Language Instructions connect readable programs to the operations a processor executes. An assembly instruction expresses an operation using a mnemonic and its operands. A machine instruction represents an operation and its operands as an encoded bit pattern. You will use RV32I to connect these representations and predict their effects, without studying compiler integration or assembler implementation.
Reading an instruction
RV32I provides 32 integer registers, named x0 through x31. Each holds 32 bits. Register x0 always reads as zero; attempts to change it do not alter that value. A register's number identifies a storage location, not the number stored there.
For arithmetic instructions, read the destination first. In add x5, x6, x7, the processor adds the contents of x6 and x7, then writes the result into x5. The source registers retain their values unless one also names the destination.
The instruction addi x9, x8, 13 instead adds an immediate constant to the contents of x8. Using R[i] for the contents of register i, its governing operation is
The modulo operation keeps the low 32 result bits. An instruction does not automatically report signed arithmetic overflow.
Distinguishing calculation, memory, and control
Memory addresses count bytes. A 32-bit word occupies four consecutive bytes, so consecutive words start four bytes apart. The word accesses considered here use addresses divisible by four and valid memory locations.
Instruction form | Effect |
|---|---|
add rd, rs1, rs2 | Add two register values and write rd. |
sub rd, rs1, rs2 | Subtract the value in rs2 from the value in rs1 and write rd. |
addi rd, rs1, k | Add constant k to the value in rs1 and write rd. |
lw rd, k(rs1) | Copy a 32-bit memory word into rd. |
sw rs2, k(rs1) | Copy the value in rs2 into a 32-bit memory word. |
beq rs1, rs2, target | Branch to target when the register values are equal. |
bne rs1, rs2, target | Branch to target when the register values differ. |
For loads and stores, calculate the byte address before accessing memory:
The base register does not change. A load copies the contents at address A; arithmetic that calculates A produces the address itself.
The program counter, written PC, determines instruction order. Here all instructions are 32 bits long, with no compressed instructions. A label can name a branch target. With signed byte displacement Δ,
A taken branch measures its displacement from the branch instruction's own address. Equality branches compare register contents directly; they do not require a preceding subtraction.
Connecting notation to instruction bits
An assembler translates supported assembly instructions into machine encodings. For the base instructions shown here, each instruction occupies one 32-bit machine word. Other assembler conveniences can expand differently, so this correspondence should not be assumed for every accepted assembly statement.
The I-type format used by addi contains five fields:
The five-bit rd and rs1 fields encode register numbers. For addi, funct3 is 000 and the opcode is 0010011. The immediate occupies 12 bits and represents integers from −2048 through 2047 using two's complement.
To encode a negative immediate k, write 212+k in 12-bit binary. During execution, sign extension preserves its signed value when extending it to 32 bits.
What to remember
- Read each instruction according to its operation and operand roles.
- Distinguish a register number, its contents, a memory address, and the contents at that address.
- Track register changes, memory changes, and the next instruction address separately.
- Encode register numbers and fixed-width immediate bits, rather than the current register contents.
Example: Assembly Language Instructions in a Calibration Update
Example: An RV32I controller adjusts a stored sensor reading. Initially, x10 = 768 and x7 = 31. The 32-bit memory word beginning at byte address 776 contains the signed integer 42. The controller executes the following four instructions, starting at byte address 4096:
lw x5, 8(x10)
addi x6, x5, -11
sw x6, 12(x10)
beq x6, x7, matchedEach instruction occupies four bytes, and matched labels an instruction at byte address 4128. All numbers above are decimal. Assume valid, aligned memory accesses and no arithmetic overflow. Addresses count bytes, while loaded values represent sensor readings.
For part c, use the I-type field order imm[11:0], rs1, funct3, rd, opcode, with widths 12, 5, 3, 5, and 7 bits. For addi, funct3 is 000 and the opcode is 0010011.
Part a: Load and adjust the reading
What signed value does x6 contain after the first two instructions?
First calculate the effective address, the memory byte address formed by adding the base register's contents and the instruction's offset:
The load therefore accesses byte address ________. It copies the word's contents, ________, into x5; it does not copy the address into x5.
Next, addi adds its signed constant to the loaded value:
The final signed value in x6 is 31. The load and arithmetic have not changed the source memory word or the base register x10.
Part b: Store and choose the next instruction
Which memory word is changed, what value is stored there, and what is the next instruction address after the branch?
The store uses the unchanged base register and a different byte offset:
It copies the value 31 from x6 into the 32-bit word starting at byte address 780. It does not change x6 or the word at address 776.
The instruction addresses are 4096, 4100, 4104, and 4108, so the branch itself is at byte address 4108. beq compares the register contents directly: x6 holds ________ and x7 holds 31, so the values are equal and the branch is taken. No preceding subtraction instruction is required.
The branch displacement is the signed byte difference between the target and the branch instruction's address:
The displacement is ________, giving
The result is 31 stored at byte address 780, followed by execution at byte address 4128. Using 4112 as the branch reference would incorrectly measure from the following instruction.
Part c: Encode the arithmetic instruction
Encode addi x6, x5, -11 as an eight-digit hexadecimal machine word.
Start with the 12-bit immediate. Its negative value is represented modulo 212:
The unsigned value of the immediate field is ________, whose 12-bit binary representation is 111111110101.
Next encode the source and destination register numbers, keeping all five bits. Source register x5 gives ________, while destination register x6 gives ________. Neither field contains the sensor reading stored in its register.
Place the fields in the supplied order:
111111110101 00101 000 00110 0010011
The widths add to 12+5+3+5+7=32 bits. Regroup the same bits into groups of four:
1111 1111 0101 0010 1000 0011 0001 0011
Converting each group to hexadecimal gives F F 5 2 8 3 1 3. The final machine word is FF528313. Its immediate field represents −11 when interpreted as signed two's complement, even though the field's unsigned interpretation is positive.
You are inspecting the source and encoded instructions for a small processor program. Match each term to its role in representing, translating, or specifying the program's instructions.
Specification of programmer-visible operations, registers, and instruction encodings
Binary instruction encodings that a processor can execute directly
Translation program that converts symbolic instruction source into encoded instructions
Symbolic instruction notation using operation names and operands
Instruction set architecture
Assembler
Machine language
Assembly language
An RV32I data logger starts with x10 = 1024. The 32-bit memory word starting at byte address 1032 contains 37. It executes the following instructions in order:
addi x11, x10, 8
lw x12, 8(x10)
addi x12, x12, -5
sw x12, 12(x10)All numbers are decimal, all memory accesses are valid and aligned, and no arithmetic overflow occurs. Complete the table with exact decimal integers after all four instructions finish. Memory locations are identified by their starting byte addresses.
| Quantity | Final value |
| Register x11 | |
| Register x12 | |
| Memory word at byte address 1032 | |
| Memory word at byte address 1036 |