Starting from:

$35

CSE220- Programming Assignment #1 Solved

Learning Outconws
After completion of this programming project you should be able to:

use system calls to print values to the screen in different formats.
Design and iinp ement algorithms in MIPS assem.b y that involve if-statements and counter-driven oops.
Read and write valUes stored in a MIPS assembly -data section.
use bitwise operations to perform simple computations in MIPS assembly.
Getting Started

Visit the course website and download the files hwkl .asm and MarsFa112020.jar. Fill n the following information at the top of hwkl .asm:

yourfirst and hst name as they appear in Blackboard
your Net 10 (e.g., jsmith)
3.      your stony  Illsgggggj
Having this n%rmation at the top of the file helps us locate yourwork. Ifyou fOrget to include this information but don't remember until after the deadline has passed, don't worry about it — we will track down your submiwion.

Inside hwkl.asm you will find some code to start with. Your job in this assignment is implement all the operations as specified below. If you are having difficulty implementing these operations, write out pseudocode or ementthe algorithms in a higher-level anquage first. Once you understand the algorithm and what steps to perform, then translate the logic to MIPS assembly code.

Important Information about CSE 220 Programming Projects

Read the entire project description document twice before starting. Questions posted on Piazza whose answers are clearly stated in the documents will be given lowest priority hy the course
You must use the Stony Brook version of MARS posted on the course website. Do not use the version of MARS posted on the official I•AAR.S website. The Stony Brook version has a reduced instruction set. added tools, and additional system calls pu might need to complete the homework assignments.
When writing assembly code. try to stay consistent with your formatting and to comment as much as possib e. It is much easier for your TAs and the pro essor to help you ifwe can quickly figure out what your code does
You persona ly must implement the programming assignments in MIPS Assembly language by yourself. You may not write or use a code generator or other tools that write any MIPS code for you. You must manual y write all MIPS assembly code you submit as part of the assignments.
Da not capy or share code. Your subm s:sions will be checked against other submissions from this semester and from previous semesters
Submit your fina .asm file to the course website by the due date and time. Late work will be pena ized as described in the course syllabus. Code that crashes and cannot be graded will eam no credit. No changes to yzur submission will be permitted once the has passed.
How Your CSE 220 Assignments Will Be Graded
With few exceptions. all aspects of your programming assiOnments will be graded entirety through automated means. Grading scripts will execute your code with input valUes (e.g.. command-line arguments. function arguments) and will check for expected results (e.g.. print-outs. retum values. etc.) For homework assignment your grogram will be generating output that will be checked for exact matches by the grading scripts. Therefore, it is inveratlve that you ÅT.p ement the "print statements" exactly as specified in the assignment.

Some other items you should be aware of:

Each test case must execute in 20,000 instructions or fewer. Efficiency is an important aspect of programming. This maximurn instruction countwil be increased in cases where a complicated algorithm might be necessary. or a large data structure must be traversed. To find the instruction count of your code in MARS. go to the TOols menu and select Instruction Statistics. Press the button marked Connect to MIPS. Then assemble and run your code as normal.
Any excess output from your program (debugging noÄ etc,) will impact grading_ Do nat leave erroneous print-outs in your code
We will provide you with a small set of test cases for each assiOnment to give a sense of how your work will be graded. It is your responsibility to test '"Jur code thoroughly by creating your own test cases.
The testing framework we use for grading yaur wark will not be released, but the test cases and euected results used for testing will be released
Configuring MARS for Command-line Arguments

Your program is go ng to accept command-I ne arguments, which will be provided as input to the program. TO tell MANS that we wish to accept command-I ne arguments, we must go to the Settings menu and check the box marked:

Program arguments provided to the MIPS program

While youre at it, also check the box marked'

Initialize Program Counter to globat 'main ' if defined

After assembling your program, in the Execute tab. you should see a text box where you can type iri your command-line arguments before running the program

Edit Execute
Text Segment
Program Argum •nts:
The command-line arguments must be separated by spaces. Note that your program must always be run with at least one command-line argument. When your program is assembled and then run, the arguments to your program are placed in main memory before execution. Informat on about the arguments is then provided to your code using the argument registers. and ial. The register contains the number of arguments gassed to your program. The Sal 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•l ne.

All arguments are saved in memory as ASCII character strings. terminated by the null character (ASCII O, which is denoted by the character in assembly codeb So, for example, ifwe want to read an integer argument on the command•ine, we actually must take a string of digit characters (e.g.. " ZOE 4 and then convert to an integer ourselves in assembly code We have provided code for you that stores the addresses of the command-line arguments at pre-defined, unique labels (e.g., acids_arga, adcir_ergl, etc.) Note that the strings themselves are not stored at these labels. Rather, the starting addresses of the strings are stored at those labels. You wil need to use load Instructions to obtain the contents of these strings stored at the addresses: iw to load the address of a string, then multiple ibu instructions to get the characters. For Instance, 1K *so, adds arg2, followed by $tcr [$80' wouldstore me character located at index 4 in             (assuming O.based indexes) ofthe mird connmand line argument-

Running the Program
Running the provided hwkl .asm tile is prety s mple_ Hit F3 on the keyboard or press the button shown below to assemble your code:

If your code has any syntax errors, MARS wil repart them in the MARS Messages pane at the bottom of the window_ Fix any syntax errors you may have, Then press F5 or hit the Run button shown be ow to run your program:

Any output generated by your program will appear in the Run I/O panel at the bottom of the window

Part 1 : Validate the First Command-line Argument and the Number of Command-line Arguments

For this assignment you will be mplementing several operations that perform computations and do data man pulatlon

In hwkl.asm, begin writing your program immediately after the label called s tart coding_here. You may declare more items in the _data section after the provided coda. Any code that has already been provided must appear exactly as defined in the given file. Do nat delete or rename these abels, as doing so will negatively impact grading.

The numberof arguments is stored in memory at the label num by code already provided for you. You Will need to use various system ca Is ta print that your cade generates. For exampli* to print a string iil MIPS, you need to use system call 4. you can find a listing of all the official MARS system calls on the MARS website_ You can also find the documentation far all instructions and supported system ca Is within MARS itself. Click the blue question mark in the right-hand end ofthe tool bar ta open it.

Later in the document is a list of the operations that your program will execute. Each operation is identified by a single character. The character is given by the first command-line argument (whose address is The parameter(s) to each argument are given as the remaining command-Ine arguments (located at addresses addr aryl,  etc)_ In this first part of the assignment your program must make sure that each operation is valid and has been given the correc:t number of parameters_ Perform the validations in the following order:

 

The first command-line argument must be a string of length one that consists of one ofthe fol owing characters: 1, 2, S, F, R or P. If the argument is a letter. it must be given in uppercase. If the argument is not one of these strings or if the argument contains more than one character, print the string found at label  and exit the program (via system ca 1 10). This string contains a newline character at the end* so you do not need to provide your

The 1 2 and S operations expect two additional arguments. Ifthe tota number of command-line arguments far these commands is nat three, printthe string found at label error and exit the program (via system call 10). This string contains a newline character at the end, so you do not need to provide yaur own.
The F operation expects one additional argument If the total number of command•line arguments for this command is not two, print the string found at label invalid_args_ersaz and exit the grogram (via system call 10}. This string contains a newline character at the end, so you do not need to provide '"Jur own.
The R operation expects six additional arguments. If the total number of command-line arguments far this command is not seven, print the string found at label error and exit the program (via system call 10). This string contains a newline character at the end, so you do not need to provide yaur own.
The P operation expects one additiona argument. If the total number ofcommand-line arguments far this command is not two. print the string faund at label invalid error and exit the program (via system call 10b This string contains a newline character at the end, so you do not need ta provide yaur own.
Important: You must use theprovided invalid operation error and arg: error strings when printing error messages. Do not create your own labels for printing output ta the screen. If your output is as incorrect by the grading scripts because of typos then it is your fault for not us ng the provided strings, and you wil lose all credit for those test cases_

ae sure to initialize all of your values (e.g., registers) within your functians_ Never assume registers or memory will hold any particular values (a_g_.         MARS initializes all of the registers and bytes of main memory to zeroes. The grading scripts for later assignments wil fill the registers and/or main memory with random values before executing your code

Later sections will eulain how to validate the arguments for each operation

Examples of Invalid Input
See the later sections af the documents for explanation of what arguments are valid for each operation.

 
Command-line Arguments
Label for String to Print
Expected Output
 
OCOE 32
invalid opera t: ion error
 
p HIJKL
invalid operation
INVALID OPERATION
 
F XYZ ABC
inMA lid
INVALID
 
 
 
 
 
 
 
 
Character Strings in MIPS Assembly
In assembly, a string is a one-dimensional array of unsigned bytes. Therefore, to read each character of the string we typically need to use a OOP. Suppose that contains the base address of the string (that is. the address of the first character of the string). We cwd use the instruction Ibu $ to, 0 to copy the first character of the string into 0. To get the next character of the string, we have two options: (i) add 1 to the contents of $sO and then execute Ibu $70, D again, or (ii) leave the contents of alone and execute Ibu Sto, 2 Generally speaking, the first approach is easier and simpler to use, but the other approach works perfectly fine ifthe string is short in length is always thesamve enqth. Note thatsyntaxllke Ibu El. is not valid. an immediate value la constant) must be given outside the parentheses.

Next Process the Input

If the program determines that the first command-line argument is a valid operation and that it has been given a correct number of additiOna arguments. the program continues b} executing the appropriate operation as specified below. Note that you are permitted to add code to the -data section as necessary to implement these operations.

Part 2: Process a String of Four Hexadecimal Digits that Represents a Signed Integer
The 1 2 and S operations treat the second command-line argument as a string of hexadecimal digit characte'rs — •A' — that represent a signed one's complement number, two's complement number or sign/magnitude number, respectively' The leftmost character represents the most significant nibble (4 bits) of the integer. The operation converts the string into a 32•bit signed integer in two's complement, storing it in a single register. The code then prints the N rightmost bits of that register, where N is given by the third argument. In other words, the integer is printed as an "Wbii, two's complement value. Finally, the code prints a newline character j. The numberof bits to print given by the third argument should be at least 16. Helpfu hint you can give an ASCII character as a literal in M PS (e.g.,

There are different a gorithms you can devise to implement this operation. To get started, think of how you would convert characters like and 'D' from the binary ASCII values 701'„0211 and

01 respectively, to the binary values 0121 and 1101, respectively. Also explore how pu can extract individua bits from a register using bitwise operators.

First command- ine argument: the character 1 , 2 or S
Second command-line argument exactly four characters that represent hexadecimal digits
6

Third command-line argument: a number of bits* 16 ands 32
Input Validation

The second command-line argument mu:st consist on y of characters that represent hexadecimal digits All letters must be provided n uppercase, An input containing any lowercase letters is considered invalid: in such cases, print the string invalid arg; error and terminate the program via system call 10. If the argument contains any other WJalid characters, print the string found at label invalid error and terminate the program You may assume thatthesecond command-line argument contains exactly four characters.

The third command-line argument must consist only of two characters that represent decimal digits. You may assume that the third command-line argument contains exactly two digit characters, but that those digits might represent a number in the range 0 through 99, inclusive_ H the number is outside the range 18 through 32, print the string invalid arg; error and terminate the pragram. When the third argument is a valid number, you may assume that the input va ue may be expressed using that number of bits.

Worked-out Exannple

Suppose the command-line arguments are  These arguments indicate that the string represents a one's complement value and that we want to print out the two's complement representation of that value using 25 bits.

Exampl es

Command-line Arguments
Expected Output
2 FFF&
 
2 OOLI 25
oaooaoaooaoaooaoaoouaool
2 7EA2
 
1 FYFE
 
1         25
 
1 4190 32
 
S OßOE 22
coogocoogocoogocoolllo
7

s   20
 
S OOF'? 19
 
 
IWÅLIL' ARG3
1 WOLE 28
 
S OIFI 05
IUVALID ARCS
Part 3: Print a Decimal Fixed-point Number as a Binary Fixed-point Number
The F operation takes a string of nine characters that representa positive decima real number and, using ordy integer arithmetic, converts the decimal value to binary and grints it to the screen. The whole number part of the output must not contain any leading zeros except in the case where the whole number part is zero. In this case, only a single zero may be printed. Finally, the code prints a newline character ( a'

The fractional part of the input number will always represent a value that can be computed by adding some wmbination of the values 0.5, 0.25, 0.125. 0.0625 and 0.03125 [that is,  2' and (This accommodation makes It possible to convert the fractiona decimal value to binary using only integer arithmetic.) The output value must always contain exactly five bits to the right of the radix point.

First command-line argument the character F
Second command•line argument exactly three decimal digits characters, followed by a period, followed by exactly five decimal digit characters
Input Validation

You may assume that the input is always valid.

Exannples
Command •line Arguments
Expected Output
 
 
10011D01ßa.1011D
 
006-40623
110.01101
 
 
0.10100
 
 
 
 
 
IOOßL.OOÜOC
 
 
1±5.18750
 
 
 
 
 
 
Part 4: Encode Six Numerical Fields as an R-upe MIPS Instruction

The R operation takes six decimal values, treats them as the six fields af a MIPS R-type instruction, and. using shifting and masking operations, combines the faur values into a single 32-bit integer that represents an R-type nstruc:tion_ The program then prints this value in hexadecimal using syscall 34. Finally, the code prints a newline character ( n ' i. An example will helpto c arify this process

First command-I ne argument: the character R
Second command-line argument the string •00"
Third command-line argument: a string that encodes a positive, two-digit decimal integer in the range 311 (the rs field). A leading zero is provided if needed
Fourth command-line argument: a string that encodes a positive, two-digit decimal integer in the range 311 (the rt field}. A leading zero is provided if needed.
Fifth command-I ne argument: a string that encodes a positive, two-digit dec mal integer n the range 311 (the rd field) A leading zero is provided if needed.
Sixth command-line argument: a string that encodes a positive, two-digit decimal integer n the range 311 (the shamt field). A leading zero is provided if needed
Seventh command-line argument a string that encodes a positive, two-digit decimal integer n the range 63) (the functfield)_ A leading zero is provided if needed_
A 32-hit R-type instruction consists of 6 fields, where the number be ow each field indicates its width in bits_ Nate that bit #0 is the rightmost bit

Field
opcod e
rs
 
rd
shamt
funct
# of bits
6
5
5
5
5
6
Suppose the command-line arguments were the strings

R   15 17 03 14 29

The program must process each numerical argurnent in turn, converting each ot the six strings nto an integer. Remember that all data in a computer is stored in binary, so, after conversion, these values will be stored as the 32.bit values given below:

 

as the opcode, rs. rt. rd, shamt and func:t fields, respectively Next. using

bitwise operations, we need to concatenate the six rightmost hits of the opcode, five rightmost bits of the rs, rd and shamt fields, and 8 rightmost bits ofthe funct field:

When printed in hexadec mal, the outputva ue (produced by syscall 34) wi" be

Input Validation
Yau may assume that the second argument is always *00'. You may assume that the third, fourtm fiftm six and seventh command-41ne arguments aluvays consist of exactly (and only) twa decimal digit characters that encode a positive integer However, yau may not assume that the values are in the legal ranges specified above. If any of the third, fourth, fifth, six and seventh command-line arguments is outside its legal range, print the string found at label invalid error and exit the program (via system call 10).

Note that the shamt fie d is app icahle only when performing a shift nstructiom You do not need to check whether a non-zero shamt amount is given when a non-shifting instruction is provided. Simply have your code convert the shamt argument to a 5-bit binary regardess af what the funct fie d is, and incorporate that 5-bit value in the final 32-bii value computed by your coda.

Exannples
Command-line Arguments
Expected Output
         15            14
OK01f11b±d
 31 24 a: 42
 
R          22 25                 El
OK02d9f83d
17 41 22 3D 08
 
Part S: Identify Non-standard, Five-card Poker Hands
The P operation treats the second command-line argument as a string of exactly five characters that encode a non-standard five-card hand from draw poker_

The program must be able to identify some of the non-standard hands of five-card draw poker and print a message indicating which one it found. If the hand can be matched with two or nwre hands, the hand of highest rank is printed. If none of these five ranked hands is identified. the program prints

HIGH_CARO. You may assume that when the P operation has been provided. the second argument always contains exactly 5 characters. For the purposes of this assignment, an Ace is always treated as a low card (i.e., less than Z), never as a high card (i.e., higher than King).

The hand ranks from highest-to-lowest, along with the relevant MIPS string to print, are:

Rank
Hand Name
Label for String to Print
Output on Screen
 
Big bobtail
bobtail ser
BOBTALL
2
Full house
full house
RULL
 
Five and dime
five and dime s t:
 
4
Skeet
skeet 5 tr
SKSE'L'
5
B aze
blaze
BLAZE
6
Every-thing e se (i.e., High card)
high_earä str
 
From Wikiped a:

Big bobtail: A tour-card straighthush (four cards ot the same suit in consecutive order)_

2- Full house: three cards of one rank and two cards of another rank (exarnple: 3-3-3.J.U).

3_ Five and dime: Slow, 1 a-high, wit-I no pair (example: 5-6-7-8-10).

4- Skeet: A hand      a 2, a 5, and a g, p us two other un.paired cards owerthan 9 (example:

2-4-5-6-9)_

Blaze: All cards are Jacks, queens, and/or Kings.

6- Hi" card: None ot the above.

A single card is encoded as an ASCII character_ Specificaty, the ASC code ranges employed are:

0141 Ox4D represent the A through ot Clubs
Ox51 represent the A through ot Spades
0161 Ox6D represent the A through ot Diarmoncis
0171 Ox7LD represent then through ot Hearts
Note that the lett hexadecimal digit encodes the suit, and the right digit encodes the rank Valid ranks range 1 • 13 (Oxl • OXO). Note that 11, 12 and 13 (OxB, OxC, OXO) correspond with Jack, Queen and

King, respectively

For example, Ox63 represents the 3 of Diamonds and corresponds with tie ASCII character 'c'. Likewise, 0148 represents the Jack ot Clubs and corresponds with the ASC I character

An example of a valid hand would be the string "uX[qT, which have ASCII codes:

0176 = 6 of Hearts
0158 = B of Spades
015B : Jack ot Spades
0171 = Ace ot Hearts
0164 = 4 of Diarmor.ds
Input Validation

You may assume that the second command-line argument al-ways consists of exactly five characters that encode valid cards. You may also assume that no card appears more than once in the input

Examples

Command-line Arguments
Meaning of Input
Expected Output
svros
 
 
P BE CEG
 
EIG BOBTAIL
 
 
RULL
 
 
 
P WRu1F
 
SKSE'L'
TRSYtJ
 
 
 
 
 
 
 
CARD

More products