chapter 3 - islamic university of gazasite.iugaza.edu.ps/ehabib/files/ca-ch35.pdf5 computer...

12
Islamic University – Gaza Engineering Faculty Department of Computer Engineering ECOM 3010: Computer Architecture Discussion Chapter 3 Exercises with solutions Eng. Eman R. Habib October, 2013

Upload: others

Post on 25-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

Islamic University – Gaza Engineering Faculty

Department of Computer Engineering ECOM 3010: Computer Architecture Discussion

Chapter 3

Exercises with solutions

Eng. Eman R. Habib

October, 2013

Page 2: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

2 Computer Architecture Discussion

Exercise 1: Perform the following conversions:

a) Convert Binary Number “11010010111010101001010010110001” to Hexadecimal Number. 1101 0010 1110 1010 1001 0100 1011 0001 D 2 E A 9 4 B 1

D2EA94B1

b) Convert Decimal Value “-26” to 32-bit Sign/Magnitude.

26 16+8+2 26 0000 0000 0000 0000 0000 0000 0001 1010 The number is negative so the last bit is “1” -26 1000 0000 0000 0000 0000 0000 0001 1010

10000000000000000000000000011010

c) Convert 32-bit 2’s Complement “11111111111111111111111010011011” to Decimal

Value. The last bit is “1” so the number is negative 1111 1111 1111 1111 1111 1110 1001 1011 0000 0000 0000 0000 0000 0001 0110 0101 1+4+32+64+256 = 357

-357

d) Convert 32-bit 2’s Complement “00000000000000000000000001010101” to Decimal

Value. The last bit is “0” so the number is positive 1+4+16+64 = 85

85

e) Convert 8-bit Unsigned “10000111” to Decimal Value

1+2+4+128 = 135

135

Page 3: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

3 Computer Architecture Discussion

f) Convert 32-bit Sign/Magnitude “10000000000000000000000000001011” to Decimal Value 1+2+8 = 11 The last bit is “1” so the number is negative

-11

g) Convert Decimal Value “-2047” to 32-bit 2’s Complement

2047 = 211 – 1 0000 0000 0000 0000 0000 0111 1111 1111 1111 1111 1111 1111 1111 1000 0000 0001

11111111111111111111100000000001

h) Convert Decimal Value “7” to 32-bit 2’s Complement

0000 0000 0000 0000 0000 0000 0000 0111

00000000000000000000000000000111

i) Convert Decimal Value “-7” to 8-bit Sign/Magnitude

The number is negative so the last bit is “1” 1000 0111

10000111

j) Convert Decimal Value “77” to 8-bit Sign/Magnitude

64+8+4+1 The number is positive so the last bit is “0” 0100 1101

0100 1101

k) Convert Decimal Value “26” to 5-bit Unsigned

11010

Page 4: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

4 Computer Architecture Discussion

Exercise 2: (3.7 from book)

addu $t2, $zero, $t3 # copy $t3 into $t2

stl $t0, $t3, $zero # if $t3 < 0; $t3 is negative then $t0 = 1

beq $t0, $zero, Done # if $t0 = 0 then done; $t3 is positive

sub $t2, $zero, $t3 # negate $t3 and place into $t2

Done:

رح اطرحو من سالباما اذا كان Doneو اطلع على t2$رح حطو برجستر 0عن اكبر من موجباذا كان الرقم t2$الو و بحطو برجستر s complement’2زرو هك بكون جبت

Exercise 3: (3.9 from book)

1البت الل بعدو 11حصر ال 1حصرلها سان اكستندد عن لو كان اخر بت A_lowerالمشكلة بهاد الحل انو .Aو طبعا رح طلع رقم تان غر t0$بعدن بتنجمع مع

بس لش؟؟؟ A_lower 1اذا كان اخر بت من 1بانو اجمعلها A_upperالحل انو اعدل على 11111001و حصرلو سان اكستندد A_lower 1001حكون 01001001بت مثال 8النو لو جربت على رقم

مع 1عن لو جمعت A_upperو هادا الرقم المطلوب انو اجمعو مع 00001001حصر 10000لو جمعتلو A_upper النو بعد بكون عملت نفس الش 0101بصرlui حصر$t0 = 01010000 و بعدlw حنجمع$t0

t0 = 01010000+11111001 = 01001001$و صر بعد ما صرلو سان اكستندد A_lowerمع A_lowerبالكود حتكون الطرقة ه عالفاض النو الزمن اصال احط A_upperل 1بس لو فكرت انو اجمع

و ال الء و طبعا هك رح كون الحل طول فاالفضل انو اعمل الطرقة االولى. 1برجستر عشان افحص اذا اخر بت =

Page 5: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

5 Computer Architecture Discussion

Exercise 4: (3.10 from book)

امتا ممكن اعرف انو صار عندي كري لما اجمع رقمن؟ لو كان الناتج اصغر من اي رقم من الرقمن مع انو المفروض الرقمن معناتو ف كري فبدي افحص اذا الناتج اصغر من الرقم االول او التان:كون اكبر من

addu $t2, $t3, $t4

sltu $t2, $t2, $t3

or

addu $t2, $t3, $t4

sltu $t2, $t2, $t4

1- 232لو كان ناتج الجمع اكبر من اكبر رقم ممكن كون بالرجستر عن اكبر من بطرقة تانة انو كمان ممكن اعرف

بس كف بدي افحص اذا الناتج اكبر من هاد الرقم؟ 232 -1 < $t4+$t3

على الجهة التانة t3$لو نقلت 232-$t3-1 < $t4

ل s complement’2ها t3$-232النو t3$ل s complement’1عن not $t3هو t3-1$-232هأل هاد $t3 1بتصر 1و لما انقص’s complement

بالمبس notبس كف بدي اعمل not $t3 = $t3 nor 0

فحصر الحل:nor $t1, $t3, $zero # not $t3 2

32-$t3-1

sltu $t 2, $t1, $t4 # 232-$t3-1 < $t4 2

32 -1 < $t4+$t3

Exercise 5: (3.12 from book)

ممكن اطرحهم من بعض و اشوف اشارة الناتج بس حكون عنا مشكلة النو ممكن طلع من s0 < $s1$عشان افحص و هك مش رح طلع unsignedالطرح اوفرفلو و صر اكسبشن ممكن انحلها بطرقتن: االولى انو اطرحهم كارقام

طرقة التانة انو افحص اشارات اكسبشن و بعدن بفحص اذا صار اوفرفلو او الء بس ه الطرقة طولة عالفاض. المختلفن باالشارة s1$ و s0$ عن نفس االشارة(s1$–) و s0$ اذا كان ممكن طلع اوفرفلو بساالرقام باالول النو

بس به الحالة اصال مش محتاجة اطرحهم عشان اشوف من اصغر النو لو مختلفن فالرقم السالب هو اكد االصغر و اذا كانو متشابهن وقتها بطرح و بشوف اشارة الناتج لو سالب حكون الرقم االول اصغر و لو موجبة حكون التان االصغر.

Page 6: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

6 Computer Architecture Discussion

بعدن بعمل شفت 0و لو متشابهن حكون 1النو لو مختلفن حكون اخر بت xorعشان افحص اذا مختلفن بعمل متشابهن وقتها بدي اطرح و اعمل معناتوبت عشان صر البت االخر هو بس بالرجستر و لو كان زرو 11رات

اصغر, اما مشحكون 0ول اصغر و لو رجستر االكون الح 1لو t1$شفت رات للناتج فحتصر االشارة بالرجستر عن هو االصغر و لو موجب t0 1$لو مختلفن حعمل شفت رات الول رجستر لو كانت اشارتو سالبة حصر ب

عن مش اصغر. t0 0$حصر ب

The solution in MIPS:

xor $t1,$s0,$s1 # if $s0 and $s1 differ in sign, the last

bit of $t1 will be ‘1’

srl $t1,$t1,31 # take the value of the last bit

beq $t1,$zero,same # if the $t1 is zero, they share the same

sign

srl $t0,$s0,31 # if differ, take the sign of $s0 if ‘1’

that mean it’s negative so it is smaller, if

‘0’ that mean it’s positive so $s1 is

smaller

j exit

same:

sub $t0,$s0,$s1 # $s0 and $s1 share the same sign, now

subtract, no overflow will occurs

srl $t0,$t0,31 # if $t0 is ‘1’ the result is negative so

$s0 < $s1

exit:

Exercise 6: (3.27 from book)

a. x+y

0000 0000 0000 0000 0000 0000 0101 1011 + 0000 0000 0000 0000 0000 0000 0000 1101 = 0000 0000 0000 0000 0000 0000 0110 1000

Page 7: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

7 Computer Architecture Discussion

b. x-y

0000 0000 0000 0000 0000 0000 0101 1011 - 0000 0000 0000 0000 0000 0000 0000 1101 = 0000 0000 0000 0000 0000 0000 0101 1011 + 1111 1111 1111 1111 1111 1111 1111 0011 = 0000 0000 0000 0000 0000 0000 0100 1110

c. x*y

Iteration Step Multiplier Multiplicand Product

0 Initial value 0000 1101 0101 1011 0000 0000

1 1Product=Product+ Multiplicand 0000 1101 0101 1011 0101 1011

Shift left Multiplicand 0000 1101 0 1011 0110 0101 1011

Shift left Multiplier 0000 0110 0 1011 0110 0101 1011

2 0no operation 0000 0110 0 1011 0110 0101 1011

Shift left Multiplicand 0000 0110 01 0110 1100 0101 1011

Shift left Multiplier 0000 0011 01 0110 1100 0101 1011

3 1 Product=Product+ Multiplicand 0000 0011 01 0110 1100 1 1100 0111

Shift left Multiplicand 0000 0011 010 1101 1000 1 1100 0111

Shift left Multiplier 0000 0001 010 1101 1000 1 1100 0111

4 1 Product=Product+ Multiplicand 0000 0001 010 1101 1000 100 1001 1111

Shift left Multiplicand 0000 0001 0101 1011 0000 100 1001 1111

Shift left Multiplier 0000 0000 0101 1011 0000 100 1001 1111

x*y = 0000 0000 0000 0000 0000 0100 1001 1111

Page 8: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

8 Computer Architecture Discussion

d. x/y

Iteration Step Quotient Divisor Reminder

0 Initial value 0000 0000 1101 0000 0101 1011

1 Rem = Rem - Div 0000 0000 1101 0000 1000 1011

Rem<0 +Div, sll Q, Q0=0 0000 0000 1101 0000 0101 1011

Shift Div right 0000 0000 0110 1000 0101 1011

2 Rem = Rem - Div 0000 0000 0110 1000 1111 1011

Rem<0 +Div, sll Q, Q0=0 0000 0000 0110 1000 0101 1011

Shift Div right 0000 0000 0011 0100 0101 1011

3 Rem = Rem - Div 0000 0000 0011 0100 0010 0111

Rem>0 sll Q, Q0=0 0000 0001 0011 0100 0010 0111

Shift Div right 0000 0001 0001 1010 0010 0111

4 Rem = Rem - Div 0000 0001 0001 1010 0000 1101

Rem>0 , sll Q, Q0=1 0000 0011 0001 1010 0000 1101

Shift Div right 0000 0011 0000 1101 0000 1101

5 Rem = Rem - Div 0000 0011 0000 1101 0000 0000

Rem>0 , sll Q, Q0=1 0000 0111 0000 1101 0000 0000

Shift Div right 0000 0111 0000 0110 0000 0000

x/y = 0000 0000 0000 0000 0000 0000 0000 0111

Exercise 7: (3.37 from book)

20 = 10100 = 1.0100 * 24

Sign = 0 Fraction = 0100 Single exponent = 4 + 127 = 131 Double exponent = 4 + 1023 = 1027 Single precision: 1 8 23

0 1000 0011 010 0000 0000 0000 0000 0000

Double precision: 1 11 52

0 1000 0000 011 0100 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

Page 9: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

9 Computer Architecture Discussion

Exercise 8: Suppose you decide to create a variant on IEEE floating point numbers you call a quarter. It has all the properties of IEEE 754 (including denorms, NaNs and ± ∞) just with different ranges, precision & representations. A quarter is a single byte split into the following fields (1 Sign, 3 Exponent, 4 Fraction): 1 3 4

Sign Exponent Fraction

a) What is the bias?

(2n-1)-1 = 23-1 – 1 = 22 – 1 = 3 n: number of Exponent bits.

b) What is the largest number smaller than ∞?

0 110 1111

(-1)0(1+1-2-4)26-3 = (2-2-4)23 = (1-2-5)24 = (0.96875)16 = 15.5

c) What is the smallest positive normalized number (closest to 0)?

0 001 0000

(-1)0(1+0)21-3 = 2-2 = 0.25

d) What is the negative denormalized number that is closest to 0 (but not -0)?

1 000 0001

(-1)1(2-4)20-3+1 = - (2-4)2-2 = -2-6 = 0.015625

e) What is the largest positive denormalized number?

0 000 1111

(-1)0(1-2-4)20-3+1 = (1-2-4)2-2 = 0.234375

f) Represent 0 using the design above?

0 000 0000

Page 10: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

10 Computer Architecture Discussion

g) What is the equivalent value of the following number?

0 111 0110

Exponent = 7, Fraction = non zero It is NaN

h) Represent -∞ using the design above?

1 111 0000

i) Represent 13 using the design above?

13 = 1101 = 1.101 * 23

Sign = 0, Fraction = 1010, Exponent = 3+3 =6

0 110 1010

j) Represent -0.875 using the design above?

-0.875 = -(0.5+0.25+0.125) = -(2-1 + 2-2 + 2-3) = -0.111 = -1.11 * 2-1

Sign = 1, Fraction = 1100, Exponent = -1+3 =2

1 010 1100

k) What is the value of the 3rd smallest denormalized float?

0 000 0011

(-1)0(0.0011)20-3+1 = (2-3+2-4)2-2 = 0.046875

l) What is the next largest (closest to it but [closer to +∞] than it) number that can be represented?

11100000 Sign = 1, Fraction = 0000, Exponent = 110 Next largest Sign = 1, Fraction = 0000 – 1 = 1111, Exponent = 110 – 1 = 101

1 101 1111

(-1)1(1+1-2-4)25-3 = -(2-2-4)22 = -(1-2-5)23= -7.75

من الفراكشن و النو الفراكشن زرو فحلزم 1النو الرقم سالب فالرقم ال اكبر منو حكون اصغر منو بالقمة فالزم انقص من الفراكشن بس وما بنقص من االكسبوننت. 1اما لو كان الفراكشن مش زرو فبنقص 1انقص من االكسبوننت

Page 11: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

11 Computer Architecture Discussion

Exercise 9:

x: Sign = 0 Exponent = 10001101 = 141 Fraction = 10110000000000000000000 x = (-1)0 (1+0.1011)2141-127 = 1.1011 * 214

y: Sign = 1 Exponent = 01111101 = 125 Fraction = 11000000000000000000000 y = (-1)1 (1+0.11)2125-127 = -1.11 * 2-2

a) x+y

Step 1: lesser exponent larger exponent

y = -1.11 * 2-2 = -0.0000 0000 0000 0001 11 * 214

Step 2: add the significands:

1.1011 + (-0.0000 0000 0000 0001 11)

1.1011 0000 0000 0000 0000 000 - 0.0000 0000 0000 0001 1100 000

= 1.1010 1111 1111 1110 0100 000

Step 3: Normalize, no change

Step 4: Round, no change

Sign = 0 Exponent = 14+127 = 141 = 10001101 Fraction = 1010 1111 1111 1110 0100 000 0100 0110 1101 0111 1111 1111 0010 0000

0 10001101 1010 1111 1111 1110 0100 000

Page 12: Chapter 3 - Islamic University of Gazasite.iugaza.edu.ps/ehabib/files/CA-ch35.pdf5 Computer Architecture Discussion Exercise 4: (3.10 from book) ضورفملا ونا عم ن مقرلا

12 Computer Architecture Discussion

b) x * y

Step 1: Add the exponents:

14+(-2) = 12

Step 2: Multiply the significands:

1.1011 *

1.11 = 11011 + 11011 + 11011 = 10111101 The product is 10.111101

Step 3: Normalize the product

1.0111101 * 21

Step 4: Round, no change

Step 5: The sign of operands differ, so the sign of the product is negative

Sign = 1 Exponent = 1+12+127 = 140 = 10001100 Fraction = 011 1101 0000 0000 0000 0000

1100 0110 0011 1101 0000 0000 0000 0000

1 10001100 011 1101 0000 0000 0000 0000

Best Wishes