Topics: writing numbers in different bases, two’s complement for signed values, shifts and bitwise operations, what a machine instruction is made of, and the ways an instruction can locate its operands. The practice game at the end covers everything on this page.
To read a number in any base, multiply each digit by that base raised to its position, then add. To go from base 10 to another base, repeatedly divide by the target base and collect remainders, reading them bottom-up.
Convert 2345 to base 10, then convert that result to base 2.
Convert 2345 directly to base 2 — without going through base 10 as an intermediate answer.
Note: base 5 isn’t a power of 2, so there’s no shortcut like grouping binary digits in threes for octal. Direct conversion here still means doing arithmetic in base 5 — the value never changes, only how it’s written.
Each place value is a bundle of smaller pieces. In base 5, a unit is 1, a strip is 5 units, a flat is 5 strips (25 units), and a cube is 5 flats (125 units). Each place can hold at most 4 pieces before it rolls over into the next place up, just like base 10 holds at most 9.
Start with 2345 (2 flats, 3 strips, 4 units). Use the buttons to change the blocks and watch both numbers update.
A machine instruction is a fixed binary pattern the CPU decodes. Its basic parts:
addu $10, $9, $8 (R-type, 32 bits) +--------+-------+-------+-------+-------+--------+ | opcode | rs | rt | rd | shamt | funct | | 000000 | 01001 | 01000 | 01010 | 00000 | 100001 | +--------+-------+-------+-------+-------+--------+ 6 5 5 5 5 6 bits rs = $9 (source 1), rt = $8 (source 2), rd = $10 (destination)
Instructions with a constant, such as addi, use the I-type layout: opcode, rs, rt, then a 16-bit immediate in place of rd, shamt, and funct.
The CPU runs every instruction through the same fetch → increment → execute cycle: fetch the instruction at the address in the PC, increment the PC so it points to the next instruction, then execute the instruction (decoding its opcode and operands as part of that step). A jump changes the PC during execute instead of letting it advance by one instruction.
| Format | opcode 31–26 | rs 25–21 | rt 20–16 | rd 15–11 | shamt 10–6 | funct 5–0 |
|---|---|---|---|---|---|---|
| R-type | opcode | rs | rt | rd | shamt | funct |
| I-type | opcode | rs | rt | immediate, bits 15–0 | ||
| J-type | opcode | address, bits 25–0 | ||||
| Register | Number | Use |
|---|---|---|
| $zero | 0 | always 0 |
| $at | 1 | assembler temporary |
| $v0–$v1 | 2–3 | return values |
| $a0–$a3 | 4–7 | arguments |
| $t0–$t7 | 8–15 | temporaries |
| $s0–$s7 | 16–23 | saved registers |
| $t8–$t9 | 24–25 | temporaries |
| $k0–$k1 | 26–27 | kernel |
| $gp | 28 | global pointer |
| $sp | 29 | stack pointer |
| $fp | 30 | frame pointer |
| $ra | 31 | return address |
| Category | Example mnemonic | What it does |
|---|---|---|
| Data transfer | lw, sw | Copy a word between registers and memory |
| Arithmetic | addu, subu, addi | Compute a result from registers, or from a register and a constant |
| Logical | and, or, xor, nor, andi, ori | Bitwise operations on operands (NOT is a pseudo-instruction, written as nor with $zero) |
| Shift | sll, srl | Move bits left or right, filling the vacated bits with 0s |
| Control flow | beq, bne, j, jal | Change the PC, so the next instruction isn’t simply the following one |
The same opcode can find its operand in several different ways. The addressing mode tells the CPU which way applies. Using MIPS examples (lw loads a word from memory into a register):
| Mode | Example | Where the operand comes from |
|---|---|---|
| Immediate | addi $t0, $zero, 5 | The value 5 is inside the instruction itself (a 16-bit immediate) |
| Direct (absolute) | lw $t0, 1000($zero) | The contents of memory address 1000. MIPS has no separate direct form for lw, so $zero (always 0) is the base |
| Register | addu $10, $9, $8 | The contents of registers $9 and $8 (no memory access) |
| Register indirect | lw $t0, 0($s0) | Memory at the address held in $s0 |
| Indexed / base+offset | lw $t0, 100($s0) | Memory at address ($s0 + 100), good for array elements |
| PC-relative | beq $t0, $t1, 3 | Address = PC + 4 + 3 × 4. The green card writes this offset as OFF18, a 16-bit word offset shifted left 2 |
$s0 holds 2000. What is the effective address for lw $t0, 100($s0)? What if the mode were register indirect lw $t0, 0($s0)?
addu $10, $9, $8 with $9 = 34 and $8 = 5, which should leave 39 in $10.addi takes the number 5 directly, so the immediate is a plain number. In lw, the number before the parentheses is an offset added to the base register, so 1000($zero) means address 1000. Check your course’s notation, since some textbooks use different symbols.With 8 bits, the unsigned range is 0 to 255. Two’s complement uses the same 8 bits for signed numbers from -128 to 127, and the leftmost bit has weight -128. A positive number keeps its unsigned pattern. To get the negative of a number, invert every bit and add 1.
Write 45 and -45 as 8-bit two’s complement patterns.
Interactive version: number-representations.html, which also shows +n and -n side by side.
sll d, s, shamt shifts bits left and fills the right side with 0s. srl d, s, shamt shifts bits right and fills the left side with 0s. A left shift by n multiplies by 2n. A logical right shift by n divides by 2n and drops the remainder, for values that are not negative. Both are R-type instructions, with opcode 000000. The funct field is 0x00 for sll and 0x02 for srl.
$4 holds 5. Find $4 after sll $4, $4, 3, then find $4 after srl $4, $4, 2.
Interactive version: shift-instructions.html.
Bitwise operations work on each bit column separately. No carries move between columns.
| Operation | MIPS form | Result bit is 1 when… |
|---|---|---|
| AND | and $d, $s, $t | both bits are 1 |
| OR | or $d, $s, $t | at least one bit is 1 |
| XOR | xor $d, $s, $t | the two bits differ |
| NOT | NOT $d, $s (pseudo); nor $d, $s, $zero | the bit is 0 |
A = 11001010, B = 10100110. Find A AND B and A XOR B.
Interactive version: bitwise-instructions.html, with AND, OR, XOR, and NOT one column at a time.
Horner’s method evaluates a polynomial with one multiply and one add per term, by nesting the terms:
3x^4 - 2x^3 + 10x^2 - 13x + 14 = ((((3)x - 2)x + 10)x - 13)x + 14
The MIPS program in HornersMethod.asm does this with mult and mflo. mult puts the full 64-bit product in the Hi and Lo registers, and mflo copies the low 32 bits into the destination register:
mult $10, $9 # $10 * $9 goes into Hi:Lo mflo $10 # low 32 bits of the product go into $10
Each step uses the accumulator value from the step before it. The result is 440, which matches evaluating 3x4 − 2x3 + 10x2 − 13x + 14 directly at x = −3: 243 + 54 + 90 + 39 + 14 = 440.
This program computes 5x − 74 with x = 12. The multiply puts the product in Hi:Lo, and mflo copies the low word into a register:
ori $8, $0, 12 # $8 = x = 12 ori $9, $0, 5 # $9 = 5 mult $9, $8 # Hi:Lo = 5 × 12 = 60 mflo $9 # $9 = Lo = 60 addiu $9, $9, -74 # $9 = 60 + (-74) = -14
After mult, Hi is 0 and Lo is 60, because the product is positive and fits in 32 bits. The final value in $9 is −14.
A result overflows when it doesn’t fit in the register’s bits. With two’s complement, adding two positive numbers can produce a negative result, and adding two negative numbers can produce a positive one. The sign of the result is then wrong.
Add 01111111 (127) and 00000001 (1) in 8-bit two’s complement.
add, addi, and sub check for overflow. If the result doesn’t fit in 32 bits, the CPU raises an arithmetic overflow exception (code 12 on the green card) and does not write the result. addu and addiu never check, so they wrap around silently. The same bits come out either way; the difference is whether the program is told.
# build 0x7FFFFFFF = 2147483647 in $8 lui $8, 0x7FFF ori $8, $8, 0xFFFF addi $9, $0, 1 add $10, $8, $9 # overflow: raises the exception, $10 is not written addu $11, $8, $9 # no check: $11 = 0x80000000 = -2147483648
The result of addu is wrong for 2147483647 + 1, and nothing in the program reports it. Use add when the program must catch overflow, and addu when wrapping is intended, as in hash functions and address arithmetic.
Ten random questions cover base conversion, two’s complement, shifts, and bitwise operations. Choose an answer, then read the explanation before moving on. Refresh or press Restart for a new set.