程序代写代做代考 mips assembler algorithm CSE 220: Systems Fundamentals I

CSE 220: Systems Fundamentals I

Homework #1

Spring 2017
Assignment Due: Feb. 15, 2017 by 11:59 pm via Sparky

o PLEASE READTHEWHOLEDOCUMENTBEFORE 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 programwill convert values from base 2-15 to base 2-15.
o YouMUSTdownload the SB version ofMARSposted on thePIAZZAwebsite. DONOTUSE theMARS
available on theofficialwebpage. TheSBversionhas a reduced instruction set andadditional systemcalls
youwill need to complete the homework assignments.
o DO NOT COPY/SHARE CODE! We will check your assignments against this semester and previous
semesters!
Your programwill take three command-line arguments: 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

https://piazza.com/stonybrook/spring2016/cse220/resources

Arguments ProgramPrints Example of
123 5 F 28 Valid Input
1111111 3 7 3121 Valid Input
987654321 A 4 322313212202301 Valid Input
EE F B 194 Valid Input
89ABC F F 89ABC Valid Input
123 ERROR Not enough arguments
123 G 2 ERROR Invalid fromBase
123 1 4 ERROR Invalid toBase
123 22 A ERROR Invalid base, toomany characters
123 3 5 ERROR Value is not representable in fromBase
121 D a ERROR Invalid tobase , invalid hex character
ab4 F C ERROR Invalid value, invalid hex characters

� Use the algorithms taught in lecture to convert the integers. Base Conversion Tools are available on-
line to check youwork.
� 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 youwill initialize your program and identify the different command line
arguments.
To begin writing your program:

• Create a newMIPS program file.
• At the top of your program in comments put your name and id.

# Homework #1
# name: MY_FIRSTNAME_LASTNAME
# sbuid: MY_SBU_ID

ConfiguringMars 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
openMars, navigate to the Settingsmenu, and select
Program arguments provided to the MIPS program .

CSE 220 – Spring 2017 Homework #1 Page 2

https://www.tools4noobs.com/online_tools/base_convert/
http://www.linfo.org/argument.html

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.
V Your programmust ALWAYS be runwith 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.
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, $a0 and
$a1 . The $a0 register contains the number of arguments passed to your program. The $a1 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 inmemory 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
Add the following directives to the .data section to definememory space for the assignment:
.data
.align 2

numargs: .word 0
integer: .word 0
fromBase: .word 0
toBase: .word 0
Err_string: .asciiz “ERROR

# buffer is 32 space characters

CSE 220 – Spring 2017 Homework #1 Page 3

buffer: .ascii ” ”
newline: .asciiz “

# 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 will store the total number of arguments provided to the program in the label 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 is an assemblermacro. Macros are different from functions. We use it to simplify access
to the command line arguments for this first assignment.
Youmay implementmacros 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 load_args macro. This
must be executed before any other code to avoid losing values in $a0 and $a1 .
In addition, NEVER call this macro again. Doing somay overwrite the command line arguments passed to
your program.

.text

.globl main
main:

load_args() # Only do this once

After the load_args macro is called, youwill begin writing your program.

CSE 220 – Spring 2017 Homework #1 Page 4

http://courses.missouristate.edu/KenVollmar/mars/Help/MacrosHelp.html

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 .data section at label Err_string .
� 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
withinMars itself. Click theä in the tool bar to open it.

If the number of arguments is valid, next check the strings for the fromBase and toBase arguments.
Each of these arguments are valid if they each satisfy the following properties:

• is only 1 character in length, and
• is in the ASCII character set[

‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’
]

� 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 inmemory.
Some other questions to ask yourself are:
ä Consider the difference between the instructions lw and lb .
ä How are strings stored in memory? Howwould 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?
If either fromBase or toBase is not valid, print Err_string and exit the program.
CSE 220 – Spring 2017 Homework #1 Page 5

http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html

If both fromBase and toBase are valid, next check each character in the string stored at the address
in integer to ensure that each of the characters are valid in fromBase . If the integer is not repre-
sentable in fromBase , print Err_string and exit the program.
At this point, yourprogramshould correctlyhandle andvalidateall of the command-lineargumentspassed
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- fromBase to decimal (base-10) using the multiplication method,
then convert the decimal value to base- toBase using the division method. Both methods were dis-
cussed in lecture and are explained in the textbook.
V Remember that each ASCII character in the integer string must be converted to the digit value it
represents. The same goes for the ASCII character representing fromBase and toBase.
V 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!
V Youmay assume the decimal value is always representable in a single 32-bit register.
When using the algorithm for base conversion the value in the newbase is created startingwith 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
justified as shown in the examples at the beginning of the assignment. As buffer is not null-terminated
( .ascii vs .asciiz ) when printing starting at the address of buffer , the newline character will
also be printed.
� 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.

Hand-in instructions
CSE 220 – Spring 2017 Homework #1 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.
Whenwriting your program try to comment asmuch as possible. Try to stay consistentwith 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

https://piazza.com/class/iquy597k75d61h?cid=16

Leave a Reply

Your email address will not be published. Required fields are marked *