CSCI 255 Homework 11

This assignment is due in class or to my office assistant in RRO 215 by 9:30 AM on 29 November.

Assume the following prototype has been given to you.

int haha(int joke) ;

In this problem we want you to implement the following pointless function in PIC 24 assembly language.

int hehe(int i, int *p) {
  return i + haha(*p) ;
}

You can use the calling conventions described in the lecture notes to answer these questions.

Problem 1 — Parameter passing

Which registers are used to:

  1. Pass the first parameter to haha.
  2. Pass the first parameter to hehe.
  3. Pass the second parameter to hehe.

Problem 2 — Returned value

Which register is used to return:

  1. The result of both haha and hehe.

Problem 3 — Stack format

Define a stack frame appropriate for implementing hehe. The only thing you really need to save on the stack is a copy of i. That’s the only thing used after the return from haha.

Problem 4 — Function prologue

What are the two PIC24 instructions needed to implement the function prologue for hehe. The opcodes for the two are LNK and PUSH. The PUSH is used to save a copy of the first parameter, i.

Problem 5 — Expression evaluation

How is the pointer dereference performed inside hehe? Think about array references in Java.

Problem 6 — Function invocation

What are the two instructions are needed to call haha. One is a MOV to load the parameter and the other is an RCALL to transfer to haha.

Problem 7 — Function return

On return from haha your program needs to add i to the value returned by hehe. It must then compute its own return value and store it appropriately. You can do this in two instructions, a MOV and an ADD. The MOV must load the saved copy of i from the stack.

Problem 8 — Function epilogue

What is the function epilogue for hehe? It’s two instructions and neither requires an operand.

Testing it out

You don’t need to test you solution, but I tested mine.

If you completed the problem above correctly, you should have an PIC24 assembly implementation of haha that has about eight instructions.

If you want to test your program, use the following as your main program.

#include <stdlib.h>
#include <stdio.h>

#include <stdint.h>


// Prototype for your function
int hehe(int, int*) ;

// It really doesn't matter what it is
int haha(int joke) {
    return joke+2012 ;
}

int main(void) {
    int u = 202 ;
    int v = 255 ;
    int w = hehe(u, &v) ;        // w should be 2469
    printf("%d\n", w) ;          // Just to shut up the compiler
    return (EXIT_SUCCESS);
}

Because it is so hard to set up the assembly program correctly, start with the following template and place your code at the indicated locations.


#include "p24Hxxxx.inc"

       .extern  _haha
       .global  _hehe
_hehe:

;; Problem 4 goes here

;; Problem 6 goes here

;; Problem 7 goes here

;; Problem 8 goes here

       .end