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 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