Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CDA 3101 Discussion Section 03.  Problem 2.6.1 The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and.

Similar presentations


Presentation on theme: "1 CDA 3101 Discussion Section 03.  Problem 2.6.1 The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and."— Presentation transcript:

1 1 CDA 3101 Discussion Section 03

2  Problem 2.6.1 The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the integer array A and B are in registers $s6 and $s7, respectively. a. f=-g + h + B[1]; b. f= A[ B[g] + 1 ]; For the C statements above, what is the corresponding MIPS assembly code? 2

3 a) lw $s0, 4($s7) sub $s0, $s0, $s1 add $s0, $s0, $s2 b) sll $t0, $s1, 2 add $t0, $s7, $t0 lw $t0, 0($t0) addi $t0, $t0, 1 sll $t0, $t0, 2 add $t0, $s6, $t0 lw $s0, 0($t0) 3

4  Problem 2.6.4 The following problems deal with translating from MIPS to C. Assume that the variables f, g, h, i and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address fo the array A and B are in registers $s6 and $s7, respectively. add $s0, $s0, $s1 add $s0, $s3, $s2 add $s0, $s0, $s3 For the MIPS assembly code above, what is the corresponding C statement? 4

5 add $s0, $s0, $s1 f = f+g; add $s0, $s3, $s2 f = i+h; add $s0, $s0, $s3 f = f+i; f = 2i + h; 5

6  Problem 2.15.5 A= B & c[0]; For the C statement above, write a minimal sequence of MIPS assembly instructions that does the identical operation? 6

7 Assuming $t1 = A, $t2 = B, $s1 = base of Array C lw $t3, 0($s1) and $t1, $t2, $t3 7

8  Problem 2.16.1 Suppose that register $t0 contains a value 1010 1101 0001 0000 0000 0000 0000 0010 two and $t1 has the value 0011 1111 1111 1000 0000 0000 0000 0000 two What is the value of $t2 after the following instructions? slt $t2, $t0, $t1 beq $t2, $zero, ELSE j DONE ELSE : addi $t2, $zero, 2 DONE: 8

9 $t2 = 1 9

10  Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. $a0: input a; $a1: input b; $v0: output; add$t0, $zero, $zero loop:beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 jloop finish:addi $t0, $t0, 100 add $v0, $t0, $zero 10

11  Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. $a0: input a; $a1: input b; $v0: output; add$t0, $zero, $zero loop:beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 jloop finish:addi $t0, $t0, 100 add $v0, $t0, $zero 11 # t=0; # if(b==0) go to finish # t = t+a, increase t by a # b = b-1, # go to loop # t = t+100 # $v0 = t, output

12  Problem 2.29 Add comments to the following MIPS code and describe in one sentence what it computes. Assume $a0, $a1 are input, initial value a, b; $v0 is output. add$t0, $zero, $zero loop:beq $a1, $zero, finish add $t0, $t0, $a0 sub $a1, $a1, 1 jloop finish:addi $t0, $t0, 100 add $v0, $t0, $zero 12 t=0; while(b!=0) { t = t + a; b = b – 1; } t = t + 100; v = t; So, v = a*b+100

13  Problem 2.30 Add comments to the following MIPS code and describe in one sentence what it computes. The code processes 2 arrays and produces a value in $v0. Each array consists of 2500 words indexed 0 through 2499, the base addresses of the arrays are stored in $a0, $a1, and their sizes are stored in $a2, $a3. What will be returned in $v0? 13

14  Problem 2.30 sll$a2, $a2, 2 sll$a3, $a3, 2 add$v0, $zero, $zero add$t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner:add$t3, $a1, $t1 lw$t3, 0($t3) bne$t3, $t4, skip addi$v0, $v0, 1 skip:addi $t1, $t1, 4 bne$t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 14

15  Problem 2.30 sll$a2, $a2, 2 sll$a3, $a3, 2 add$v0, $zero, $zero add$t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner:add$t3, $a1, $t1 lw$t3, 0($t3) bne$t3, $t4, skip addi$v0, $v0, 1 skip:addi $t1, $t1, 4 bne$t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 15 #a2=a2*4, size in byte; #a3=a3*4, # $v0=0, counter; # $t0=0, index of $a0 (x) i; # $t4 = x[i]; start of outer loop; # $t1=0, index of $a1 (y) j; # $t3 = y[j], inner loop # if x[i]==y[j], counter increment; # else compare x[i] with y[j+1], until j reach the end of y # go back to start point of outer loop. Compare x[i+1] with y, and count the number

16  Problem 2.30 sll$a2, $a2, 2 sll$a3, $a3, 2 add$v0, $zero, $zero add$t0, $zero, $zero outer:add $t4, $a0, $t0 lw $t4, 0($t4) add $t1, $zero, $zero inner:add$t3, $a1, $t1 lw$t3, 0($t3) bne$t3, $t4, skip addi$v0, $v0, 1 skip:addi $t1, $t1, 4 bne$t1, $a3, inner addi $t0, $t0, 4 bne $t0, $a2, outer 16 Assume $a0->x, $a1->y, it equals to a nested loop in C code: count = 0; for(i=0; i<2500; i++) { for(j=0; j<2500; j++) { if(x[i] == y[j]) count ++; } }


Download ppt "1 CDA 3101 Discussion Section 03.  Problem 2.6.1 The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and."

Similar presentations


Ads by Google