1 / 8

CS170 Computer Organization and Architecture I

Learn to generate MIPS assembly code from complex C examples, focusing on arrays vs. pointers and efficient parameter passing techniques.

vincentl
Download Presentation

CS170 Computer Organization and Architecture I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Outline • A more complex example Procedure sort (section 3.10) • Arrays versus pointers Section 3.11 CS170 Fall 2002

  3. sort procedure1/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? 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: v, n assigned to $a0, $a1 i, j assigned to registers $s0, $s1 CS170 Fall 2002

  4. sort procedure2/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 CS170 Fall 2002

  5. sort procedure3/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 CS170 Fall 2002

  6. sort procedure4/4 CS170 Fall 2002

  7. Arrays versus Pointers1/2 • 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? • 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? 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 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 CS170 Fall 2002

  8. Arrays versus Pointers2/2 • 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? • 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? 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 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 CS170 Fall 2002

More Related