MIPS汇编代写: CSE 220: Systems Fundamentals I Homework #1

CSE 220: Systems Fundamentals I

Homework #1

Fall 2016
Assignment Due: Sept. 23, 2016 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, loops, conditional logic and memory representations. In the first part of the assignment you will read in 2 ASCII characters strings and print their different interpretations. In the second part, you will generate random integer values and collect statistics on the numbers.

Part 1: Command-line arguments

In the first part of the assignment you will initialize your program and identify the different command line arguments.

• 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 and tell your program what operation to perform. To do this, we first need to tell Mars that we want to accept

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!

CSE 220 – Fall 2016 Homework #1 Page 1

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 .

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.

Tousetheargumentsinyourprogram,youwillneedtousethe $a0 and registers.The $a0 regis- ter will contain 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 an item that you specified on the command line.

In this assignment, we will be using two or three arguments. We will give you boilerplate code for obtain- ing these arguments further down in the assignment.

Setting up the .data section
Addthefollowingdirectivestothe .data sectiontodefinememoryspacefortheassignment:

.data
.align 2
numargs: .word 0
arg1: .word 0
arg2: .word 0
arg3: .word 0
Err_string: .asciiz "ARGUMENT ERROR"
# Helper macro for grabbing command line arguments
.macro load_args

You can expect that command line arguments will always be given in the correct order for this assign- ment.

$a1

$a1

CSE 220 – Fall 2016 Homework #1 Page 2

    sw $a0, numargs
    lw $t0, 0($a1)
    sw $t0, arg1
    lw $t0, 4($a1)
    sw $t0, arg2
    lw $t0, 8($a1)
    sw $t0, arg3
.end_macro

Writing the program

In your .text section, you will create a label main: . This is the main entry to your program. Next we will extract the command line arguments. You should do this before anything else to avoid losing the addresses to them. Also, never call this macro again, as you will 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, add code to check the number of command line arguments pro- vided. If there are < 2 arguments or > 3 arguments, the program should print out the string “ARGUMENT ERROR” and exit the program. The number of arguments is stored in memory at the label by the macro, but the value is also STILL in $a0 , as the macro code does not modify the contents of . Remember, values remain in registers until a new value is stored into the register, thereby overwriting it.

load_args isanassemblermacro.Macrosaredifferentfromfunctions.Weuseittosimplifyaccess to the command line arguments for this first assignment.

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.

numargs

$a0

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.

CSE 220 – Fall 2016 Homework #1 Page 3

If the number of arguments is valid, check the first argument provided on the command line. The first argument is valid if it has both of the following properties:

• isonly1characterinlength,and
• isupperorlowercaseASCIIcharactersfor‘A’or‘R’

Some other questions to ask yourself are:

If the first argument is not valid, print ”ARGUMENT ERROR” and exit the program. If the first argument is valid, write code to check between the possible cases ‘A’ or ‘R’ (upper or lower case). If the argument is ‘A’, then you will perform the operation defined in Part 2 of the assignment. If the argument is ‘R’, then Part 3 of the assignment will be performed. For now, use the string syscall to print the string “Part 2: ” or “Part 3: ” along with the value of the first argument to the output. You must create these strings in your .data section of your program.

Sample Outputs:

Arguments: Output:

Arguments: Output:

Arguments: Output:

To use the values placed in arg1 , arg2 , and arg3 , you will need to use load instruction(s) to get the value(s) stored in memory. The labels arg1, arg2, and arg3 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 uppercase and lowercase ASCII characters in their binary represen- tation?

CSE 220 – Fall 2016

Homework #1

Page 4

abcd abcd abcd
ARGUMENT ERROR

a cse220

Part 2: a

A abcd aBcD

Part 2: A

Arguments: Output:

Arguments: Output:

Arguments: Output:

Arguments: Output:

Arguments: Output:

Part 2: ASCII Characters

In this part of the assignment, you will analyze the first 4 ASCII characters of the second and third com- mand line arguments and print out information about these ASCII strings and then quit the program.

Comment out the lines of code which print “Part 2: ” and the value of the first argument. We will be replacing this functionality in this section.

First, since this part of the assignment requires 3 arguments, you must first check that 3 arguments were provided. If only 2 arguments were provided, print “ARGUMENT ERROR” and exit the program.

Output Sample:

Arguments: Output:

Next, load the first 4 ASCII characters of the second command line argument into a register. Remember, these ASCII characters are encoded binary which are interpreted when printing. Therefore, the 1’s and 0’s of their representation can be interpreted in different ways. To illustrate this, you will print the different representations of the binary in different number formats.

1 1234 5678

ARGUMENT ERROR
z abracadabra opensesame
ARGUMENT ERROR

R 20

Part 3: R

r rrrrr

Part 3: r

C3-P0 R2D2

ARGUMENT ERROR

a cse220

ARGUMENT ERROR

Assume that the strings for arg2 and arg3 will always be 4+ ASCII characters in length. The assign- ment WILL ONLY be tested for these cases.

Memory alignment of strings will make loading the 4 ASCII characters into a register more difficult thanjustusingthe lw instruction.Howcanyouloadthesecharactersfrommemoryandplacetheminto a register just as they are stored in memory?

Endianess matters! MIPS is Little Endian, therefore the first character of the string should be in the Least Significant Byte (LSB) of the register!

Print the value in your register in the following format:

ARG2: BINARY_VALUE 0xHEX_VALUE TWOS_COMPLEMENT ONES_COMPLEMENT SIGNED_MAGNITUDE

CSE 220 – Fall 2016 Homework #1 Page 5

Print the label string “ARG2: ”, the binary representation of the characters, the hexadecimal representa- tion of characters, the 2’s complement integer value of the number, the one’s complement value of the number, and the signed magnitude value of the number.

Beginbydefiningthelabelstringfor ARG2 inyour.datasection. You will need to:

  1. Printthelabelstring(syscall4)
  2. Printthebinaryrepresentationofthecharacters, BINARY_VALUE (syscall35)
  3. Printthehexadecimalvalueofthecharacters, 0xHEX_VALUE (syscall34)
  4. Print the value of the binary as if it were a 2’s complement number TWOS_COMPLEMENT (syscall 1)
  5. Print the value of the binary as if it were a 1’s complement number ONES_COMPLEMENT (syscall 100)
  6. PrintthevalueofthebinaryasifitwereaSignedMagnitudenumber SIGNED_MAGNITUDE(syscall 101)

To print sign magnitude and 1’s complement values using MARS, we created additional syscalls. These syscalls are not standard and will not appear in the official MARS documentation.

description syscall arguments print 1’s comp

print SM 101 $a0 = value to print

SampleOutputs: Arguments: Output:

Arguments: Output:

Repeat this process for the third argument.

Sample Outputs:

Arguments: A abcd aBcD Output:

result

100

$a0 = value to print

A abcd aBcD

ARG2:  01100100011000110110001001100001 0x64636261 1684234849 1684234849 1684234849

a 1234 7890

ARG2:  00110100001100110011001000110001 0x34333231 875770417 875770417 875770417

It is acceptable to “copy and paste” and modify the registers to do this, as we have not covered func- tions yet.

Make sure to keep the 4 characters of in a register. Do NOT overwrite them as we will use them again.

arg2

ARG2:  01100100011000110110001001100001 0x64636261 1684234849 1684234849 1684234849
ARG3:  01000100011000110100001001100001 0x44634261 1147355745 1147355745 1147355745

CSE 220 – Fall 2016

Homework #1

Page 6

Arguments: a 1234 7890 Output:

The last thing you will do is compare the hamming distance between the binary representations of these arguments. The Hamming distance between two binary representations of equal length is the number of bits which differ. In another way, it measures the minimum number of bit flips required to change one representation into the other. For example, for 7-bit binary numbers 1011101 and 1001001 the hamming distance is 2.

Calculate the hamming distance between the binary representations of arg2 and arg3 and print it to the output as shown in the example. After printing, the program should exit (syscall 10).

Hint: Which logical operation will tell you if two bits have different values?

Sample Output:

Arguments: A abcd aBcD Output:

Arguments: a 1234 7890 Output:

Part 3: Random Numbers

In this part of the assignment, you will use the random number generator in MARS to draw numbers based on the value of arg2 , the seed. For the numbers drawn you will keep track of statistics about these values. You will stop drawing numbers when the value is a power of 2 and the value is less than 64.

Comment out the lines of code which print “Part 3: ” and the value of the first argument. We will be replacing this functionality in this section.

First, since this part of the assignment requires only 2 arguments, you must first check that 2 arguments were provided. If 3 arguments were provided, print “ARGUMENT ERROR” and exit the program.

Next, you will need to convert the string in arg2 to an integer encoded in two’s complement. Pseudo code of the algorithm for converting the string to a number is presented below (positive numbers only). If an invalid character in the string is found, conversion should stop and the part of the number converted

ARG2:  00110100001100110011001000110001 0x34333231 875770417 875770417 875770417
ARG3:  00110000001110010011100000110111 0x30393837 809056311 809056311 809056311
ARG2:  01100100011000110110001001100001 0x64636261 1684234849 1684234849 1684234849
ARG3:  01000100011000110100001001100001 0x44634261 1147355745 1147355745 1147355745
Hamming Distance:  2
ARG2:  00110100001100110011001000110001 0x34333231 875770417 875770417 875770417
ARG3:  00110000001110010011100000110111 0x30393837 809056311 809056311 809056311
Hamming Distance:  7

Why are the 2’s complement, 1’s complement, and Signed Magnitude values always the same? Will they ever be different?

CSE 220 – Fall 2016 Homework #1 Page 7

shouldbeused.Ifthefirstcharacterof arg2 isaninvalidcharacter,thealgorithmwillresultinthe sum of 0.

var sum = 0
# Convert every character until the end of the string is reached
# or an invalid character is reached
for each character in the input:
    if character >= ’0’ and character <= ’9’:
        sum = (sum * 10) + (char - ’0’)

else: break

Sample Conversion:

arg2: “123” sum: 123

arg2: “1a3” sum: 1

arg2: “a” sum: 0

arg2: “23-4” sum: 23

arg2: “-10” sum: 0

Once arg2 isconvertedintoapositiveintegervalue(storedinregisteras2’scomplement),settheseed oftherandomnumbergeneratorusingsyscall40(Set $a0 to0).

Details of the syscalls for drawing random numbers can be found in greater detail in the MARS documen- tation. We have included an abbreviated version for reference.

description set seed

syscall

arguments result None

40

= i.d. of pseudorandom number generator (any int).
= seed for corresponding pseudorandom number generator.

$a0

$a1

random int range
For the random numbers which you will be drawing, you will need to collect the following statistics:

• Totalnumberofvaluesdrawn
CSE 220 – Fall 2016 Homework #1 Page 8

42

= i.d. of pseudorandom number generator (any int). = upper bound of range of returned values.

next value

$a0

$a0

$a1

• Totalnumberofoddvaluesdrawn
• Totalnumberofvalueswhichareapowerof2
• Totalnumberofvalueswhichwereamultipleof2 • Totalnumberofvalueswhichwereamultipleof4 • Totalnumberofvalueswhichwereamultipleof8

Create a while loop to continue drawing random values in the range of 1-1024 (ie. 1+ rand()%1024 ), using syscall 41, until the value is a power of 2 AND is less than 64. Given the value drawn, update all statistics. When the while loop terminates, print out all the statistics and the last random number drawn. After printing, the program should exit (sycall 10).

Sample Output:

Arguments: r 0 Output:

Last value drawn:  2
Total values:  28

# of Even: 12

#ofOdd: 16

Power of 2: 1

Multiple of 2:  12
Multiple of 4:  9
Multiple of 8:  3

Arguments: r 10 Output:

Last value drawn:  1
Total values:  564

# of Even: 286

# of Odd: 278

Power of 2: 3

Multiple of 2:  286
Multiple of 4:  140
Multiple of 8:  62

Arguments: R 220 Output:

Last value drawn:  16
Total values:  342

# of Even: 155

CSE 220 – Fall 2016

Homework #1

Page 9

# of Odd: 187

Power of 2: 4

Multiple of 2:  155
Multiple of 4:  76
Multiple of 8:  39

Hand-in instructions

Ifyoucompletedallpartsoftheassignmentyourprogramshouldsupportthe A and R flagsonly.Please 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.

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 – Fall 2016 Homework #1 Page 10