Name: _______________________
INEL 4206 Exam I
Fall 2009
September 29, 2009
Open
book and notes. Only the text copies,
instruction sheet handout, slide printouts and your own notes may be used. In the interests of originality and
creativity please turn off all electronic communication devices including celulares, laptops, and pocket computing devices.
1. Describe and program a MIPS program equivalent to the
C program
t1 = 1; s1=45;
t2=10;
while{ t1 < s1)
t1
= t1 * t1;
t2 = t2
– 1;
s1 = s1 * t2 *(t1 – t2) + 1;
}
Assume t1, etc. will be in registers$
t1, etc. and s1
is in $s1 and do need to be initialized. You may need temporary registers.
Describe and program this loop in MIPS assembler (you do not need the .text and
.data, etc., you only need to show the code fragment.
Please place your description here – this should include a description (it
takes one short sentence) of what the program calculates
The program
begins with the initialized definitions of t1, etc. The while loop, while t1<s1 starts with a
label, again, followed by a test to see if t1<s1. If not, the loop is ended by a jump to a
label done. The body of the loop is a
straightforward translation of the three expressions to assembly; the longer
expression takes a temporary to hold t1-t2 and several expressions
And your code here.
.text
li $t1,1
li $s1,45
li $t2,10
bge $t1,$s1,done
again: mul $t1,$t1,$t1
addui $t2,$t2,-1
sub $t4,$t1,$t2
mul $s1,$s1,$t2
mul $s1,$s1,$t4
addui $s1,$s1,1
b again
done:
2. Pseudoinstructions are expanded by the assembler into more
than one machine instruction when instruction, as desired, is not part of the
instruction set.
The symbolic assembler instruction
div $13,
$12, S15
is expanded by SPIM into the sequence of absolute assembler
instructions:
bne $15, $0, 8
break $0
div $12, $15
mflo $13
a. The bne
instruction, if taken, skips the break instruction. Why is it done?
To catch the divide-by-zero case where the divisor, $15 is zero. Otherwise the break is bypassed and the
division proceeds safely.
b. What
does the mflo instruction do?
Places the quotient, which is in lo, into the destination $13
c. Suppose the instruction mflo is replaced by mfhi. What arithmetic operation is performed by
this instruction sequence? Explain.
The remainder – is in hi after the
division, is copied into $13 – this is the pseudoinstruction
rem $13,
$12, S15
3. These questions refer to various
instruction set characteristics. Please answer briefly and avoid the dreaded
RADQ.
a. Why is it necessary to use
two instructions to load a 32-bit constant, as for example in the instruction li $t3,0x435312 into a register?
The RI format has room only for a 16-bit
constant. So the lui
loads the upper half, and the displacement (0x1152) is added to produce the
32-bit address.
b. Explain what the sequence of
instructions
lui $at,
0x3101
lh $t3,0x1152($at) does
Loads the halfword
starting at 0x31011152 into the word-size register $t3, sign extended.
c. Explain what the sequence of
instructions (note these are the previous two in reverse order)
lh $t3,0x1152($at)
lui $at,
0x3101 does
Loads $t3 with the halfword
at 0x????1152, sign-extended. The old
value of $at becomes the upper half of the address – almost certainly a
mistake.
d. Is the destination-first
feature used for writing assembly code for SPIM a feature of the assembler in
SPIM or is it true for all assemblers for the MIPS instruction set.? Explain.
No, some assemblers use the source-first
convention. This is OK as long as you
understand your assembler.
4.
This
question is based on the program file and the SPIM window shown on the next two
pages. You may detach those two pages to
make it easier to read them as you answer the questions.
a.
Show C
code for the
loop – the lines starting at line 20 in the SPIM window (the break has to do
with a breakpoint, but falls inside a pseudoinstruction)
You can use the notation $s1=s1 for variable names.
while (t0=v1$v0) {
if(t0==0)
break;
v1=v0;
v0=t0;
}
b.
When you
reach a breakpoint and want to examine the code step-by-step do you answer yes
or no to the continue question? What do
you then do to step through the code?
You answer no, otherwise you would
continue running until the next breakpoint and not necessarily see any of the
steps. To step, you just keep stepping
with the f10 key.
# Example of a leaf procedure
# Also illustrates
system calls and little convenience functions
.data
.align 0
n1_prompt: .asciiz "First
number - doesn't have to be the largest"
n2_prompt: .asciiz "Second
number - doesn't have to be the smallest"
gcd_prompt: .asciiz "The gcd might just be "
newline: .asciiz "\n"
.text
.globl main
.align 2
get_int_prompted:
li $v0,4
syscall
li $v0,5
syscall
jr $ra
Euclid: move $v1,$a0
move $v0,$a1
again: rem $t0,$v1,$v0
beq $t0,$0,done
move $v1,$v0
move $v0,$t0
b again
done: jr $ra
main: la $a0,n1_prompt
jal get_int_prompted
beq $v0,$0,really_done
move $s0,$v0
la $a0,n2_prompt
jal get_int_prompted
move $a1,$v0
la $a0,gcd_prompt
li $v0,4
syscall
move $a0,$s0
jal Euclid
move $a0,$v0
li $v0,1
syscall
la $a0,newline
li $v0,4
syscall
b main
really_done:
li $v0,10
syscall # back to OS
