CS231 MIDTERM EXAM
Student-Id :
Name :
1.
i.
(6 pts) Given the 8-bit pattern 1010 1100, find what it represents as a
decimal number in :
a)
Sign and Magnitude - 44
b)
1s complement -
83
c)
2s Complement -
84
d)
Unsigned integer
172
notations
ii. (12 pts) If $2 = 0 x 21AC and $3 = 0 x 8C15, show the value of $1 in
binary after execution of each instruction below. Assume the values of $2 and $3 are as
above for each instruction.
a) add $1, $2, $3
0 x ADC1
..
b) xor $1, $2, $3
0 x ADB9
..
c) slt $1, $2,
$3
0 x 1
.
d) sltu $1, $2, $3
0 x 1
.
e) sll $1, $2, 4
0 x 0002 1AC0
.
f) lui $1, 0 x 3A2C
0 x 3A2C 0000
.
iii. (7 pts) Find
the minimum MIPS instructions to perform the following C statements
Use any registers for variables and
assume the required values are already loaded.
a)
x[3] = x[4] + 8; lw $t0,16($s0); addi
&t0,&t0,8; sw $t0,12($s0)
.
b) a = b -5; addi $t0, $t1, -5
.
c) k = 3 *m + 2;
addi $t1,$0,3; mult $t0,$t1; mflo $t2; addi $t3,$t2,2
d)
a++;
addi $t0, $t0,1
.
2.
For the following code segment :
a)
(12
pts) Fill in the machine code representations in binary. Opcodes
for all R instructions are 0; function code for add is 32; opcode for addi is 8; opcode for bne is 4;
opcode for j
is 2.
b)
(6 pts) Fill in the comments
001000 00000 00001 0000 0000 0000 0000 Begin : addi $1, $0, 0 #
initialize $1 to 0
.
001000
00000 00010 0000 0000 0001 0100
add $2,
$0, 20 #
initialize $2 to 20
..
000000 00001 00001
00010 00000 100000 Loop
: add $1, $1, $2 #
add $1 to $2 and store at
$1
..
001000 00010 00010
1111 1111 1111 11110 addi $2,
$2, -2 #
decrement $2 by 2
000100 00000 00010 1111 1111 1111 11101
bne $2, $0,
000010 (Lower Begin address bits) j Begin #
jump to0 beginning
c)
(
7 pts) Describe briefly what the program is achieving. Give also the final
values of all registers used
This program calculates the sum of all even nubers between 0 and 20.
It also jumps back
(infinite loop) The value is 110.
3.
(25
pts) A C function to sort the elements of an array is given below. The
dimension of the array as the number of words and its address are passed as
parameters to this function. Write this function in MIPS with comments. Obey
calling conventions.
sort( int n, int a[])
{ int i,j;
for(i=0;i<n;i++)
for
(j=i+1; j<n; j++)
if (a[i] < a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
( a tested program)
.data
a: .word 3,5,7,1,23,7,8,9,2,19
.text
la $a1,a
la $a0,10
sort : addi $t0,$0,0 # i=0
L1 : addi $t1,$t0,1 # j=i+1
L2: sll $t2,$t0,2 # $t2=4*i
add $s0,$a1,$t2 # $s0=&a[i]
sll $t3,$t1,2 # $t3=4*j
add $s1,$a1,$t3 # $s1=&a[j]
lw $t2,0($s0) # t2=a[i]
lw $t3,0($s1) # t3=a[j]
bge $t2,$t3, skip # branch to skip if a[i]>=a[j]
add $t4, $t2,$0 # temp=a[i]
sw $t3,0($s0) # a[i]=a[j]
sw $t4,0($s1) #a[j]=temp
add $t2, $t3,$0 # a[i]=a[j]
skip: addi $t1,$t1,1 # increment j
bne $t1,$a0,L2 # if not equal to n go back
addi $t0,$t0,1 # increment i
bne $t0,$a0,L1 # if not equal to n go back
li $v0,10
syscall
4. Write a MIPS function that
accepts a binary number in register $a0 and returns a value in $v0
corresponding to the number of ones in the binary number.
a) (6 pts) Write the pseudocode for this function
b) (14 pts)
Write the MIPS assembly code with comments
c) (5 pts)
Show how to call this function from the main program
a)
calc(int number)
{ int sum=0;
for(i=1;i<=32;i++)
{ check=number
& 1;
if (check==1) count++;
number=number>>1;
}
b)
calc: addi $v0,$0,0 # counter for # of 1s
addi $t3,$0,1 # mask
addi $t0,$0,32 # counter for loop
beq $t1,$0,
skip # if result is 0 skip
addi $v0,$v0,1 # else increment count
skip: srl $a0,$a0,1 #
shift number right
addi $t0,$t0,-1 # decrement loop count
bne $t0,$0,
jal calc # call the function