Uploaded by Adhya Chaudhary

MIPS Assembly Programming Guide for CS230

advertisement
Working with MIPS and Assembly Programs
These are some basic instructions for writing assembly code and using the MIPS
assembler and emulator used in CS230.
Step 1:
Write the assembly code
Using a simple text editor, write the code for your program using conventions
described in the lecture notes. You may write this code on your own computer, or
you may edit this code in directly on the university's Linux server. To assemble and
execute the program, you must eventually have a copy of the file on your account on
the UW Linux server.
By convention, assembly language programs should have the extension .asm
Step 2:
Assemble the code
Log in to linux.student.cs.uwaterloo.ca. In Unix, make sure you are using the
bash shell. If you see a $ at the end of the command line prompt, you are using bash.
You can invoke the bash shell by typing bash at the prompt.
To assemble your code, you must use the MIPS assembler that is available to CS230
students. In the same directory where your assembly program file (e.g program.asm)
is found, type the following command:
/u/cs230/pub/binasm < program.asm > program.mips
Breaking down this instruction:
/u/cs230/pub
is a directory in the CS230 account. The permissions are
public, so you can access files contained in this directory.
binasm
is an executable file that assembles programs into MIPS code.
This was written in Java. If you see Java errors when you are
trying to assemble your code, it probably means you are
having problems with the Unix machine, not a problem with
your assembly code. Make sure you are using
linux.student.cs.uwaterloo.ca
< program.asm
redirects input from standard input (the keyboard). Without
this, you would have to type in your assembler code
immediately, and what you typed would be lost afterwards.
With this, the program in the file program.asm is assembled.
You may use any file name you wish for the program file, but
it is a good idea not to have any spaces in the file name.
> program.mips
Step 3:
redirects output from standard output (the screen) to the file
program.mips. By convention this file name should have the
extension .mips You may use any name you like for the
output file, but it is a good idea not to have any spaces in the
file name.
Executing the program
Since we do not have direct access to the operating system on our computers, your
programs are executed using a MIPS emulator provided to CS230 students. There
are two front ends that are available to handle simple user input from the keyboard
automatically before executing the program instructions. Essentially the front ends
are calling your MIPS program as a subroutine after setting up some values in
registers and/or memory. A return address is also automatically put into register
$31. The final line of your program should be:
jr $31
which will allow you to exit the emulator gracefully.
The simpler front end prompts the user to enter two integers from standard input.
In the same directory where your MIPS file is found, type the following command:
/u/cs230/pub/twoints program.mips
You will be prompted for input twice. The first integer you enter will automatically
be put in register $1 and the second integer you enter will automatically be put into
register $2. When writing programs that are executed using this emulator you
should assume that registers $1 and $2 are initialized with values provided by the
user.
If you would like to work with more than two integers as input data, you can use the
array front end. In the same directory where your MIPS file is found, type the
following command:
/u/cs230/pub/array program.mips
You will be prompted for the size of the array (a natural number) and then the
values of the elements of the array in order. The memory location of the beginning
of the array will automatically be stored in register $1 and the size of the array will
automatically be stored in register $2.
There is a third front end, noargs, which does not expect input before executing the
instructions of the program. The noargs front end can be used by a program that
handles keyboard input as part of the code.
Note:
For assignments you are only required to submit the .asm file. The .mips file
you create and then execute is for your own testing purposes.
Download