Arrays, Pointers, Strings and Procedures in MIPS

Arrays and Pointers

The first procedure involving array is named clear1() given as follows:

  clear1(int array[], int size)
 {
     int i;
     for (i=0; i < size; i=i+1)
         array[i] = 0;
 }
 

###############
## clear1.a
###############

  .text
  .globl __start

__start:
      la $a0, array
      lw $a1, size

      move $t0, $0         #i=0
 
loopl:  sll $t1, $t0, 2    #$t1=i*4
        add $t2, $a0, $t1     #$t2=&array[i]

      sw  $0, 0($t2)       #array[i]=0

      addi $t0, $t0, 1     #i = i+1
      slt  $t3, $t0, $a1   #$t3 = (i<size)
      bne  $t3, $0, loopl  #if(i<size) go to loopl
    
      li  $v0, 10
      syscall

  .data
array:  .word 34 55 44 22
size:   .word 4
   
######################
## endof file clear1.a
#####################
The second procedure involving pointer is named clear2() given as follows:

 clear2(int *array, int size)
 {
    int *p;
    for (p=&array[0];p < &array[size];p=p+1)
         *p = 0;
 }
 

###############
## clear_p1.a
###############

  .text 
  .globl __start

__start:
   la $a0, array
   lw $a1, size

   move $t0, $a0        #p = address of array[0]
 
loop2:    sw  $0, 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 = (p<&array[size])
          bne $t3, $0, loop2  #if(p<&array[size]) go to loop2

     li  $v0, 10
     syscall
 

        .data
array:  .word 34 55 44 22
size:   .word 4
   
######################
## endof file clear_p1.a
#####################
###############
## clear_p2.a
###############

  .text 
  .globl __start

__start:
   la $a0, array
   lw $a1, size

      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 $0, 0($t0)      #Memory[p] = 0
           addi $t0, $t0, 4     #p =p + 4
           slt $t3, $t0, $t2   #$t3 = (p<&array[size])
           bne $t3, $0, loop2  #if(p<&array[size]) go to loop2

     li  $v0, 10
     syscall
 

   .data
array:  .word 34 55 44 22
size:   .word 4
   
######################
## endof file clear_p2.a
#####################

Strings


void strcpy(char x[], char y[])
{
   int i;
   i=0;

   while((x[i] = y[i]) !=0)    /* copy and test byte */
         i = i+1;  
}

MIPS assembly code 

Base addresses for arrays x and y are found in $a0 and $a1.
i is in register $s0. strcpy adjusts the stack pointer and then 
saves the saved register $s0 on the stack. 

      la  $a0, x
      la  $a1, y 

strcpy:  
      addi $sp, $sp, -4    # adjust stack for 1 more item 
      sw  $s0, 0($sp)    # save $s0

      add $s0, $zero, $zero   #i = 0 + 0
      
L1:   add $t1, $a1, $s0  # address of y[i] in $t1
      lb  $t2, 0($t1)    # $t2 = y[i]
      

      add $t3, $a0, $s0  # address of x[i] in $t3
      sb  $t2, 0($t3)    # x[i] = y[i] 
  
      beq $t2, $zero, L2 # if y[i] == 0, go to L2
      addi $s0, $s0, 1    # i = i + 1
      j  L1              #  go to L1

L2:   lw  $s0, 0($sp)    # y[i] == 0; end of string 
      addi $sp, $sp, 4    # restore old s0
                    # pop 1 word off stack
      jr  $ra            # return 
          


Procedures

int leaf_example (int g,int h, int i, int j){
	int f;
	f=(g+h)-(i+j);
	return f;
}
MIPS assembly code 

The parameter variables g,h,i,j corresponds to the argument registers $a0, $a1,$a2,$a3, and f corresponds to $s0.

leaf_example:
     addi $sp, $sp,-12       #adjust stack $sp make room for 3 items
     sw $t1, 8($sp)          #save register $t1 to use after the procedure
     sw $t0, 4($sp)          #save register $t0 to use after the procedure
     sw $s0, 0($sp)          #save register $s0 to use after the procedure

     add $t0, $a0, $a1        #$t0 contains g+h	
     add $t1, $a2, $a3        #$t1 contains i+j
     sub $s0, $t0, $t1        #f=$t0-$t1, which is (g+h)-(i+j)
     add $v0, $s0, $zero      # returns f ($v0=$s0+0)

     lw $s0, 0($sp)         #restore $s0 for caller
     lw $t0, 4($sp)         #restore $t0 for caller
     lw $t1, 8($sp)         #restore $t1 for caller

     add $sp, $sp, 12       #adjust stack to delete 3 items
     jr $ra                 #jump back to calling routine