Homework 2 Solution

2.3.1 -Convert the following C/Java statements into MIPS assembly code:

2.3.1a: f = -g – f;

#assume $t0 holds value for f, $t1 holds value for g
	sub	$t1, $zero, $t1		#subtract g from 0 to get -g; store in $t1
	sub	$t0, $t1, $t0		#subtract f from -g; store in $t0

2.3.1b: f = g + (-f – 5);

#assume $t0 holds value for f, $t1 holds value for g
	sub	$t0, $zero, $t0		#subtract f from 0 to get -f; store in $t0
	addi	$t0, $t0, -5		#add -5 to -f; store in $t0; note: would be ok to use subi pseudoinstruction 
	add	$t0, $t1, $t0		#add g and (-f-5); store in $t0

2.4.1 – Convert the following C/Java to MIPS assembly:

2.4.1a: f = -g – A[4];

#assume A is a label in .data memory as below
#$t0 holds value for f, $t1 holds value for g
	la	$t3, A			#load address represented by A into memory
	lw	$t4, 16($t3)		#A[4] starts 16 bytes (4words x 4bytes/word) from start of A; load value there into $t4
	sub	$t4, $zero, $t4		#subtract value from 0 to get -value; store in $t4
	sub	$t0, $t4, $t1		#subtract g (in $t1) from -value; store in $t0 (f)

.data
A:
.word	1,2,3,4,5,6,7,8,9,0		#arbitrary values for A array (10 values total, or 40 bytes total @ 4 bytes/word)

2.4.1b: B[8] = A[i-j];

#assume A and B are labels in .data memory as below
#$t0 holds value for i, $t1 holds value for j
	sub	$t2, $t0, $t1		#subtract j from i; store value in $t2
	la	$t3, A			#load address represented by A into memory - this is the address of A[0]
#[i-j] is some WORD offset from the start of A. 4*[i-j] is the equivalent BYTE offset.
	sll	$t2, $t2, 2		#shifting left twice multiplies the value by 4 (section 2.6 in reading)
#It would be nice if lw  $t4, $t2($t3) was a valid instruction, but there is no such syntax. 
#Instead, let's just add the byte offset to the base address of A (in $t3) to get the base address of A[i-j]:
	add	$t3, $t3, $t2		#now $t3 contains the address of A[4]
	lw	$t4, 0($t3)		#load value at start of A[i-j] (in $t3) into $t4

	la	$t5, B			#load address represented by B into memory
	sw	$t4, 32($t5)		#store value in $t4 (A[i-j]) into B[8]

.data
A:
.word	1,2,3,4,5,6,7,8,9,0		#arbitrary values for A array (10 values total, or 40 bytes total @ 4 bytes/word)
B:
.word   1,2,3,4,5,6,7,8,9,0		#arbitrary values for B array (10 values total, or 40 bytes total @ 4 bytes/word)

 

2.4.2 – related to 2.4.1: how many MIPS instructions are needed for each C/Java statement above?
The only tricky part here is to know that the "la" instruction is actually a pseudoinstruction and equates to 2 MIPS basic instructions. The others are all basic instructions. Also, remember that directives are NOT instructions. So, for 2.4.1a: 5 instructions. For 2.4.1b: 9 instructions
 

2.4.3 – related to 2.4.1: how many different registers are needed to carry out the C/Java statement (in MIPS assembly)?
This can vary a lot depending on your implementation. I "recycled" registers a lot, and I could have done more (or less) recycling.
So, for 2.4.1a, I used 4 registers (I didn't use $t2 for no particular reason, but I used $t0, 1, 3, and 4). In 2.4.1b, I used 6 registers ($t0-t5)
 

2.5.4 Translate the following hexadecimal numbers to decimal:

0xabcdef12
OK, if I use a calculator, it's a snap. To do it by hand it's (10*16E7 + 11*16E6 + 12*16E5 + 13*16E4 + 14*16E3 + 15*16E2 + 1*16 + 2) = 2882400018

0x10203040
=
270544960

2.5.5 Show how the data for these values would be arranged in memory for a little-endian vs a big-endian machine. Assume the data is stored starting at address 0.
MIPS is big-endian (MSB in theh value has the lowest byte in memory), so in the number 0xabcdef12, 0xab comes first in memory, followed by 0xcd, 0xef, 0x12.
For a little-endian, the ordering is the opposite: 0x12 first, followed by 0xef, 0xcd, 0xab.
 

2.7: For the following 32-bit binary numbers:

0010 0100 1001 0010 0100 1001 0010 0100

0101 1111 1011 1110 0100 0000 0000 0000

2.7.1 what are the equivalent decimal numbers, assuming the above is a 2’s complement representation?

2.7.2 what are the equivalent decimal numbers, assuming the above is an unsigned representation?

Trick questions! In these numbers, they are both positive in either 2's complement or unsigned representations, because the MSB is 0. (A 1 in the MSB would mean the number was negative in 2's complement)
First value = 613,566,756
Second value = 1,606,303,744

2.7.3 what are the equivalent hexadecimal representations?
Conveniently, these numbers are already grouped into 4-bit clusters

    0010 0100 1001 0010 0100 1001 0010 0100
0x  2      4     9      2     4     9      2     4 = 0x24924924

    0101 1111 1011 1110 0100 0000 0000 0000
0x  5      f     b      e     4     0      0     0 = 0x5fbe4000

 

For the following decimal numbers:

-1
1024

2.7.4 Convert each to 2’s complement binary representation
To start, know that +1 = 0000 0000 0000 0000 0000 0000 0000 0001
To take the 2's complement, first flip all the bits: 1111 1111 1111 1111 1111 1111 1111 1110
Then add 1; thus -1 = 1111 1111 1111 1111 1111 1111 1111 1111 = 0xffffffff

+1024 =       0000 0000 0000 0000 0000 0100 0000 0000 = 0x00000400

2.7.5 Convert each to 2’s complement hexadecimal representation
See above

2.7.6 Negate each number (ie take 1 and -1024) and convert to 2’s complement hexadecimal 
From above, -1 = 1111 1111 1111 1111 1111 1111 1111 1111
To take the 2's complement, first flip all the bits: 0000 0000 0000 0000 0000 0000 0000 0000
Then add 1; thus +1 = 0000 0000 0000 0000 0000 0000 0000 0001 = 0x00000001

+1024 =       0000 0000 0000 0000 0000 0100 0000 0000
Flip all bits: 1111 1111 1111 1111 1111 1011 1111 1111
Then add 1; thus -1024 = 1111 1111 1111 1111 1111 1100 0000 0000 = 0xfffffc00