3. MIPS Programming Flashcards
Explain the function of the following MIPS commands:
add $t1 $t2 $t3
sub $t1 $t2 $t3
and $t1 $t2 $t3
or $t1 $t2 $t3
xor $t1 $t2 $t3
sll $t1 $t2 5
srl $t1 $t2 5
sra $t1 $t2 5
add: sums the values of $t2 and $t3 and stores the result in $t1
sub: subtracts the value of $t3 from $t2 and stores the result in $t1
and: performs a bitwise and operation on $t2 and $t3 and stores the result in $t1
or: performs a bitwise or operation on $t2 and $t3 and stores the result in $t1
xor: performs a bitwise xor operation on $t2 and $t3 and stores the result in $t1
sll: shifts the value of $t2 left by 5 and stores the result in $t1
srl: logically shifts the value of $t2 right by 5 and stores the result in $t1
sra: arithmetically shifts the value of $t2 right by 5 and stores the result in $t1
How many programmer exposed registers does MIPS have?
32
What is the R-format of MIPS instructions?
6 bits for main opcode.
5 bits for operand 1
5 bits for operand 2
5 bits for the result
5 bits for the shift amount
6 bits for the sub-function opcode.
The registers $s0 through $s7 are used primarily for …..?
Program variables.
The registers $a0 through $a3 are used primarily for …..?
Arguments.
The registers $v0 and $v1 are used primarily for …..?
Values of results and expressions.
The registers $t0 through $t9 are used primarily for …..?
Temporary variables.
Give two examples of special role registers in MIPS.
Zero register, PC, stack pointer, frame pointer, global pointer, return address.
Which standard registers are preserved on call?
$s0 to $s7
What is the I-format of MIPS instructions?
opcode = 6 bits
source_reg = 5 bits
dest_reg = 5 bits
constant = 16 bits (signed)
How is a constant of more than 16 bits dealt with?
The load instruction is used:
li $r1 constant is translated into:
lui $r1, constant_1 —> loads the 16-bit constant into the upper 16 bits of r1.
ori $r1, $r1, constant_2 —> bitwise or the lower 16-bits
Write a simple MIPS program to copy an array of size 10 beginning at the address in $s1 to an array at address stored in $s2.
add $t0, $t0, $zero
loop:
beq $t0, 10, end
lw $t1, 0($s1)
sw $t1, 0($s2)
addi $s1, $s1, 4
addi $s2, $s2, 4
addi $t0, $t0, 1
j loop
end:
Which of the following I-format operations sign extend:
lw, addi, sw?
lw and addi
Give an advantage and a disadvantage of word alignment over unalignment.
Advantage: Coding is easier, because it is known where one word starts/ends.
Disadvantage: Extra memory is taken up in spacing out words.
Explain the terms little endian and big endian with reference to the register containing: 0A 0B 0C 0D
In little endian, the least significant byte is stored first -> 0D 0C 0B 0A
In big endian, the most significant byte is stored last -> 0A 0B 0C 0D (MIPS)
Is beq R-format, I-format or J-format?
I-format
What is the J-format of MIPS instructions?
opcode: 6 bits
pseudo-address: 26 bits
Is slt R-format, I-format or J-format?
R-format
Which is not a pseudo-instruction:
slt, sle, sgt, sge, seq, sne?
slt
Describe how the jal and jr instruction works.
jal sets $ra to the value of $pc (as this will have already moved on by 4).
Then, $pc is set to the value of label.
jr sets $pc to the value of $ra.
Explain how addresses for nested method calls are stored.
Stored as a stack in memory, starting from the top address (0x7fffffff). The stack pointer should be pointed at the lowest value of the stack address.
Push:
addi $sp, $sp, -4
sw $ra, 0($sp)
Pop:
lw $ra, 0($sp)
addi $sp, $sp, 4
What uses does the stack have, other than to store nested method calls?
Saves caller’s registers, so the callee can use them.
Can pass and return parameters.
Used for local variables within the function.
Compare CISC and RISC architectures.
RISC (Reduced Instruction Set Computer) is a much simpler set of instructions than CISC (Complex Instruction Set Computer).
RISC has more registers, more memory and faster clock frequencies than CISC.
What is $at used for?
It’s reserved to handle large constants.
Define ‘spilling’ in the context of registers.
The moving of less commonly used variables from registers to memory
Define ‘masking’ in the context of bit manipulation.
AND-ing with a number with 0s in the place you don’t want, and 1s in those you do, concealing information.
In MIPS, what instruction would perform a NOT operation on $t0 and send the result to $t1?
nor $t1, $t0, $zero
Why does NOR have no immediate?
Constants are rare for NOR.