CSE378
Autumn 1998
Midterm Solutions


QUESTION 1:
Pseudoinstructions are instructions that are not supported by hardware.
The assembler converts them to one or more instructions which are in the
instruction set.
Examples: la, move, mul, etc.


QUESTION 2:
A:
	F	A	B	4
      1111    1010    1011    0100
    - 0000    0101    0100    1100
    - (2^10 + 2^8 + 2^6 + 2^3 + 2^2)
    		-1356

B:
1011.1011 -> 1.0111011 * 2^3 -> 0 | 130 | 0111011 ->
	0 | 1000 0010 | 0111 0110 0000 0000 0000 000

0.1110010 -> 1.111001 * 2^-1 -> 0 | 126 | 111001 ->
	0 | 0111 1110 | 1110 0100 0000 0000 0000 000

	1.0111011 * 2^3	  ->	1.0111011  * 2^3
      +	1.11001   * 2^-1  ->  +	0.000111001 * 2^3
	----------------	----------------
				1.100100101 * 2^3

1.10010011 * 2^3 -> 0 | 130 | 10010011 ->
	0 | 1000 0010 | 1001 0010 1000 0000 0000 000

C:	
-2^9 -> 2^9-1


QUESTION 3:	
6.02*10^23 -> ~8*10^23
10^3 ~= 2^10
=> 10^21 ~= 2^70
8*10~23 ~= 2^80
-> 80 bits
80 < 127 -> fits in sp fp


QUESTION 4:

Not including Cin:
 A   B   C   R   Cout
---------------------
 0   0   0   0   0
 0   0   1   1   0
 0   1   0   1   0
 0   1   1   0   1
 1   0   0   1   0
 1   0   1   0   1
 1   1   0   0   1
 1   1   1   1   1

Including Cin:
Cin A   B   C   R   Cout1  Cout2
--------------------------------
0   0   0   0   0   0      0
0   0   0   1   1   0	   0
0   0   1   0   1   0	   0
0   0   1   1   0   1	   0
0   1   0   0   1   0	   0
0   1   0   1   0   1	   0
0   1   1   0   0   1	   0
0   1   1   1   1   1	   0
1   0   0   0   1   0	   0
1   0   0   1   0   1	   0
1   0   1   0   0   1	   0
1   0   1   1   1   1	   0
1   1   0   0   0   1	   0
1   1   0   1   1   1	   0
1   1   1   0   1   1	   0
1   1   1   1   0   0	   1


QUESTION 5:
instruction	result in $s0 (x=doesnt matter)
-----------------------------------------------
		xxxxxxxxxxxxxxxxxxx
lui:		xxxxxxxxxxxxxxxxxxx
addiu:		xxxxxxxxxxxxxxxxxxx
sll:		xxxxxxxxxxxxxxx0000
not:		xxxxxxxxxxxxxxx1111
andi:		0000000000000000100

$s0 = 4


QUESTION 6:

A:
0011 1100 0000 0001 0000 0000 0010 0000 ->
2^29 + 2^28 + 2^27 + 2^26 + 2^16 + 2^5

B:
0011 1100 0000 0001 0000 0000 0010 0000 ->
001111 | 00000 | 00001 | 0000 0000 0010 0000 ->
15 | 0 | 1 | 32 ->
lui $1, 32

C:
0011 1100 0000 0001 0000 0000 0010 0000 ->
0 | 0111 1000 | 0000 0010 0000 0000 0100 000
=> + 2^(120-127) * 1.0000 0010 0000 0000 01
=> (1 + 2^-7 + 2^-18) * 2^-7 = 2^-7 + 2^-14 + 2^-25


QUESTION 7:
       sub     $s1, $0, $s0
       addi    $s1, $s1, -1


QUESTION 8:
	la 	$t0, label
label:	addi	$t0, $t0, -4

OR

	jal	label
label:	move	$t0, $ra

QUESTION 9:

# initialize counter, and endval
		li	$s2, 0
		li	$s4, 16

# move multiplicand to LHS
		sll	$s0, $s0, 16

loop:
# increment counter
		addi	$s2, $s2, 1

# mask out LSB of $s1 and put result in $s3
		andi	$s3, $s1, 1

# add $s0 to $s1 if $s3 = 1
		beqz	$s3, shift
		add	$s1, $s1, $s0

# shift the product register
shift:		srl	$s1, $s1, 1

# repeat
		bne	$s2, $s4, loop

QUESTION 10:		
PC-relative addressing addes the "address field" of the instruction (which
is a signed 2s complement number) to the PC (usually PC+4) to get the address
of the next instruction, which is a word address.

QUESTION 11:

PART A:

PART B:

  op  | result
--------------
  00  | sra A
  01  | A and B
  10  | A or B
  11  | A or not B