Your solution to this assignment, a PIC32 assembly language file called tooNegative.s, should be uploaded by Homework 10 on moodle by 11:00 PM on 5 November.
The Problem
This is your one-and-only assembly language programming project. You are to implement the following function in MIPS32 assembly language.
#include <stdlib.h>
#include "tooNegative.h"
int tooNegative(int V[], int n) {
int lowCount ;
int i ;
lowCount = 0 ;
for (i=0; i<n; ++i) {
if (V[i] < 0) {
V[i] = 0 ;
lowCount++ ;
}
}
return lowCount ;
}
Testing
You can test your routine using the following main routine.
#include <stdio.h>
#include <stdlib.h>
#include "tooNegative.h"
static void printResult(int a[], int n, int r) {
int i ;
fputs("Array:\n", stdout) ;
for (i=0; i<n; ++i) {
fprintf(stdout, "%3d: %6d\n", i, a[i]) ;
}
fprintf(stdout, "Returned: %3d\n\n", r) ;
}
int main(int argc, char** argv) {
int U[] = {255, -3, 5, 70, -101, 30 } ;
int V[] = {} ;
int u = tooNegative(U, sizeof U/sizeof(int)) ;
printResult(U, sizeof U/sizeof(int), u) ;
int v = tooNegative(V, sizeof V/sizeof(int)) ;
printResult(V, sizeof V/sizeof(int), v) ;
return (EXIT_SUCCESS);
}
You should use this prototype to compile the main routine.
#ifndef TOONEGATIVE_H #define TOONEGATIVE_H int tooNegative(int *, int) ; #endif /* TOONEGATIVE_H */
Getting started
You can download a an incomplete project for MPLAB X to get started. It has a file tooNegative.s that is the start of the MIPS assembly code.
If you would like to see how it runs in C, you can download a working C implementation as a NetBeans project to check your results.
How hard is this?
Well, not that hard. First of all, since this is a leaf procedure, you don’t need to implement a stack frame.
My initial implementation used 18 instructions. Two of those were the
return instructions
jr $ra
nop
There were also three other nop instructions.
That’s only 14 non-nop instructions.
You can get the implementation down to 12 instructions with only one
nop. But that requires a little more work.
Hints
Every couple of days, I will post hints on the Homework 10 moodle page.
Read these to keep on schedule.