[SOLVED] CSE220-Homework 1

30.00 $

Category:

Description

5/5 - (1 vote)

these labels. Rather, the starting addresses of the strings are stored at those labels. You will need to

addr_arg2, followed by Ibu $t0, 4 ($s0) would store the character located at index 4 in $t0 (assuming O-based indexes) of the third command line argument.

Running the Program

Running the provided hwkl .asm file is pretty simple. Hit F3 on the keyboard or press the button shown below to assemble your code:

If your code has any syntax errors, MARS will report them in the MARS Messages panel at the bottom of the window. Fix any syntax errors you may have. Then press F5 or hit the Run button shown below 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 implementing several operations that perform computations and do data manipulation.

In hwkl .asm, begin writing your program immediately after the label called start coding here.

_

You may declare more items in the .data section after the provided code. Any code that has already been provided must appear exactly as defined in the given file. Do not delete or rename these labels, as doing so will negatively impact grading.

The number of arguments is stored in memory at the label num args by code already provided for you. You will need to use various system calls to print values that your code generates. For example, to orint a strina in MIPS. vou need to use svstem call 4. You can find a listina of all the official MARS

 

The number of arguments is stored in memory at the label num args by code already provided for

supported system calls within MARS itself. Click the blue question mark in the right-hand end of the tool bar to 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 addr_arg0). The parameter(s) to each argument are given as the remaining

4

command-line arguments (located at addresses addr_argl, addr_arg2, etc.). In this first part of the assignment your program must make sure that each operation is valid and has been given the correct number of parameters. Perform the validations in the following order:

  1. The first command-line argument must be a string of length one that consists of one of the following 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 invalid operation 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 your own.
  2. The 1, 2 and S operations expect two additional arguments. If the total number of command-line arguments for these commands is not three, print the string found at label and exit the program (via system call 10). This string contains a newline character at the end, so you do not need to provide your own.
  3. 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 and exit the program (via system call 10). This string contains a newline character at the end, so you do not need to provide your own.

arguments for this command is not two, print the string found at label invalid_args 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 your own.

Important: You must use the provided invalid operation error and invalid args error strings when printing error messages. Do not create your own labels for printing output to the screen. If your output is marked as incorrect by the grading scripts because of typos, then it is your fault for not using the provided strings, and you will lose all credit for those test cases.

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

Later sections will explain how to validate the arguments for each operation.

Examples of Invalid Input

5

See the later sections of the documents for explanation of what arguments are valid for each operation.

Command-line Arguments Label for String to Print Expected Output
     
Command-line Arguments Label for String to Print Expected Output
Signed OOOE 32 invalid operation error INVALID OPERATION
p HIJKL invalid operation error INVALID OPERATION
F XYZ ABC invalid args INVALID ARGS

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 loop. Suppose that $so contains the base address of the string (that is, the address of the first character of the string). We could use the instruction Ibu $tO, O ($sO) to copy the first character of the string into $tO. To get the next character of the string, we have two options: (i) add I to the contents of $sO and then execute Ibu $tO, O ($sO) again, or (ii) leave the contents of $so alone and execute Ibu $to, 1 ($s0) . Generally speaking, the first approach is easier and simpler to use, but the other approach works perfectly fine if the string is short in length and is always the same length. Note that syntax like Ibu $tO, $tl ($sO) is not valid; an immediate value (a 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 additional arguments, the program continues by 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 I, 2 and S operations treat the second command-line argument as a string of hexadecimal digit characters (‘0’ — ‘9’, ‘A’ — ‘F’) 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 N-bit, two’s complement value. Finally, the code prints a newline character ( ‘       ). The number of bits to print given by the third argument should be at least 16. Helpful hint: you can give an ASCII character

register, where N is given by the third argument. In other words, the integer is printed as an N-bit, two’s complement value. Finally, the code prints a newline character print given by the third argument should be at least 16. Helpful hint: you can give an ASCII character as a literal in MIPS (e.g., li $aO, ‘\n’).

There are different algorithms you can devise to implement this operation. To get started, think of how you would convert characters like ‘7’ and ‘D’ from the binary ASCII values 00110111 and

6

01000100, respectively, to the binary values 0111 and 1101, respectively. Also explore how you can extract individual bits from a register using bitwise operators.

  • First command-line argument: the character 1, 2 or S
  • Second command-line argument: exactly four characters that represent hexadecimal digits
  • Third command-line argument: a number of bits 2 16 and 32

Input Validation

The second command-line argument must consist only of characters that represent hexadecimal digits. All letters must be provided in uppercase. An input containing any lowercase letters is considered invalid; in such cases, print the string invalid args error and terminate the program via system call 10. If the argument contains any other invalid characters, print the string found at label invalid args_error and terminate the program. You may assume that the second 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. If the number is outside the range 16 through 32, print the string invalid args_error and terminate the program. When the third araument is a valid number, vou mav assume that the inout value mav be exoressed

 

complement representation of that value using 25 bits.

integer -14 in one’s complement representation, which is represented in 25-bit two’s complement as

Examples

Command-line Arguments Expected Output
2 FFFF 20
2 0011 25 0000000000000000000010001
2 7EA2 19

7

  1 FFFE 20  
  1 FFFI 25  
  1 4190 32 00000000000000000100000110010000  
  s OOOE 22 0000000000000000001110  
  s 800A 20  
  s OOFF 19  
  s OOFF 19
2 faCE 19 INVALID ARGS
1 WOLF 28 INVALID ARG S
s OIFI 05 INVALID ARG S

Part 3: Print a Decimal Fixed-point Number as a Binary Fixed-point Number

The F operation takes a string of nine characters that represent a positive decimal real number and, using only integer arithmetic, converts the decimal value to binary and prints 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 ( ).

The fractional part of the input number will always represent a value that can be computed by adding some combination of the values 0.5, 0.25, 0.125, 0.0625 and 0.03125 (that is, 2-1 , 2-2, 2-3 , 24 and 2 -5). (This accommodation makes it possible to convert the fractional 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.

Examples

Command-line Arguments Expected Output

8

File Edit View Tools Help

Part 4: Encode Six Numerical Fields as an R-Type MIPS Instruction

The R operation takes six decimal values, treats them as the six fields of a MIPS R-type instruction, and, using shifting and masking operations, combines the four values into a single 32-bit integer that represents an R-type instruction. The program then prints this value in hexadecimal using syscall 34. Finally, the code prints a newline character ( ‘    ). An example will help to clarify this process.

  • First command-line 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 [0, 31] (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 [O, 31] (the rt field). A leading zero is provided if needed.
  • Fifth command-line argument: a string that encodes a positive, two-digit decimal integer in the range [O, 31] (the rd field). A leading zero is provided if needed.
  • Sixth command-line argument: a string that encodes a positive, two-digit decimal integer in the range [O, 31] (the shamt field). A leading zero is provided if needed.
  • Seventh command-line argument: a string that encodes a positive, two-digit decimal integer in the range [0, 63] (the funct field). A leading zero is provided if needed.

 

Field opcode rs rt rd shamt funct
# of bits 6 5 5 5 5 6

9

Suppose the command-line arguments were the strings

R 00 15 17 03 14 29

The program must process each numerical argument in turn, converting each of the six strings into 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:

00000000000000000000000000000000

00000000000000000000000000010001

00000000000000000000000000000011

00000000000000000000000000001110

00000000000000000000000000011101

We treat these six values as the opcode, rs, rt, rd, shamt and funct fields, respectively. Next, using bitwise operations, we need to concatenate the six rightmost bits of the opcode, five rightmost bits of the rs, rt, rd and shamt fields, and 6 rightmost bits of the funct field:

When printed in hexadecimal, the output value (produced by syscall 34) will be Ox01 fl 1b9d.

characters that encode a positive integer. However, you 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 args_error and exit the program (via system call 10).

Note that the shamt field is applicable only when performing a shift instruction. 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 value, regardless of what the funct field is, and incorporate that 5-bit value in the final 32-bit value computed by your code.

Examples

Command-line Arguments Expected Output
R 00 15 17 03 14 29 Ox01f11b9d
R 00 02 31 24 05 42 Ox005fc16a
R 00 22 25 31 00 61 Ox02d9f83d

10

 
R 00 17 41 22 30 08 INVALID ARGS

Part 5: 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 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 more hands, the hand of highest rank is printed. If none of these five ranked hands is identified, the program prints HIGH CARD. 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 2), 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
1 Big bobtail big bobtail str BIG BOBTAIL
2 Full house full house str FULL HOUSE
3 Five and dime five and dime str FIVE AND DIME
4 Skeet skeet str SKEET
5 Blaze blaze str BLAZE
6 Everything else (i.e., High card) high card_str HIGH CARD

From Wikipedia:

I . Big bobtail: A four-card straight flush (four cards of the same suit in consecutive order). 2. Full house: three cards of one rank and two cards of another rank (example: 3-3-3-J-J).

  1. Five and dime: 5-low, 10-high, with no pair (example: 5-6-7-8-10).
  2. Skeet: A hand with a 2, a 5, and a 9, plus two other un-paired cards lower than 9 (example:

24-5-6-9).

  1. Blaze: All cards are Jacks, Queens, and/or Kings.
  2. High card: None of the above.

A single card is encoded as an ASCII character. Specifically, the ASCII code ranges employed are:

  • Ox41 – Ox4D represent the A through K of Clubs

 

For example, Ox63 represents the 3 of Diamonds and corresponds with the ASCII character ‘c’. Likewise, Ox4B represents the Jack of Clubs and corresponds with the ASCII character

An example of a valid hand would be the string “vX[qd”, which have ASCII codes:

= 6 of Hearts

— 8 of Spades

  • Ox5B = Jack of Spades
  • ox71 = Ace of Hearts

– 4 of Diamonds

Input Validation

You may assume that the second command-line argument always 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
P BVTUS 64445434 BIG BOBTAIL
P BDCEG BIG BOBTAIL
P RTtbB 24444’2+2* FULL HOUSE
P WUxj 1 FIVE AND DIME
P WRuiF 74245’9+6* SKEET
P TRSYU 442434945* BIG BOBTAIL
P [L}mK J4Q4K’K+J* BLAZE
P GIDuF HIGH CARD