Essential MIPS Assembly Programming Practice Problems
Classified in Computers
Written on in
English with a size of 2.83 KB
1. MIPS Addition of Four Variables
Code in MIPS to add the 4 integer variables: a, b, c, d.
add $e, $a, $b add $e, $e, $c add $e, $e, $d
2. MIPS Implementation of f = (g + h) - (i + j)
Mapping f, g, h, i, j to $s0, $s1, $s2, $s3, $s4:
add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1
3. Why is a MIPS Register 32 Bits?
32 bits occur frequently and thus in MIPS, it has a special name: ‘word’. A size of 32 aligns well with the MIPS instruction format.
4. Understanding Big Endian in MIPS
‘MIPS is Big Endian’ means the most significant byte is stored at the least address of a word.
5. Purpose of lw and sw Instructions
These are MIPS data transfer instructions used to access specific memory locations:
- lw (load word): Transfer data from memory to register.
- sw (store word): Transfer data from register to memory.
6. MIPS Code for g = h + A[8]
Assume g in $s1, h in $s2, and base address of A in $s3:
lw $t0, 32($s3) # 32 is A[8]*4 add $s1, $s2, $t0
7. MIPS Code for A[12] = h + A[8]
Assume h in $s2 and base address of A in $s3:
lw $t0, 32($s3) add $t0, $s2, $t0 sw $t0, 48($s3) # 48 is A[12]*4
8. Advantages of Registers Over Memory
Registers are faster to access than memory. Operating on memory data requires loads and stores, increasing instruction count. Compilers must prioritize register usage for variables.
9. MIPS Code for k = k - 5
Assume k is in $s1:
addi $s1, $s1, -5
10. Transferring Data Using $zero
Transfer the content of $s1 to $t2 using the add instruction:
add $t2, $s1, $zero
11. Binary Patterns in 2's Complement
- Most-negative: 1000 0000 ... 0000
- Most-positive: 0111 1111 ... 1111
12. Explaining x + ~x = -1
If ~x is the complement of x, then x + ~x = 1111...1111₂ = -1. Note that ~x + 1 = -x.
13. Sign Extension Explained
Converting 8-bit signed numbers to 16-bit:
- +2: 0000 0010 becomes 0000 0000 0000 0010
- -2: 1111 1110 becomes 1111 1111 1111 1110
14. MIPS Register Roles
- $v0, $v1: Result-returning registers
- $a0 - $a3: Argument registers
- $t0 - $t9: Temporary registers
- $s0 - $s7: Saved temporary registers
15. MIPS Code for A[300] = h + A[300]
Assume $t1 has the base of A and $s2 maps to h:
lw $t0, 1200($t1) add $t0, $s2, $t0 sw $t0, 1200($t1)
16. Checking the 10th Bit in $t1
addi $s1, $zero, 1024 # 2^10 = 1024 and $t2, $t1, $s1 bne $t2, $zero, YES