Reputation: 19
I got a few questions marked incorrect on a MIPS assembly exam, and I was wondering if anyone could help me figure out why because I don't understand why at all.
First question:
Set all bits of register $t1 to 1
My answer was ori $t1, $t1, -1
The other two questions gave some assembly code and asked to "Write the operation performed by each assembly program." The original code was uncommented. The comments below are mine.
First short-answer question:
.data
len: .word 4
list: .word -4, 6, 7, -2, 1
.text
main: la $s0, list
la $s1, len
lw $t1, 0($s1) // NOTE: 4, so loops over array
add $t0, $zero, $zero
alpha: lw $t2, 0($s0)
blez $t2, beta
add $t0, $t0, $t2
beta: addi $t1, $t1, -1
addi $s0, $s0, 4
bgtz $t1, alpha
add $v0, $zero, $t2 // NOTE: $t2 should be $t0?
jr $ra
My response for the first short-answer question was: "Adds up all positive elements of array, and returns last element of array"
I thought the second last line should have $t0 instead of $t2 so that it would return the sum it calculated instead of the last element, but I asked the instructor about this during the exam and he said to treat all the given code as correct/intended even if I thought it was a mistake.
Second short-answer question. Again, the original code was uncommented. The comments below are mine.
.data
len: .word 4
list: .word -4, 6, 7, -2, 1
.text
main: la $s0, list
la $s1, len
lw $t1, 0($s1) // $t1 = len
add $t4, $zero, $zero // $t4 = 0
alpha: lw $t0, 0($s0)
add $t4, $t4, $t0 // $t4 adds up all elements of array
addi $t1, $t1, -1
addi $s0, $s0, 4
beta: blez $t1, alpha
or $v0, $t4, $zero
jr $ra
My response for the second short-answer question was: "Returns the sum of all elements in the array"
If anyone could help me to figure out what did I do wrong, or if it was marked wrong?
Upvotes: 1
Views: 1182
Reputation: 364200
First part: ori
takes a zero-extended 16-bit immediate (like other bitwise boolean instructions). Other MIPS instructions, including addiu
, sign-extend their 16-bit immediate to 32 bits so 0xFFFFFFFF
is encodeable for those instructions1.
ori $t1, $t1, -1
is not encodeable as a single machine instruction, only supported by MARS's assembler as an "extended" pseudo-instruction that materializes the constant in another register then uses it with or
. (For example addiu $at, $zero, -1
/ or $t1, $t1, $at
).
You can do it in one real machine instruction (and without any input dependency):
addiu $t1, $zero, -1
Footnote 1: I'm not sure why this design decision was made; it's arbitrary and RISC-V chose differently (always sign extend). But see this for more about MIPS.
andi
is certainly useful with all-ones in the high bits, e.g. to align a pointer with p &= -16
. MIPS can bitwise NOT a value using nor
so it doesn't need xori
with -1
to be encodeable. ori
with all high bits set to 1
is likely less useful than doing something with the low 16 (instead of 15) bits of a register.
It also allows lui / ori
for constructing 32-bit values without having to offset the high half to account for bit #15 being set (top of the low half) like you have to for lui / addiu
.
The second part of your question is really a separate question; the answers to it is totally independent of how immediates work for ori
.
Upvotes: 3