编程辅导 C C++ Java Python MIPS Processing 网络家教 在线辅导

编程家教 远程写代码 Debug 讲解答疑 课后答疑 不是中介,本人直接答疑

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

 As you become increasing familiar with the *nix environment, most programs will have some sort of usage statement if you just run the program with no arguments. Here, we present you the usage st

 As you become increasing familiar with the *nix environment, most programs will have some sort of usage statement if you just run the program with no arguments. Here, we present you the usage statement of your program, and the conditions you must satisfy to complete this assignment.
$ ./mstat -h
Usage: ./mstat [OPTION]
./mstat -h Displays this help menu.
./mstat -i [-u] Displays statistics about instruction types.
./mstat -r [-u] Displays information about the registers.
./mstat -o [-u] Displays number and percentage of opcodes used.
Optional flags:
-u Displays human readable headers for the different outputs.
ĕ The -u argument is optional and even redundant if paired with -h as the help menu is already
in a human readable format. Your program will have three different output formats. In addition to these outputs you must implement a user help menu or usage similar to the standard help menu found in programs like ls or grep . Your help menu and several output formats will be denoted by command line flags.
ĕ Try typing ls --help to see what an ideal help menu looks like.
Q You are not permitted to use an argument parsing library such as getopt for this assignment
you must practice parsing the arguments yourself. These command arguments will be accessible in your code via the argv array of size argc . For ease of this first assignment, you can assume that all the program arguments will always appear in the same order.
Stdin, Stdout, and Stderr Your program will read input from stdin and write output to stdout and write descriptive error strings to stderr . You should become familiar with what these three files represent. These are known as the
standard streams. Prior to Unix, computer programs needed to specify and be connected to a particular I/O device such as magnetic tapes. This made portability nearly impossible. Later in the course we will delve deeper into “files” and how they represent abstract devices in Unix-like operating systems. For now understand that they work much like your typical .txt file they can written to and read from. Here are some methods of reading and writing to the standard I/O streams.
#include <unistd.h>
ssize_t read (int filedes, void *buf, size_t count);
ssize_t write(int filedes, void *buf, size_t count);
#include <stdio.h>
int fscanf (FILE *stream, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
ĕ Note the read and write calls take the same arguments as MIPS read and write syscalls, this
is an example of a high-level interface to a low-level syscall. Typically, this is the arrangement of file descriptors to standard streams.
I/O File descriptor FILE* stdin 0 FILE* stdout 1 FILE* stderr 2 A file descriptor is simply an integer that is an indicator of a particular file. For example, if we were to open a file with
#include <sys/stat.h>
#include <fcntl.h>
int fd;
char *buffer[100];
if( (fd = open("test.txt", O_RDONLY) < 0){
exit(EXIT_FAILURE);
} we could then read from it using
#include <unistd.h>
ssize_t bytes = read(fd, buffer, 100); There are many ways to access and handle files. Different approaches suit the needs of different programs. Read this page on approaches for handling files in C.
Input to your program Your program will accept input via stdin . The input will consist of strings which are the hexadecimal representations of MIPS instructions (one instruction per line). Input Example File:
0x3c011001
0x3424024f
0x24020004
0x0000000c
0x24020005
0x0000000c
0x24080001
0x24090002
0x240a0003
0x1048000f
0x1049000e
0x104a017e
0x00404025
0x3c011001
ĕ You should use fgets, scanf, read, or fread to get input from the user. Then use the function
strtoull to parse and error check your input.
ĕ You can create your own input file with your own MIPS instructions using MARS. Take any Mips
program, and assemble it. After assembling press the command ctrl-d . This should pop up a
menu about dumping a section of your program. For the memory segment select the .text section
and for the dump format select Hexadecimal Text. Finally select Dump to File… which will ask you
were you want to save your instruction dump. This file won’t have 0x at the beginning of each hex
number you can easily add this though. Use $ sed -i.bak 's/^/0x/' filename in the
command line. Put the name of your file where “filename” is in the command. The argument -
i.bak creates a backup of your file in case of an error.
ĕ You will have to use C bitwise operations after parsing the hex values to figure out what type of
instructions they are. Your program will then parse the instructions to gather the statistical data required to create the data tables shown with each flag option. To provide input to your program you will need to use the I/O redirection operators to take the content from the sample files and provide it to stdin of your program.
ĕ You can find I/O redirection explanations and tutorials at the source. If the tldp explanation is too
dense for you to understand, you can find a more beginner friendly tutorial here. To provide the input to your program, you could redirect the instructions from the sample file to the
stdin of your program using the < operator. This can be done like: $ ./mstat -r <
instructions.txt Alternatively you can use the pipe | operator to send the output of another program (such as cat ) to the stdin of your program. This can be done like: $ cat instructions.txt | ./mstat -r
Instruction Information
-i Instruction Information When given the command line flag -i your program will produce some simple statistics in the following format. Each line will contain the instruction type, the count of this instruction type given in the input, and the percentage of this instruction type (based on the total number of instructions provided in the input). Upon successful completion the program should return EXIT_SUCCESS . Example output:
$ cat instructions.txt | ./mstat -i
I-Type 303 62.9%
J-Type 58 12%
R-Type 121 25.1%
$ echo $?
0 When additionally given the command line flag -u the printout will display a header labeling what the columns for the output means. This flag can ONLY be used in conjunction with another flag.
ĕ This is only useful for humans, the -u flag should not be provided when giving your output to
another program. Example output of -i with -u :
$ cat instructions.txt | ./mstat -i -u
TYPE COUNT PERCENT
I-Type 303 62.9%
J-Type 58 12%
R-Type 121 25.1%
$ echo $?
0
-r Register Information When given the command line flag -r your program should list out the statistics of each register. There is a column for how many times it was used in total, how many r-type instructions used it, how many i-type instructions used it, how many j-type instructions used it, and the percentage of times the register was being used based on all the other registers. Upon successful completion the program it should return
EXIT_SUCCESS . Example output:
$ cat instructions.txt | ./mstat -r
$0 468 212 256 0 97.1%
$1 80 12 68 0 16.6%
$2 71 15 56 0 14.7%
$3 0 0 0 0 0%
$4 92 20 72 0 19.1%
... Removed rest of output for brevity
$31 4 4 0 0 1.2%
$ echo $?
0 When your program receives the -u flag then it should add column headers as the previous section described. Furthermore, you should print out the human readable names of the registers that you are familiar coding with in MIPS. Example output of -r with -u :
$ cat instructions.txt | ./mstat -r -u
REG USE R-TYPE I-TYPE J-TYPE PERCENT
$zero 468 212 256 0 97.1%
$at 80 12 68 0 16.6%
$v0 71 15 56 0 14.7%
$v1 0 0 0 0 0%
$a0 92 20 72 0 19.1%
... Removed rest of output for brevity
$fp 0 0 0 0 0%
$ra 11 0 11 0 2.3%
$ echo $?
0
-o Opcode Information When given the command line flag -o your program should list out the statistics of each opcode used. There are 127 possible opcodes (2 - 1). You should keep track of the number of times an opcode is used and the percentage used in the entire program. For the instructions with opcode 0x00 , also known as R-type instructions, you should keep track of the function field and its usage percentage with respect to the total number of instructions with opcode 0x00 . If done correctly the percentage of instructions with
(6+1) opcode 0x00 should equal the percentage of R-type instructions you see when you run your program with -i . Note the single line break between opcode and function information. Example output:
$ cat instructions.txt | ./mstat -o
0x0 121 25.1%
0x1 20 4.1%
0x2 1 0.2%
0x3 27 5.6%
0x4 16 3.3%
... Removed rest of output for brevity
0x7C 0 0%
0x7D 0 0%
0x7F 0 0%
0x0 62 51.2%
0x1 80 66.1%
0x2 9 7.4%
... Removed rest of output for brevity
0x7F 0 0% Note with the -u flag there is a single line break between opcode and function header information. Example output of -o with -u :
$ cat instructions.txt | ./mstat -o -u
OPCODE COUNT PERCENTAGE
0x0 62 12.9%
0x1 80 16.6%
0x2 9 1.9%
0x3 5 1%
... Removed rest of output for brevity
0x7F 2 1.2%
FUNC COUNT PERCENTAGE
0x0 62 51.2%
0x1 80 66.1%
0x2 9 7.4%
... Removed rest of output for brevity
0x7F 0 0%
Fail conditions
Fail conditions Your program must be given one of the arguments -h , -i , -r , or -o . If your program is run without any of these arguments it should exit gracefully but with an EXIT_FAILURE status. Also, whenever a program is run improperly it usually prints out its usage statement, yours should do the same. The -u argument is for use in tandem with the -i , -r , or -o argument. If -u is passed to your program it should format the output into a human readable format, so if -u is given without -i ,
-r , or -o then you should print the usage statement and return EXIT_FAILURE . If your program cannot parse a hexadecimal value in the input file (e.g, 0xzbq3 ) then it should return EXIT_FAILURE and print the improper hex value to stderr . This is not an exhaustive list of errors, use good judgement to handle other errors you may encounter. Unless otherwise specified use EXIT_SUCCESS or EXIT_FAILURE whenever your program exit. If there is a failure, print a descriptive string as to why it failed to stderr along with the usage of your program. Essentially your program should never crash it should always exit gracefully.
ĕ There is an environment variable ? that holds the exit value of the previously run program. So
you can check you’re program’s exit code by typing $ echo $? in the command line.
Combining your program with other UNIX
tools If you make a good Unix tool, then you should be able to use it in tandem with other existing tools. You should become familiar with using some of the following tools. When we grade your assignment we will be combining it with other Unix tools. You should be testing your program this way as well.
Sample program combinations Assume that your program produces the following output:
$ ./mstat -i < instructions.txt
I-Type 303 62.9%
J-Type 58 12%
R-Type 121 25.1% Now lets chain this together with the sort program. We will sort the rows based on the numeric values in the second column.
$ ./mstat -i < instructions.txt | sort -k2n
J-Type 58 12%
R-Type 121 25.1%
I-Type 303 62.9% Now lets use grep to search for a value. We will sort the rows based on the numeric values in the second column in descending order and then use grep to look for any row that contains the char 1.
$ ./mstat -i < instructions.txt | sort -k2nr | grep 1
J-Type 58 12%
R-Type 121 25.1% Now lets use wc to count how many results we have:
$ ./mstat -i < instructions.txt | sort -k2nr | grep 1 | wc -l
2
ĕ The programs wc, grep, sort, head , tail are some common tools which are used to help format
and examine the output of programs. They usually come installed with most Linux and BSD
distributions.
Q Just because we showed these commands, it does not mean this is the only way we will test your
program. This is just a good way to gauge that your program is working correctly but it is NOT the end
all be all. Test your program appropriately.
MIPS Instruction Format Reference For reference, the MIPS instruction formats are as follows:
R-type:
OPCODE RS RT RD SHAMT FUNC 000000 5-BITS 5-BITS 5-BITS 5-BITS 6-BITS
I-type:
OPCODE RS RT IMMEDIATE 6-BITS 5-BITS 5-BITS 16-BITS
ĕ In this assignment, opcodes for Immediate instructions are any values other than 0, 2, or 3, which
are used for the other instruction formats.
J-type:
OPCODE ADDRESS 000010 26-BITS 000011 26-BITS
ĕ Registers can be specified in the RS, RT and RD fields of the instructions. You will need to mask
out the values of each of these fields to collect the statistics about register usage.
ĕ A sheet containing all the MIPS reference data has been provided for you on piazza in the
resources section.
Hand-in instructions (
You are expected to hand in at minimum the following files in the hw1 folder of your git submission: 1. All *.c and *.h files you have created for the assignment 2. Makefile - It must include the following: all target mstat target The mstat target must produce a binary called mstat clean target 3. README - It must contain the following information: name SBUID # Partners (none for this assignment) Anything relevant to grading your project when errors may occur
Your assignment is expected to work on the following platforms: 1. Ubuntu Desktop x86_64 15.10
Submitting your assignment
REMEMBER Do not submit at the last minute. We will be using the time stamp of the commit associated with the tag to determine if your homework assignment is late or not. On top of that we will NOT GRADE
late assignments 1. Make sure all files for this assignment are in a hw1 directory. 2. To tag your submission, make sure first that you have pulled from the remote and all code changes are merged. Once everything is merged, push it back to the remote server. Be sure you are done
making any and all changes before proceeding. Tags cannot be deleted. 3. Log on to your gitlab account and navigate to the tags page from your repository’s main page. 4. Here you can click the green New Tag button. 5. Enter hw1 for the tag name, the name of your current branch (typically master ) and an optional completion message.
Do not put important information in the message as we will not necessarily see it. Any relevant information to your submission should be in your hw1/README 6. To check if you submitted properly, you should now see a hw1 tag in the list of tags for your repository.
ĕ When writing your program try to comment as much as possible. Try to stay consistent with your
formatting. It is much easier for your TA and the professor to help you if we can figure out what your
code does quickly.

相关推荐