Programming Project #1

Assigned 9/12/95

Due Friday, 9/22/95 at 11:59pm


Problem Statment

int ascii_to_binary(char *string)

Write a function in MIPS assembly language that converts an integer represented by a string of null-terminated ASCII characters to the same integer represented by a two's complement number. Your function should accept a pointer to the string as its single argument and provide the two's complement equivalent as the return value from the function.

A valid input string may have any number of leading spaces or tabs (whitespace). The first non-whitespace character must be ³-³or a digit. Your function should continue reading the string until the first non-digit character is found, e.g., a whitespace character or the null character. The two's complement equivalent of the string of digits, including the ³-³, is returned to the caller. Your function should return the value ³0² if the input isn't valid.

Use the MIPS simulator xspim found in /home/elec320/spim/bin on Owlnet. It is recommended that you use xspim as opposed to spim because it displays the entire processor state, i.e., register contents, at each step. You may want to add this directory to your path.

Documentation on how to use xpsim and write MIPS assembly can be found in Appendix A in the back of your book.

Important details:

* Since your ascii_to_binary routine is to be a function, make sure you use the standard gcc calling convention for passing arguments and returning values. The gcc calling convention is described in Appendix A of your book. Briefly, this means that arguments should be passed in $a0 - $a3, the return value should be in $v0, and registers that need to be maintained between calls should be saved on the stack (for example, the return address). To save a word on the stack, subtract 4 (the size in bytes of a word) from the stack pointer. Then use a store word instruction to put the word in the location pointed to by the stack pointer. To remove the word from the stack, load the word from the stack and add 4 to the stack pointer.

* Comment your code using ³#² after every line. Uncommented assembly is extremely hard to understand and will only serve to frustrate the labbies (and lower your grade). Also, at the beginning of each function, describe how each register is used (if it has a consistent use). I recommend that you use the same registers for a specific purpose all throughout a function. This will confuse you less and make your code more understandable. Finally, use the register names found in figure A.9 on page A23 of your book, not the register number. The only exception to this is that you may use $0 instead of $zero.

* Do not use global variables in your function. Any temporary variables for your function should use the temporary registers. You should have plenty of registers for this assignment, so there should be no need to have to save and restore temporary variables.

* Do not worry about overflows or underflows. If you do detect them, print a message that says ³overflow² or ³underflow² and return ³0².

* Your finished source file should use the read_string system call to input a number and call your function. After you function returns, you should print out the value that your function returned using the print_int system call.

* Try to make your function as short and fast as possible. The whole reason for writing something in assembly is to make it as fast as possible.

How to turn in your solution:

Put your final solution in a directory elec320/lab1 located in your home directory. Your solution will be graded out of this directory after the due date. Late submissions will not be graded, so make sure that you do not change any files after the due date. Also, make sure your directory and files are world readable so that we can read your solution. You can change the protection of a file or directory by using the chmod command. To change the permissions of a file, use chmod 644 filename. For a directory, use chmod 755 directory.

Start early! This may sound like a simple problem, but it may take you a while to get used to xspim and MIPS assembly op-codes.

Finally, if you have any questions, send mail to elec320 or post to the elec320 newsgroup (rice.owlnews.elec320). Please may sure you read the elec320 newsgroup often, because any clarifications will be posted there.