02~solutions_for_chapter_2_exercises

02~solutions_for_chapter_2_exercises - Solutions for...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Solutions for Chapter 2 Exercises 1 Solutions for Chapter 2 Exercises 2.2 By lookup using the table in Figure 2.5 on page 62, 7fff fffa hex = 0111 1111 1111 1111 1111 1111 1111 1010 two = 2,147,483,642 ten . 2.3 By lookup using the table in Figure 2.5 on page 62, 1100 1010 1111 1110 1111 1010 1100 1110 two = cafe face hex . 2.4 Since MIPS includes add immediate and since immediates can be positive or negative, subtract immediate would be redundant. 2.6 sll $t0, $t3, 9 # shift $t3 left by 9, store in $t0 srl $t0, $t0, 15 # shift $t0 right by 15 2.8 One way to implement the code in MIPS: sll $s0, $s1, 22 # shift receiver left by 22, store in data srl $s0, $s0, 24 # shift data right by 24 (data = receiver.receivedByte) andi $s1, $s1, 0xfffe # receiver.ready = 0; ori $s1, $s1, 0x0002 # receiver.enable = 1; Another way: srl $s0, $s1, 2 # data = receiver.receivedByte andi $s0, $s0, 0x00ff andi $s1, $s1, 0xfffe # receiver.ready = 0; ori $s1, $s1, 0x0002 # receiver.enable = 1; 2.9 lb $s0, 0($sl) # load the lower 8 bytes of a into bits sll $t0, $s0, 8 # $t0 = bits << 8 or $s0, $s0, $t0 # bits.data1 = bits.data0 lui $s0, 0000 0000 0110 0100 # bits.data2 = ‘d’ lui $t0, 0000 0001 0000 0000 # load a 1 into the upper bits of $t0 or $s0, $s0, $t0 # bits.valid = 1 2 Solutions for Chapter 2 Exercises 2.10 slt $t3, $s5, $zero # test k < 0 bne $t3, $zero, Exit # if so, exit slt $t3, $s5, $t2 # test k < 4 beq $t3, $zero, Exit # if not, exit sll $t1, $s5, 2 # $t1 = 4*k add $t1, $t1, $t4 # $t1 = &JumpTable[k] lw $t0, 0($t1) # $t0 = JumpTable[k] jr $t0 # jump register L0: add $s0, $s3, $s4 # k == 0 j Exit # break L1: add $s0, $s1, $s2 # k == 1 j Exit # break L2: sub $s0, $s1, $s2 # k == 2 j Exit # break L3: sub $s0, $s3, $s4 # k == 3 j Exit # break Exit: 2.11 a. if (k==0) f = i + j; else if (k==1) f = g + h; else if (k==2) f = g - h; else if (k==3) f = i - j; Solutions for Chapter 2 Exercises 3 b. bne $s5, $0, C1 # branch k != 0 add $s0, $s3, $s4 # f = i + j j Exit # break C1: addi $t0, $s5, -1 # $t0 = k - 1 bne $t0, $0, C2 # branch k != 1 add $s0, $s1, $s2 # f = g + h j Exit # break C2: addi $t0, $s5, -2 # $t0 = k - 2 bne $t0, $0, C3 # branch k != 2 sub $s0, $s1, $s2 # f = g - h j Exit # break C3: addi $t0, $s5, -3 # $t0 = k - 3 bne $t0, $0, Exit # branch k != 3 sub $s0, $s3, $s4 # f = i - j Exit: c. The MIPS code from the previous problem would yield the following results: (5 arithmetic)1.0 + (1 data transfer)1.4 + (2 conditional branch)1.7 + (2 jump)1.2 = 12.2 cycles while the MIPS code from this problem would yield the following: (4 arithmetic)1.0 + (0 data transfer)1.4 + (4 conditional branch)1.7 + (0 jump)1.2 = 10.8 cycles 2.12 The technique of using jump tables produces MIPS code that is independent of N, and always takes the following number of cycles: (5 arithmetic)1.0 + (1 data transfer)1.4 + (2 conditional branch)1.7 + (2 jump)1.2 = 12.2 cycles However, using chained conditional jumps takes the following number of cycles in a worst-case scenario: ( N arithmetic)1.0 + (0 data transfer)1.4 + (arithmetic)1....
View Full Document

This note was uploaded on 04/20/2010 for the course IE ie200 taught by Professor . during the Spring '10 term at 카이스트, 한국과학기술원.

Page1 / 21

02~solutions_for_chapter_2_exercises - Solutions for...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online