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 commandl 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 commandine, 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 commandline 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 characters A that represent a signed ones complement number, twos 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 32bit signed integer in twos 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, twos 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 7010211 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 ones complement value and that we want to print out the twos 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 OOE 22 | coogocoogocoogocoolllo |
7
s 20 | |
S OOF? 19 | |
IWLIL 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 commandline 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 | |||
10011D01a.1011D | ||||
006-40623 | 110.01101 | |||
0.10100 | ||||
IOOL.OOOC | ||||
15.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 | OK01f11bd |
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 | SKSEL |
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 | SKSEL | |
TRSYtJ | ||
CARD |
Reviews
There are no reviews yet.