Converting C++ to MIPS Assembly

Converting C++ to MIPS Assembly - Spring 2008 CS 33 Project...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring 2008 CS 33 Project 4 Notes So how do you get started on Project 4? If there's anything you should have learned in CS 31 and CS 32, it's to develop incrementally. So let's see how that applies to developing an assembly language program from a C++ program. Suppose we had written a C++ program that reads a series of integers representing student scores on an exam (terminated by a negative number), and we want to print the average. Here's our program: #include <iostream> using namespace std; int total = 0; int n = 0; int value; int main() { cout << "(Terminate input by entering a negative integer.)" << endl; for (;;) { cout << "Enter an integer: "; cin >> value; if (value < 0) break; total += value; n++; } if (n == 0) cout << "No numbers were entered." << endl; else cout << "The average of those " << n << " integers is " << static_cast<double>(total) / n << endl; } Let's start small: Can we write a program that simply prints the initial prompt and terminates? Using code in the book as a guide (e.g., p. 20 and the table on p. 112), we try this: # Compute average of input integers .data openingmsg: .asciiz "(Terminate input by entering a negative integer.)\n" .text main: la $a0,openingmsg # Print opening message ori $v0,$0,4 syscall jr $ra # Return from main
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Or perhaps we try something almost like that, but not quite right. Because there's so little code, it should be straightforward to find and fix any bug we discover when we run this. Once this
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Converting C++ to MIPS Assembly - Spring 2008 CS 33 Project...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online