/* * testUtil.c - testDriver for the functions of util.h * * "Copyright (c) 2014 by Fritz Sieker." * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright notice * and the following two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" * BASIS, AND THE AUTHOR NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * */ #include #include #include #include "Debug.h" #include "util.h" /** @mainpage cs270 Programming Assignment - Assembler Utilities * \htmlinclude "util.html" */ /** @file testUtil.c * @brief Driver to test functions of util.c (do not modify) * * @details This is a driver program to test the functions of util.c * The program accepts two command line parameters and executes one of the functions * of util.c. To understand the usage, type the testUtil. with no * parameters. This will give a brief description of each command and its parameter. *

* @author: Fritz Sieker */ /** Print a usage statement describing how program is used */ static void help() { puts("Usage: testUtil [-debug] \n"); puts("testUtil a2i (e.g. x41)> - prints value or error"); puts(" to test decimal value, quote it (e.g. '#-5')\n"); puts("testUtil ccode (e.g. np)> - prints cond code or -1"); puts("testUtil label (e.g. HERE)> - prints 'ok' or 'invalid'"); puts("testUtil op (e.g. ADD)> - prints numeric opcode or -1"); puts("testUtil reg (e.g. R3)> - prints register or -1"); } /** Print a usage statement describing how program is used, and exits */ static void usage() { help(); exit(1); } /** Entry point of the program * @param argc count of arguments, will always be at least 1 * @param argv array of parameters to program argv[0] is the name of * the program, so additional parameters will begin at index 1. * @return 0 the Linux convention for success. */ int main (int argc, const char* argv[]) { debugInit(&argc, argv); if (argc < 3) usage(); debug("'%s' invoked with %d parameters", argv[0], argc); // demo DEBUG const char* cmd = argv[1]; const char* tok = argv[2]; if (strcmp(cmd, "a2i") == 0) { int val; if (! util_get_int(tok, &val)) printf("'%s' is NOT a valid immediate\n", tok); else printf ("hex: 0%x, dec: %d\n", val, val); } else if (strcmp(cmd, "ccode") == 0) { printf("numeric cond-code: %d\n", util_parse_cond(tok)); } else if (strcmp(cmd, "label") == 0) { printf("%s\n", (util_is_valid_label(tok) ? "ok" : "invalid")); } else if (strcmp(cmd, "op") == 0) { printf("opcode: %d\n", util_get_opcode(tok)); } else if (strcmp(cmd, "reg") == 0) { printf("register number: %d\n", util_get_reg(tok)); } else { usage(); } return 0; }