Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.56 KB | None | 0 0
  1.    /**************************************************************************
  2.      *
  3.      * Filename: Ace2.c
  4.      *
  5.      * Synopsis:
  6.      *
  7.      * Purpose:
  8.      *
  9.      * Author:
  10.      *      Louise Watson, Reg no: 201347438
  11.      *
  12.      * Group:
  13.      *      Thu 3-5
  14.      *
  15.      * Promise: I confirm that this submission is all my own work.
  16.      *
  17.      *          (Signed)_____________________________________(Louise Watson)
  18.      *
  19.      *
  20.      * Version: See VERSION below
  21.      *
  22.      *********************************************************************/
  23.      
  24.      /**************************************************************************
  25.      *Version History
  26.      *V1.0 Removed most of the unnecessary code provided in Duncan's Sample code in order to
  27.      *begin Ace2. Pasted in Get Hex provided by Duncan.
  28.      *V1.1 Added in program counter to code and changed '6' to '2' so it will shift 2 bits
  29.      *instead of 6 bits.
  30.      *v1.2 Used the previously defined Opcode function and used OpDigits to get the first six digit
  31.      *opcode from the first hex number. This allowed me to use OpDigits to check if it was I, r or J
  32.      *type instruction. Done this by creating an array of J-type instructions and using an if
  33.      *statement. This will compare if OpDigits is equal to 0, if so, return r, compare to J, if equal
  34.      *return j, else it will return it to be of an I type instruction.
  35.      *v1.3 Trying to correct as many errors as possible by adding in Uint_t before my program counter
  36.      *
  37.       *********************************************************************/
  38.      
  39.     #define VERSION "Ace 2. Version 1.4.  by Louise Watson, Last Update 23/11/2014\n"
  40.     #define USAGE "Usage: %s <regNo>\n",argv[0]
  41.      
  42.     #include <stdio.h>
  43.     #include <limits.h>
  44.     #include <ctype.h>
  45.     #include <stdlib.h>
  46.     #include <string.h>
  47.      
  48.     /* #include "p4sample.h */
  49.     /* ** Start of p4sample.h (if the following were moved into that .h file) ** */
  50.      
  51.     #if SHRT_MAX == 2147483647
  52.     typedef unsigned short int uint32_t;
  53.     #elif INT_MAX == 2147483647
  54.     typedef unsigned int uint32_t;
  55.     #elif LONG_MAX == 2147483647
  56.     typedef unsigned long uint32_t ;
  57.     #elif LLONG_MAX == 2147483647
  58.     typedef unsigned long long uint32_t;
  59.     #else
  60.     #error "Cannot find 32bit integer."
  61.     #endif
  62.   /* On some systems an UINT32_MAX is already defined, if not... */
  63. #ifndef UINT32_MAX
  64. #define UINT32_MAX ((uint32_t)-1)
  65. #endif
  66.  
  67. /* Note on the following #define suffix nomenclature:
  68.    _L = Least significant bit position of the field
  69.    _M = Most significant bit position of the field
  70.    _SZ = Size of the field
  71. */
  72. /* Fields in the lower 16 bits of the MIPS instruction format: */
  73. /* I-type field in lower 16 bits */
  74. #define I_L 0
  75. #define I_M 15
  76. #define I_SZ ((I_M-I_L)+1)
  77.  
  78. /* R-type fields in lower 16 bits */
  79. /* Function */
  80. #define FN_L 0
  81. #define FN_M 5
  82. #define FN_SZ ((FN_M-FN_L)+1)
  83. /* Shamt - shift amount */
  84. #define SH_L 6
  85. #define SH_M 10
  86. #define SH_SZ ((SH_M-SH_L)+1)
  87. /* Register RD */
  88. #define RD_L 11
  89. #define RD_M 15
  90.  
  91. /* Fields in the upper 16 bits of the MIPS instruction format: */
  92. /* I-type and R-type share a commmon set of fields in the upper 16 bits */
  93. /* Register RT */
  94. #define RT_L 16
  95. #define RT_M 20
  96. /* Register RS */
  97. #define RS_L 21
  98. #define RS_M 25
  99. /* Registers RD, RT and RS are all the same size */
  100. #define R_SZ ((RD_M-RD_L)+1)
  101.  
  102. /* Opcode is in the top 6 bits for _all_ instructions */
  103. #define OP_L 26
  104. #define OP_M 31
  105. #define OP_SZ ((OP_M-OP_L)+1)
  106.  
  107. /* The J(ump) instruction is a special case */
  108. /* J-type uses all the bits other than the opcode field */
  109. #define J_L 0
  110. #define J_M 25
  111. #define J_SZ ((J_M-J_L)+1)
  112.  
  113. /* The IEEE 754 Single Precision FP fields */
  114. /* Sign Bit */
  115. #define FPS_L 31
  116. #define FPS_M 31
  117. #define FPS_SZ ((FPS_M-FPS_L)+1)
  118. /* Exponent Bits */
  119. #define FPE_L 23
  120. #define FPE_M 30
  121. #define FPE_SZ ((FPE_M-FPE_L)+1)
  122. /* Fraction Bits */
  123. #define FPF_L 0
  124. #define FPF_M 22
  125. #define FPF_SZ ((FPF_M-FPF_L)+1)
  126. /* IEEE 754 uses a Biased Exponent */
  127. #define IEEE754_BIAS 127
  128.  
  129. /* INERR is an Input Error */
  130. #define INERR -1
  131.  
  132. /* The following union allows the unsigned 32 bits to also be interpreted as
  133.    a 32-bit floating point number */
  134. typedef union {
  135.   uint32_t i;
  136.   float f;
  137.   } fp_i_t;
  138.  
  139. /* The following structs represent the fields in the various MIPS instructions.
  140.    The members of the struct include:
  141.    - the 32-bit representation of the instruction in the .in member
  142.    - the contents of the field as an integer value, e.g. 14 in .rs
  143.    - the (pointer to the) corresponding bit string, e.g. &"01110" in .rsbits
  144.    - the (pointer to the) name/label of the field, e.g. &"RS" in .rsname
  145. */
  146.      
  147. typedef struct {
  148.   uint32_t in;
  149.   int  op; char *opbits; char *opname;
  150.   int  rs; char *rsbits; char *rsname;
  151.   int  rt; char *rtbits; char *rtname;
  152.   int  rd; char *rdbits; char *rdname;
  153.   int  sh; char *shbits; char *shname;
  154.   int  fn; char *fnbits; char *fnname;
  155. } mips_rtype_t;
  156.  
  157. typedef struct {
  158.   uint32_t in;
  159.   int  op; char *opbits; char *opname;
  160.   int  rs; char *rsbits; char *rsname;
  161.   int  rt; char *rtbits; char *rtname;
  162.   int  immediate; char *ibits; char *iname;
  163. } mips_itype_t;
  164.  
  165. typedef struct {
  166.   uint32_t in;
  167.   int  op; char *opbits; char *opname;
  168.   int  address; char *jbits; char *jname;
  169. } mips_jtype_t;
  170.  
  171. typedef struct {
  172.   fp_i_t in;
  173.   int  s; char *sbits; char *sname;
  174.   int  e; char *ebits; char *ename;
  175.   int  f; char *fbits; char *fname;
  176. } ieee_754_t;
  177.  
  178. /* The following is a macro that 'extracts' from the bits passed in the
  179.    value of the field of size bit_sz that begins at bit position bit_l.
  180.  */
  181. #define IN(bits,bit_l,bit_sz) ((bits >> bit_l) & ((1 << bit_sz) - 1))
  182.  
  183. /* The PC value as stipulated in the specification */
  184. #define PC 0X10000000
  185.  
  186. /* ** End of p4sample.h (if the above had been contained in that .h file) ** */
  187.  
  188. /* Start of the actual code */
  189.  
  190. /* Synopsis:
  191.  * #include <ctype.h>
  192.  * int isnumeric(char *s)
  193.  *
  194.  * Description:
  195.  * A convenience function similar to isdigit() except that it works for
  196.  * strings rather than single characters.  
  197.  *
  198.  * Returns:
  199.  * The same result as isdigit()
  200.  */
  201. int isnumeric(char *s) {
  202.     int result;    /* Current isdigit() return value */
  203.     do                           /* Check that each character of the...*/
  204.         result = isdigit(*s++);  /* ...string is a digit and move on...*/
  205.     while (result && *s) ;       /* ...until non-digit found or at EOS */
  206.     return result;  /* Return result of last isdigit() call */
  207. }
  208.  
  209. /* Synopsis:
  210.  * uint32_t strtouint32(const char * restrict s,int base)
  211.  *
  212.  * Description:
  213.  * Converts a string into an uint32_t (32 bit unsigned) integer value.
  214.  * A conversion function in the style of strtol(). The character string s is
  215.  * interpreted as a numeral to the base specified by the second parameter base.
  216.  * The string should only contain digits between 0 and 9 even though the
  217.  * the base can be >10. This restriction is allowed since only 9 digit
  218.  * student registration numbers are expected to be converted. If the input
  219.  * string is not a valid numeral then the return value is UINT32_MAX.
  220.  *
  221.  * Returns:
  222.  * The numeric value of the string or UINT32_MAX if string isn't numeric
  223.  */
  224. uint32_t strtouint32(char *s,int base) {
  225.     uint32_t result = 0;
  226.     if (!isnumeric(s))    /* Basic sanity check on input string */
  227.       return(UINT32_MAX); /* UINT32_MAX uses as error indicator */
  228.     while (*s)
  229.         result = (result * base) + ((*s++) - '0'); /* Convert */
  230.     return (result);
  231. }
  232.  
  233. /* Synopsis:
  234.  * #include <stdlib.h>
  235.  * char* bitstostr (uint32_t bits, int size, int split)
  236.  *
  237.  * Description:
  238.  * Converts the first parameter into a character string that contains the
  239.  * bit pattern equivalent of the internal representation of the integer.
  240.  * The second parameter is the number of bits to be inserted into the string.
  241.  * The third parameter determines the number of bits to be grouped together.
  242.  * A value of 0 for split means that no splitting is to take place. Non-zero
  243.  * values for split cause a space to be inserted into the string after split
  244.  * bits have been inserted.
  245.  *
  246.  * Returns:
  247.  * The return value is a pointer to the (dynamically allocated) string of bits.
  248.  * However, if the string cannot be allocated, it returns a pointer to the
  249.  * "Calloc Failed" error string. This is rather a kludge since calloc is not
  250.  * expected to fail and so the user may assume it succeeds and won't bother
  251.  * checking the return result. Better error reporting/handling is left as an
  252.  * exercise to the reader!
  253.  */
  254. char* bitstostr (uint32_t bits, int size, int split) {
  255.     char *bitsPtr;  /* Pointer to the bit string being built */
  256.     int stringsize, /* (Maximum) Size of the string that's required */
  257.         splitter;   /* Countdown count until a splitting space insertion */
  258.     stringsize = (split)?size+size/split:size; /* Calculate size with splits */
  259.     splitter = split; /* Initialise countdown to a splitting space insertion */
  260.     /* Now we know the maximum number of characters needed calloc() them. */  
  261.     if (NULL == (bitsPtr = calloc(stringsize+1,sizeof(char))))
  262.       return("Calloc Failed"); /* Left as an exercise for future improvement */
  263.     /* We now have our array initialised to '\0's so no need to plant an EOS */
  264.     bitsPtr += stringsize; /* String is built in reverse so start at the end */
  265.     /* Now perform the conversion. The (bits&1) mask is used to pick off the
  266.      * lowest bit of the number.
  267.      */
  268.     for (  ; size-- ; bits >>= 1) { /* Keep shifting bits down until all done */
  269.         *--bitsPtr = (bits&1)?'1':'0';    /* (Back)Fill string with 1s and 0s */
  270.         if (splitter > 0)          /* Do the bits get split up into groups?   */
  271.           if (0 == (--splitter)) { /* if so do we split them this time round? */
  272.             *--bitsPtr = ' ';      /* Yes, so insert a space into the string  */
  273.             splitter = split;      /* ...and reset the split countdown count. */
  274.           }
  275.     }
  276.     if (' ' == *bitsPtr) ++bitsPtr; /* Skip any leading space */
  277.     return(bitsPtr);
  278. }
  279.  
  280. /* The following init_? functions initialise the appropriate members of the
  281.    data structure with the values derived from the first parameter - bits.
  282.    The second parameter in each function is a pointer to the data structure.
  283.    The semantics of each function should be self-explanatory ;)
  284. */
  285.  
  286. void init_rtype(uint32_t bits, mips_rtype_t *rtype) {
  287.   rtype->in = bits;
  288.   rtype->op = IN(bits,OP_L,OP_SZ);
  289.   rtype->opbits = bitstostr(rtype->op,OP_SZ,0);
  290.   rtype->rs = IN(bits,RS_L,R_SZ);
  291.   rtype->rsbits = bitstostr(rtype->rs,R_SZ,0);
  292.   rtype->rt = IN(bits,RT_L,R_SZ);
  293.   rtype->rtbits = bitstostr(rtype->rt,R_SZ,0);
  294.   rtype->rd = IN(bits,RD_L,R_SZ);
  295.   rtype->rdbits = bitstostr(rtype->rd,R_SZ,0);
  296.   rtype->sh = IN(bits,SH_L,SH_SZ);
  297.   rtype->shbits = bitstostr(rtype->sh,SH_SZ,0);
  298.   rtype->fn = IN(bits,FN_L,FN_SZ);
  299.   rtype->fnbits = bitstostr(rtype->fn,FN_SZ,0);
  300.   return;
  301. }
  302.  
  303. void init_itype (uint32_t bits,mips_itype_t *itype) {
  304.   itype->in = bits;
  305.   itype->op = IN(bits,OP_L,OP_SZ);
  306.   itype->opbits = bitstostr(itype->op,OP_SZ,0);
  307.   itype->rs = IN(bits,RS_L,R_SZ);
  308.   itype->rsbits = bitstostr(itype->rs,R_SZ,0);
  309.   itype->rt = IN(bits,RT_L,R_SZ);
  310.   itype->rtbits = bitstostr(itype->rt,R_SZ,0);
  311.   itype->immediate = IN(bits,I_L,I_SZ);
  312.   itype->ibits = bitstostr(itype->immediate,I_SZ,0);
  313.   return;
  314. }
  315.  
  316. void init_jtype (uint32_t bits,mips_jtype_t *jtype) {
  317.   jtype->in = bits;
  318.   jtype->op = IN(bits,OP_L,OP_SZ);
  319.   jtype->opbits = bitstostr(jtype->op,OP_SZ,0);
  320.   jtype->address = IN(bits,J_L,J_SZ);
  321.   jtype->jbits = bitstostr(jtype->address,J_SZ,0);
  322.   return;
  323. }
  324.  
  325. void init_ftype (uint32_t bits,ieee_754_t *ftype) {
  326.   ftype->in.i = bits;
  327.   ftype->s = IN(bits,FPS_L,FPS_SZ);
  328.   ftype->sbits = bitstostr(ftype->s,FPS_SZ,0);
  329.   ftype->e = IN(bits,FPE_L,FPE_SZ);
  330.   ftype->ebits = bitstostr(ftype->e,FPE_SZ,0);
  331.   ftype->f = IN(bits,FPF_L,FPF_SZ);
  332.   ftype->fbits = bitstostr(ftype->f,FPF_SZ,0);
  333.   return;
  334. }
  335.  
  336. /* A wrapper function for all the data structures to be initialsed */
  337. void init_all_types(uint32_t bits,
  338.                     mips_rtype_t *rtype,
  339.                     mips_itype_t *itype,
  340.                     mips_jtype_t *jtype,
  341.                     ieee_754_t *ftype) {
  342.   init_rtype(bits,rtype);
  343.   init_itype(bits,itype);
  344.   init_jtype(bits,jtype);
  345.   init_ftype(bits,ftype);
  346.   return;
  347. }
  348.  
  349. /* The following print_? functions are really just wrappers round the
  350.    printf()s. This is intended to be a help when (in no particular order):
  351.    - reading the code
  352.    - understanding the code
  353.    - maintaining the code
  354.    - adapting the code
  355. */
  356.  
  357. void print_rtype (mips_rtype_t *rtype) {
  358.   printf("R-Type fields:\t+ %6s + %5s + %5s + %5s + %5s + %6s +\n",
  359.                      rtype->opname,
  360.                            rtype->rsname,
  361.                                  rtype->rtname,
  362.                                        rtype->rdname,
  363.                                              rtype->shname,
  364.                                                    rtype->fnname);
  365.   printf("\tBits:\t| %s | %s | %s | %s | %s | %s |\n",
  366.              rtype->opbits,
  367.                   rtype->rsbits,
  368.                        rtype->rtbits,
  369.                             rtype->rdbits,
  370.                                  rtype->shbits,
  371.                                       rtype->fnbits);
  372.   printf("\tValues:\t+ %6d + %5d + %5d + %5d + %5d + %6d +\n",
  373.                 rtype->op,
  374.                       rtype->rs,
  375.                             rtype->rt,
  376.                                   rtype->rd,
  377.                                         rtype->sh,
  378.                                               rtype->fn);
  379. }
  380.  
  381. void print_itype (mips_itype_t *itype) {
  382.   printf("I_Type fields:\t+ %6s + %5s + %5s + %16s +\n",
  383.          itype->opname,itype->rsname,itype->rtname,itype->iname);
  384.   printf("\tBits:\t| %s | %s | %s | %s |\n",
  385.          itype->opbits,itype->rsbits,itype->rtbits,itype->ibits);
  386.   printf("\tValues:\t+ %6d + %5d + %5d + %11hd(dec) +\n",
  387.          itype->op,itype->rs,itype->rt,(short)itype->immediate);
  388. }
  389.  
  390. void print_jtype (mips_jtype_t *jtype) {
  391.   printf("J-Type fields:\t+ %6s + %26s +\n",jtype->opname,jtype->jname);
  392.   printf("\tBits:\t| %s | %s |\n",jtype->opbits,jtype->jbits);
  393.   printf("\tValues:\t+ %6d + %21X(hex) +\n",jtype->op,jtype->address);
  394.                  
  395. }
  396.  
  397. void print_ieee_754 (ieee_754_t *ftype) {
  398.   printf("FP fields:\t+ %1s + %8s + %23s +\n",
  399.          ftype->sname,ftype->ename,ftype->fname);
  400.   printf("\tBits:\t| %s | %s | %s |\n",ftype->sbits,ftype->ebits,ftype->fbits);
  401.   printf("\tValues:\t+ %1d + %8d + %18d(ten) +\n",ftype->s,ftype->e,ftype->f);
  402. }
  403.  
  404. void print_all_types(mips_rtype_t *rtype,
  405.                      mips_itype_t *itype,
  406.                      mips_jtype_t *jtype,
  407.                      ieee_754_t *ftype) {
  408.   print_rtype(rtype);
  409.   print_itype(itype);
  410.   print_jtype(jtype);
  411.   print_ieee_754(ftype);
  412.   return;
  413. }
  414.  
  415. /* The following print_? functions just format the instructions as they
  416.    would appear in MIPS assembly language.
  417. */
  418. void print_i_type_mem(mips_itype_t *itype) { /* Load/Store instructions */
  419.   printf("%s\t$%d,%hd($%d)\n",
  420.           itype->opname,itype->rt,(short)itype->immediate,itype->rs);
  421.   return;
  422. }
  423.  
  424. void print_i_type_alu(mips_itype_t *itype) { /* I-type ALU instructions */
  425.   printf("%s\t$%d,$%d,%hd\n",
  426.          itype->opname,itype->rt,itype->rs,(short)itype->immediate);
  427.   return;
  428. }
  429.  
  430. void print_r_type_shift(mips_rtype_t *rtype) { /* R-type shift instructions */
  431.   printf("%s\t$%d,$%d,%d\n",
  432.          rtype->opname,rtype->rd,rtype->rt,rtype->sh);
  433.   return;
  434. }
  435.  
  436. void print_r_type(mips_rtype_t *rtype) { /* R-type ALU instructions */
  437.   printf("%s\t$%d,$%d,$%d\n",
  438.          rtype->opname,rtype->rd,rtype->rs,rtype->rt);
  439.   return;
  440. }
  441.  
  442. void print_j_type(mips_jtype_t *jtype) { /* J-type instruction(s) */
  443.   printf("%s\t%08X(hex)\n",
  444.          jtype->opname,((jtype->address << 2) | (PC & 0XF0000000)));
  445.   return;
  446. }
  447.  
  448. void print_ieee_754_actual(ieee_754_t *ftype) { /* IEEE 754 FP (32-bit) */
  449.   float actualf = 1.0+((float)ftype->f/(1 << FPF_SZ));
  450.   int actuale = ftype->e - IEEE754_BIAS;
  451.   print_ieee_754(ftype);
  452.   printf("\tActual:\t  %c     2^(%3d) %24f\n",
  453.          (ftype->s)?'-':'+',actuale,actualf);
  454.   printf("\tResult:\t  %c%f * 2^(%3d) -> %15G\n",
  455.          (ftype->s)?'-':'+',  /* Translate sign bit of the FP */
  456.          actualf,    /* Converted (actual) fraction of the FP */
  457.          actuale,    /* Converted (actual) exponent of the FP */
  458.          ftype->in.f /* Interpret bits as the value of the FP */  
  459.          );
  460.   return;
  461. }
  462.  
  463.  
  464.  uint32_t GetHex(void) {
  465.     #define MAXIN 4096 /* MAXIN is the maximum number of input characters */
  466.       static char input[MAXIN]; /* declared as static so it's not on the stack */
  467.       char last;
  468.       uint32_t result;
  469.       fgets(input,MAXIN,stdin); /* fgets as scanf() can't handle blank lines */
  470.       /* First of all check if it was a blank line, i.e. just a '\n' input...*/
  471.       if ('\n' == input[0]) return(INERR);  /* ...which is invalid input too */
  472.       /* Now sscanf() can be used to parse a hex # but we still need to detect
  473.          extraneous input _after_ a valid hex #. has been parsed, hence the %c
  474.          that should have '\n' assigned to it. So, if a valid hex # is parsed
  475.          _and_ there was at least one more character - which there should be -
  476.          then sscanf() will return 2 - the number of successfully parsed items */
  477.       if ((sscanf(input,"%8x%c",&result,&last) < 2)) return(INERR);
  478.       /* Now check to make sure the last character input was a '\n', if not... */
  479.       if ('\n' != last) return(INERR);      /* ...then return an input error value */
  480.       /* If we get to this point it has to be a valid hex # so just return it. */
  481.       return(result);
  482.     }
  483.    
  484.    
  485.     /*declaring OpDigits */
  486.     static int OpDigits;
  487.    
  488.     /* Here I will declare array for J type instructions*/
  489.      
  490.      
  491.      
  492.      
  493.     int main(int argc, char *argv[]) {
  494.    
  495.       uint32_t regashextouint32, regasuint32lslby2, regasdectouint32, programcounter, input;
  496.       /* Declare & (partially) initialise vars for each type of instruction/data */
  497.       mips_rtype_t rtypeinstruction = {0,
  498.                                        0,NULL,"Opcode",
  499.                                        0,NULL,"RS",
  500.                                        0,NULL,"RT",
  501.                                        0,NULL,"RD",
  502.                                        0,NULL,"Shamt",
  503.                                        0,NULL,"Funct"
  504.                                        };
  505.       mips_itype_t itypeinstruction = {0,
  506.                                        0,NULL,"Opcode",
  507.                                        0,NULL,"RS",
  508.                                        0,NULL,"RT",
  509.                                        0,NULL,"Immediate"
  510.                                        };
  511.       mips_jtype_t jtypeinstruction = {0,
  512.                                        0,NULL,"Opcode",
  513.                                        0,NULL,"Address"
  514.                                        };
  515.       ieee_754_t ftypenumber = {{0},
  516.                                 0,NULL,"S",
  517.                                 0,NULL,"E",
  518.                                 0,NULL,"F"
  519.                                 };
  520.       printf(VERSION);
  521.      
  522.        /* Now perform some basic sanity checks on the command line input string... */
  523.             if ( argc != 2 || /* if there's an input string carry on & check its length */
  524.             strlen(argv[1]) != 9 || /* if OK then convert it and check the result: */
  525.             (UINT32_MAX==(regashextouint32=strtouint32(&argv[1][1],16))) ){ /* OK? */
  526.             printf(USAGE);  /* Failed at least one of the above checks so alert user  */
  527.             exit(1);        /* ...and exit. */
  528.             }
  529.             /* All of the above tests passed so continue on with the rest of the code. */
  530.      
  531.            
  532.             regasdectouint32 = strtouint32(&argv[1][0],10); /* Treat reg no as base 10 */
  533.            
  534.             regasuint32lslby2 = regasdectouint32 << 2; /* Calculate the LSL by 2 value */
  535.            
  536.             programcounter = regasuint32lslby2;
  537.            
  538.        
  539.      
  540.             printf("\t%s treated as a 32-bit number and printed as 0x%%08X notation is: 0x%08X\n", argv[1], regasdectouint32 );
  541.                    
  542.             do {
  543.                     printf("Type in a hex number please: ");
  544.                     input = GetHex(); /* input is a uint32_t, this is your mips instruction code */
  545.                     printf("That hex number is %d (base 10)\n",input);
  546.                     OpDigits = IN(input, OP_L,OP_SZ);
  547.                    
  548.                    
  549.                     printf("\tInitial program counter is %08X << 2 => %08X (hex)\n", regasdectouint32, programcounter);
  550.                          
  551.                    
  552.                     /*if statement to check if i, r or j type */
  553.                     if (OpDigits ==0) {   /* checks if digit is equal to opcode 000000*/
  554.                             printf("This is of type r\n");  /*Returns that digit is of type r*/
  555.                     }
  556.                     else if (OpDigits == (2|3)){   /*checks to see if digit is equal to opcode 2 or 3 as that is the opcode numbers for the J types*/
  557.                             printd("This is a J type"); /*returns that digit is of type J*/
  558.                     }
  559.                     else {
  560.                             printf("This is an i type instruction"); /*else will return that digit is of type I*/
  561.      
  562.                     }
  563.                    
  564.                    
  565.             } while (INERR != input);
  566.      
  567.             puts("Quitting");
  568.             return(0);
  569.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement