Key to assignment #5

advertisement
Chapter 4: problems 1,2,4,6,10,21,22
John Visser
CS 2560
Assignment #5 Key
4.1 Convert 512ten into a 32-bit two’s complement binary number.
51210 = 29 = 0000 0000 0000 0000 0000 0010 0000 0000
4.2 Convert –1023ten into a 32-bit two’s complement binary number.
102310 = 210 – 1 = 10000000000 – 1 = 0000 0000 0000 0000 0000 0011 1111 1111
flip the bits and add one: = 1111 1111 1111 1111 1111 1100 0000 0001
4.4 What decimal number does this two’s complement number represent?
flip the bits and add one:
1111 1111 1111 1111 1111 1110 0000 1100
0000 0000 0000 0000 0000 0001 1111 0011 + 1
0000 0000 0000 0000 0000 0001 1111 0100
1111101002 = 28 + 27 + 26 + 25 + 24 + 22 = 500 ten, i.e., - 500ten considering the sign bits.
4.6 What decimal number does this two’s complement number represent?
0111 1111 1111 1111 1111 1111 1111 1111 = 231 – 1 = 2,147,483,647ten
4.10 Find the shortest sequence of MIPS instructions to determine the absolute value of a two’s
complement integer:
abs
$t2, $t3
addu $t2, $zero, $t3
bgez $t2, 8
sub $t2, $zero, $t3
# move the number into t2
# if number is positive or 0, skip next instruction
# t2 = 0 – t3 = complement of t3
# because t3 is negative, t2 will contain the abs(t3)
4.21 Suppose we want to put 5 times the value of $s0 into $s1, ignoring any overflow that may occur.
Show a minimal sequence of MIPS instructions for doing this without using a multiply instruction.
5 = 1012, so we need to take $s0, copy it into $s1, shift the $s0 two places to the left, and then
add that number to the value in $s1.
sll
addu
$s1, $s0, 2
$s1, $s0, $s1
# multiply s0 by 4 (shift left two places because 4 = 1002)
# s1 = 1 x s0 + 4 x s0 = 5 x s0
4.22 Find the shortest sequence of instructions that extracts bits 8 through 19 from $s0 and places
them in the least-significant bits of $s1: (8th - 19th bits are in red, shift amount in blue.)
sll $s1, $s0, 12
srl $s1, $s2, 20
# 1111 1111 1111 1111 1111 1111 1111 1111
# remove 12 most-significant bits, (add 12 zeroes to the right end)
# 1111 1111 1111 1111 1111 0000 0000 0000
# shift right so that 8th bit becomes 0th bit.
# 0000 0000 0000 0000 0000 1111 1111 1111
or:
srl $s1, $s0, 8
andi $s1, $s1, 0xFFF
# shift bits 19..8 to the far right
# and remove all other bits
Download