Fall 2014 CSCI 255 Homework 11

This assignment is due in class on 5 November or may be uploaded to Homework 11 on moodle by 2:00 PM on 5 November.

The Problem

Here is a rather odd C routine. It a recursive function that has been written to have only one return statement and one recursive invocation.

unsigned int GCD(unsigned int m, unsigned int n) {
    int r ;
    if (n==m) {
        r = n ;
    } else {
        int arg1; 
        int arg2;
        if (n<m) {
            arg1 = m ;
            arg2 = n ;
        } else {
            arg1 = n-m ;
            arg2 = m ;
        }
        r = GCD(arg1, arg2) ;
    }
    return r ;
}

When I ran this through the XC32 compiler, it produced a MIPS32 assembly program from which I removed a lot of fluff.

      .align  2
      .globl  GCD
      .ent    GCD
      .type   GCD, @function
GCD:
      .frame  $fp,40,$31
      .mask   0xc0000000,-4
      .fmask  0x00000000,0
      .set    noreorder
# Ignore stuff before this line	

      addiu   $sp,$sp,-40
      sw      $ra,36($sp)
      sw      $fp,32($sp)
      move    $fp,$sp
      sw      $a0,40($fp)
      sw      $a1,44($fp)
      lw      $v1,44($fp)
      lw      $v0,40($fp)
      bne     $v1,$v0,.L2
      nop
      lw      $v0,44($fp)
      sw      $v0,16($fp)
      j      .L3
      nop
.L2:
      lw      $v1,44($fp)
      lw      $v0,40($fp)
      sltu    $v0,$v1,$v0
      beq     $v0,$0,.L4
      nop
      lw      $v0,40($fp)
      sw      $v0,20($fp)
      lw      $v0,44($fp)
      sw      $v0,24($fp)
      j       .L5
      nop
.L4:
      lw      $v1,44($fp)
      lw      $v0,40($fp)
      subu    $v0,$v1,$v0
      sw      $v0,20($fp)
      lw      $v0,40($fp)
      sw      $v0,24($fp)
.L5:
      lw      $v1,20($fp)
      lw      $v0,24($fp)
      move    $a0,$v1
      move    $a1,$v0
      jal     GCD
      nop
      sw      $v0,16($fp)
.L3:
      lw      $v0,16($fp)
      move    $sp,$fp
      lw      $ra,36($sp)
      lw      $fp,32($sp)
      addiu   $sp,$sp,40
      j       $ra
      nop

# Ignore stuff after this line	
      .end    GCD
      .size   GCD, .-GCD

Your task

Your task is to annotate the MIPS32 code. You should mention where the variables are stored on the stack and point out the prologue, epilogue, function call, and function returns sections of the code.

A picture of the stack frame would also be nice.