CSE 220: Systems Fundamentals I
Homework #1
Spring 2017
Assignment Due: Feb. 15, 2017 by 11:59 pm via Sparky
PLEASE READ THE WHOLE DOCUMENT BEFORE STARTING!
Introduction
The goal of this homework is to become familiar with basic MIPS instructions, syscalls, basic loops, con- ditional logic and memory representations. In this homework you will be creating a base conversion pro- gram. The program will convert values from base 2-15 to base 2-15.
You MUST download the SB version of MARS posted on the PIAZZA website. DO NOT USE the MARS available on the official webpage. The SB version has a reduced instruction set and additional system calls you will need to complete the homework assignments.
DO NOT COPY/SHARE CODE! We will check your assignments against this semester and previous semesters!
Yourprogramwilltakethreecommand-linearguments: integer fromBase toBase
• • •
integer : The null-terminated ASCII string representing the value to convert.
fromBase : The base of the value is given in.
toBase : The base to convert the value to.
Valid values for fromBase and toBase are 2-15. We will represent them as arguments using
hexedecimal digits [2-9, A-F].
Only capital letters A-F will be considered valid Hexadecimal digits for arguments
The program will convert the integer represented in base- fromBase to its corresponding value in base- toBase and print the new value out. If any of the arguments are invalid, the program will print “ERROR”.
Examples:
CSE 220 – Spring 2017 Homework #1 Page 1
Arguments
Program Prints
Example of
123 5 F
28
Valid Input
Valid Input
Valid Input
Valid Input
Valid Input
Not enough arguments Invalid
Invalid
Invalid base, too many characters Valueisnotrepresentablein fromBase Invalid tobase,invalidhexcharacter Invalid value, invalid hex characters
1111111 3 7
3121
987654321 A 4
322313212202301
EE F B
194
89ABC F F
89ABC
123
123 G 2
ERROR
ERROR
fromBase
123 1 4
ERROR
toBase
123 22 A
ERROR
123 3 5
ERROR
121 D a
ERROR
ab4 F C
ERROR
Use the algorithms taught in lecture to convert the integers. Base Conversion Tools are available on- line to check you work.
The numerical representation of the input value is guaranteed to fit in 32-bits. The maximum decimal value is 231 − 1
Part 1: Command-line arguments
In the first part of the assignment you will initialize your program and identify the different command line arguments.
To begin writing your program:
• CreateanewMIPSprogramfile.
• Atthetopofyourprogramincommentsputyournameandid.
# Homework #1 # name: MY_FIRSTNAME_LASTNAME # sbuid: MY_SBU_ID ...
Configuring Mars for command line arguments
Your program is going to accept command line arguments which will provide input to the program. To do this, we first need to tell Mars that we want to accept command line arguments. To do this you need to open Mars, navigate to the Settings menu, and select
Program arguments provided to the MIPS program .
CSE 220 – Spring 2017 Homework #1 Page 2
After assembling your program, in the Execute tab you should see a text box where you can type in your command line arguments before running the program.
Each command line argument should be separated by a space.
When your program is assembled and then run, the arguments to your program are placed in memory. Information about the arguments is then provided to your code using the argument registers, and $a1 . The $a0 register contains the number of arguments passed to your program. The register contains the starting address of an array of strings. Each element in the array is the starting address of the
argument specified on the command line.
All arguments are saved in memory as ASCII character strings.
In this assignment, we will be using three arguments. We have provided you boilerplate code for extract-
ing each of the 3 arguments from the array.
Setting up the .data section
Addthefollowingdirectivestothe .data sectiontodefinememoryspacefortheassignment:
.data .align 2
numargs: .word 0 integer: .word 0 fromBase: .word 0 toBase: .word 0 Err_string: .asciiz "ERROR\n"
# buffer is 32 space characters
Your program must ALWAYS be run with at least 1 command line argument! You can expect that com- mand line arguments will always be given in the correct order for this assignment.
$a0
$a1
CSE 220 – Spring 2017 Homework #1 Page 3
buffer: .ascii " " newline: .asciiz "\n"
# Helper macro for grabbing command line arguments .macro load_args
sw $a0, numargs lw $t0, 0($a1) sw $t0, integer lw $t0, 4($a1) sw $t0, fromBase lw $t0, 8($a1) sw $t0, toBase
.end_macro
load_args willstorethetotalnumberofargumentsprovidedtotheprograminthelabel numargs.In addition, the starting address of each argument string that is provided to your program can be accessed using labels integer , fromBase , and toBase .
load_args isanassemblermacro.Macrosaredifferentfromfunctions.Weuseittosimplifyaccess to the command line arguments for this first assignment.
You may implement macros in your own programs, however it is not a requirement. We caution their use as they can introduce non-obvious coding bugs if not used carefully.
You can declare more items in your .data section as you wish, but you must at minimum have the ones defined exactly as they appear in the above code section.
Writing the program
Create the .text section in your homework file and create a label main: . This is the main entry to your program. Next, we will extract the command line arguments using the macro. This
mustbeexecutedbeforeanyothercodetoavoidlosingvaluesin $a0 and .
In addition, NEVER call this macro again. Doing so may overwrite the command line arguments passed to
your program.
.text .globl main main:
load_args() # Only do this once
Afterthe load_args macroiscalled,youwillbeginwritingyourprogram.
CSE 220 – Spring 2017 Homework #1 Page 4
load_args
$a1
First add code to check the number of command line arguments provided. If the value string stored in memory at label numargs is not 3, the program should print out Err_string and exit the program. Note that the error string has already been defined in your section at label Err_string .
.data
The number of arguments is stored in memory at the label numargs by the macro, but the value is
also STILL in $a0 , as the macro code does not modify the contents of $a0 . Remember, values remain in registers until a new value is stored into the register, thereby overwriting it.
To print a string in MIPS, you need to use system call 4. You can find a listing of all the official Mars systems calls here. You can also find the documentation for all instructions and supported system calls within Mars itself. Click the in the tool bar to open it.
Ifthenumberofargumentsisvalid,nextcheckthestringsforthe fromBase and toBase arguments. Each of these arguments are valid if they each satisfy the following properties:
• isonly1characterinlength,and • isintheASCIIcharacterset
‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’
Some other questions to ask yourself are:
Ifeither fromBase or toBase isnotvalid,print Err_string andexittheprogram.
CSE 220 – Spring 2017 Homework #1 Page 5
To use the values placed in integer , fromBase , and toBase , you will need to use load instruc- tion(s) to obtain the value(s) stored in memory. The labels integer , fromBase , and toBase each store the starting address of a null-terminated sequence of ASCII characters stored in memory.
Consider the difference between the instructions lw and lb . |
How are strings stored in memory? How would you know if the argument string has a length of only 1 character? How do you check this? |
What is the relationship between the ASCII characters in their binary representation? Can you write simple checks, rather than a LARGE if/else or switch/case statement? |
and toBase are valid, next check each character in the string stored at the address to ensure that each of the characters are valid in fromBase . If the integer is not repre-
,print Err_string andexittheprogram.
At this point, your program should correctly handle and validate all of the command-line arguments passed
to the program.
Part 2: Converting integer
In this part of the assignment, convert integer from base- fromBase to base- toBase .
First convert integer from base- to decimal (base-10) using the multiplication method, then convert the decimal value to base- using the division method. Both methods were dis- cussed in lecture and are explained in the textbook.
You may assume the decimal value is always representable in a single 32-bit register.
When using the algorithm for base conversion the value in the new base is created starting with the right- most digit (Least Significant digit) to the left-most digit (Most Significant digit). To store the new value as we create it we have allocated space for 32 characters in memory at the label buffer . Directly after this in memory is a newline character.
As you execute the algorithm, store the ASCII character for each digit of the new value into the buffer, starting from the right-most position. The memory address of the right most position is the address of label buffer+31 or newline-1 . With each additional digit to store, subtract 1 from the address.
When you print the result, print the string starting at the label buffer . The value printed will be right
If both
in sentablein
fromBase
integer
fromBase
fromBase
toBase
Remember that each ASCII character in the string must be converted to the digit value it represents. The same goes for the ASCII character representing and toBase.
integer
fromBase
Remember that numbers are stored in computers using 2’s complement notation. The decimal value will be stored in its 2’s complement representation within the register!
justified as shown in the examples at the beginning of the assignment. As ( .ascii vs .asciiz ) when printing starting at the address of
also be printed.
Hand-in instructions
CSE 220 – Spring 2017 Homework #1
is not null-terminated , the newline character will
buffer
buffer
NOTE: There is a base case for conversion. If the fromBase and toBase are the same, the char- acters from value can be copied into the buffer directly. It is up to you if you want to implement this check or apply the regular conversion algorithm.
Page 6
Do not add any miscellaneous printouts, as this will probably make the grading script give you a zero. Please print out the text exactly as it is displayed in the examples, one output line ONLY.
See Sparky Submission Instructions on piazza for hand-in instructions. There is no tolerance for home- work submission via email. They must be submitted through Sparky. Please do not wait until the last minute to submit your homework. If you are struggling, stop by office hours.
When writing your program try to comment as much as possible. Try to stay consistent with your format- ting. It is much easier for your TA and the professor to help you if we can figure out what your code does quickly.
CSE 220 – Spring 2017 Homework #1 Page 7