Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 20: 11/12/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.

Similar presentations


Presentation on theme: "Lecture 20: 11/12/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University."— Presentation transcript:

1 Lecture 20: 11/12/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 20: 11/12/2002

2 CS170 Fall 20022 Outline A more complex exampleProcedure sort (section 3.10) Arrays versus pointersSection 3.11

3 Lecture 20: 11/12/2002CS170 Fall 20023 sort procedure 1/4 void sort (int v[], int n) { int i, j; for (i=0; i < n; i = i+1) for (j = i-1 ; j >= 0 && v[j] > v[j+1] ; j = j –1) swap (v, j); } What is the generated MIPS assembly code? v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1 Outer for loop move $s0, $zero# i  0 for1tst:slt $t0,$s0,$a1#$t0  0 if $s0 >= $a1 beq $t0, $zero, exit1# if i >= n go to exit1 …. …..(body of loop) addi $s0,$s0,1# i = i +1 j for1tst# jump to test Exit1:

4 Lecture 20: 11/12/2002CS170 Fall 20024 sort procedure 2/4 Inner for loop addi $s1, $s0,-1# j = i-1 for2tst:slti $t0,$s1,0# $t0  1 if $s1 <0 bne $t0,$zero,exit2# if j <0 go to exit 2 add $t1,$s1,$s1# $t1  j *2 add $t1,$t1,$t1# $t1  j *4 add $t2,$a0,$t1# $t2  v + (j*4) lw $t3, 0($t2)#$t3  v[j] lw $t4,4($t2)#$t4  v[j+1] slt $t0,$t4,$t3# $t0  0 if $t4 >= $t3 beq $t0, $zero,exit2 # if $t4 >= $t3 go to exit2 …. addi $s1.$s1,-1# j = j-1 j for2tst# jump to test Exit2: void sort (int v[], int n) { …… for (j = i-1 ; j >= 0 && v[j] > v[j+1] ; j = j –1) swap (v, j); } What is the generated MIPS assembly code? v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1

5 Lecture 20: 11/12/2002CS170 Fall 20025 sort procedure 3/4 Procedure call and parameter passing ….. move $s2,$a0# copy $a0 into $s2 move $s3,$a1# copy $a1 into $s3 ….. move $a0, $s2 #first swap parameter is v move $a1,$s1 # second swap parameter is j jal swap Preserving registers in sort ($ra,$s0-$s3) addi $sp,$sp,-20# make room for 5 regs sw $ra, 16($sp)# save $ra on stack sw $s3,12($sp)# save $s3 on stack.. … void sort (int v[], int n) { …… for (j = i-1 ; j >= 0 && v[j] > v[j+1] ; j = j –1) swap (v, j); } What is the generated MIPS assembly code? v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1 swap needs parameters in $a0, $a1, then sort must save these registers before calling swap. An efficient solution stores $a0, $a1 in other registers instead of saving on stack

6 Lecture 20: 11/12/2002CS170 Fall 20026 sort procedure 4/4

7 Lecture 20: 11/12/2002CS170 Fall 20027 Arrays versus Pointers 1/2 void clear1 (int array[], int size) { int i; for ( i = 0; i < size ; i = i+1) array[i] = 0; } What is the generated MIPS assembly code? void clear2 (int *array, int size) { int *p; for ( p = &array[0]; p < &array[size] ; p =p+1) *p = 0; } What is the generated MIPS assembly code? move $t0, $zero #i = 0 Loop1:add $t1, $t0,$t0 #$t1 = i*2 add $t1,$t1,$t1 #$t1= i *4 add $t2,$a0,$t1 # $t2 = array + 4 *I sw $zero,0($t2) #array[i] = 0 addi $t0,$t0,1 # i = i+1 slt $t3,$t0,$a1 # $t3 = 1 if i < size bne $t3,$zero,loop1 # go to loop1 if more iterations #This code works as long as size is greater than zero move $t0, $a0 #p = address of array[0] Loop2:sw $zero 0($t0) # Memory[p] = 0 addi $t0, $t0,4 # p = p +4 add $t1,$a1,$a1 # $t1  size * 2 add $t1,$t1,$t1 # $t1  size*4 add $t2,$a0,$t1 #$t2  address of array[size] slt $t3,$t0,$t2 #$t3 = 1 if (p < &array[size]) bne $t3,$zero,loop2 # go to loop2 if more iterations #This code works as long as size is greater than zero

8 Lecture 20: 11/12/2002CS170 Fall 20028 Arrays versus Pointers 2/2 void clear1 (int array[], int size) { int i; for ( i = 0; i < size ; i = i+1) array[i] = 0; } What is the generated MIPS assembly code? void clear2 (int *array, int size) { int *p; for ( p = &array[0]; p < &array[size] ; p =p+1) *p = 0; } What is the generated MIPS assembly code? move $t0, $zero #i = 0 Loop1:add $t1, $t0,$t0 #$t1 = i*2 add $t1,$t1,$t1 #$t1= i *4 add $t2,$a0,$t1 # $t2 = array + 4 *I sw $zero,0($t2) #array[i] = 0 addi $t0,$t0,1 # i = i+1 slt $t3,$t0,$a1 # $t3 = 1 if i < size bne $t3,$zero,loop1 # go to loop1 if more iterations #This code works as long as size is greater than zero A more optimized solution move $t0, $a0 #p = address of array[0] add $t1,$a1,$a1 # $t1  size * 2 add $t1,$t1,$t1 # $t1  size*4 add $t2,$a0,$t1 #$t2  address of array[size] Loop2:sw $zero 0($t0) # Memory[p] = 0 addi $t0, $t0,4 # p = p +4 slt $t3,$t0,$t2 #$t3 = 1 if (p < &array[size]) bne $t3,$zero,loop2 # go to loop2 if more iterations #This code works as long as size is greater than zero


Download ppt "Lecture 20: 11/12/2002CS170 Fall 20021 CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University."

Similar presentations


Ads by Google