simulator - Y86 Processor in C help! Stuck on the loader.c file -


this gonna bit of large question (at least me seems) bear me. we're making y86 simulator in c , part of assignment supposed open file, load memory, perform dump, , exit. main method provided these things, we're supposed create loader.c , loader.h files loading. load function returns true(1) if load successful (input file free of errors) , false(0) otherwise. right now, i'm stuck on load function, , think issue on around line 58-67 of loader.c. i'm not calling method properly, cannot figure out how format numbers properly. first time i'm working in c, it's still rather confusing me. if needed can provide more code. far i've included loader.c, loader.h, memory.c, , memory.h. bool.h included, it's typedef true-false figured leave out. apologize if bit much, i'm stumped , don't know else go. thank in advance given, appreciated!

loader.c:

1   #include <stdio.h> 2   #include <string.h> 3   #include "loader.h" 4   #include "bool.h" 5   #include "memory.h" 6   7   bool checkaddressformat (char addressarray[]); 8   bool checkdataformat(char dataarray[]); 9   bool isdigit(char character);  10  bool checkline(char inputline[]); 11  12  int load(int argc, char *argv[]){ 13  14      if (argc != 2){ 15          printf("file opening failed \nusage: yess <filename>.yo\n"); 16          return 0; 17      } 18      else{ 19          //checks files exist 20          file* file = fopen(argv[1], "r" ); 21          char* ext; 22          ext = strrchr(argv[1], '.'); //grab extension 23          if (file == 0 || strcmp(ext,".yo") != 0) { 24              printf("file opening failed \nusage: yess <filename>.yo\n"); 25              return 0; 26          } 27         else{ 28             char inputline[250]; //the current line being used 29             int intline[250]; 30             int ch; 31             int i; 32             int j; 33             int k; 34             int m; 35             bool linecheck; 36             bool memerror = false; 37             int byteaddress; 38             int linenumber = 1; 39              40             ch = fgetc(file); //grab first character 41             while (!feof(file)) //while file not empty 42             { 43                 while(ch != '\n'){ 44                     inputline[i] = ch; 45                     intline[i] = ch; 46                     ch = fgetc(file); 47                 } 48                 char addressstring[4]; 49                 addressstring[3] = '\0'; 50                 for(j = 0; j < 3; j++){ //creates char array of address on current line. 51                      addressstring[j] = inputline[j+4]; 52                 } 53                 byteaddress = atoi(addressstring); //turns array int pass putbyte 54  55                 unsigned char datastring[12]; 56                 for(k = 0; k < 12; k++) { 57                      datastring[k] = (unsigned char)intline[k+9]; 58                 } 59  60                 if(checkline(inputline) == false){ 61                     printf("error on line %d:\n",linenumber); 62                     printf("%s\n",inputline); 63                     return 0; 64                 } 65                  66                 else{ 67                     for(m = 0; m < 250; m++) { 68                         putbyte(byteaddress, datastring[m], &memerror); 69                     } 70                     printf("call putbyte"); 71                 } 72  73                 for(i = 0; < 250; i++){ 74                     inputline[i] = ' '; 75                     intline[i] = ' '; 76                 } 77                 printf("clear arrays"); 78                 linenumber++; 79                 ch = fgetc(file); 80             } 81         } 82         close(file); 83      } 84      return 1; 85  } 86  87  bool checkline(char inputline[]){ 88       89      if(checkdataformat(inputline) == false || checkaddressformat(inputline) == false){ 90          return false; 91      } 92      else if(inputline[22] != '|'){ 93          return false; 94      } 95      else { 96          return true; 97      } 98  99  } 100 bool checkdataformat(char dataarray[]) { 101     int i; 102     bool temp; 103     int counter = 0; 104     for(i = 9; <= 20; i++) { 105         temp = isdigit(dataarray[i]); 106         if(temp  == true){ 107             counter++; 108         } 109     } 110     if(counter > 12){ 111         return false; 112     } 113     else{ 114         return true; 115     } 116 } 117 118 bool checkaddressformat(char addressarray[]) { 119     if(isdigit(addressarray[2]) == false || isdigit(addressarray[4]) == false || isdigit(addressarray[5])  == false || isdigit(addressarray[6]) == false) 120         return false; 121     if(addressarray[3] != 'x') 122         return false; 123     if(addressarray[7] != ':') 124         return false; 125     else 126         return true; 127 } 128 129 bool isdigit(char character) { 130     if(character == '0' || character == '1' || character == '2' || character == '3' || 131        character == '4' || character == '5' || character == '6' || character == '7' || 132        character == '8' || character == '9' || character == 'a' || character == 'b' || 133       character == 'c' || character == 'd' || character == 'e' || character == 'f') 134       return true; 135    else 136        return false; 137} 138 

loader.h:

1    #ifndef loader_h 2    #define loader_h 3 4    int load(int argc, char *argv[]); 5 6    #endif 

memory.c:

1   #define memsize 1024     //1024 words of memory 2   #ifndef memory_h 3   #define memory_h 4   #include "bool.h" 5  6   static unsigned int memarray[memsize]; 7  8  9   unsigned int fetch(int address, bool * memerror){ 10    11      if(address < 0 || address > 1024) 12          (*memerror) = true; 13      else{ 14          (*memerror) = false; 15          return memarray[address]; 16      } 17  } 18 19  void store(int address, unsigned int value, bool * memerror){ 20      if(address < 0 || address > 1024) 21          (*memerror) = true; 22      else{ 23          (*memerror) = false; 24          memarray[address] = value; 25      } 26  } 27 28  unsigned char getbyte(int byteaddress, bool * memerror){ 29      if(byteaddress < 0 || byteaddress > 4095){ 30          (*memerror) = true; 31          return 0; 32      } 33      else{ 34          (*memerror) = false; 35          int wordaddress = fetch((byteaddress/4), memerror); 36          char * x = (char*)&wordaddress; 37          char temp = x[(byteaddress%4)]; 38          return temp; 39      } 40  } 41  void putbyte(int byteaddress, unsigned char value, bool * memerror){ 42      if(byteaddress < 0 || byteaddress > 4095) 43          (*memerror) = true; 44      else{ 45          (*memerror) = false; 46          int wordaddress = fetch((byteaddress/4), memerror); 47          char * x = (char*)&wordaddress; 48          x[(byteaddress%4)] = value; 49          store(byteaddress/4, wordaddress, memerror); 50      } 51  } 52  void clearmemory(){ 53 54      int i; 55      for(i=0;i<memsize;i++){ 56          memarray[i] = 0; 57      } 58 59  } 60  //address must multiple of 4 61  unsigned int getword(int byteaddress, bool * memerror){ 62 63      if(byteaddress < 0 || byteaddress > 4095 || (byteaddress%4) != 0){ 64          (*memerror) = true; 65          return 0; 66      } 67      else{ 68          int word = fetch(byteaddress/4, memerror); 69          (*memerror) = false; 70          return word; 71      } 72  } 73  //address must multiple of 4 74  void putword(int byteaddress, unsigned int value, bool * memerror){ 75 76     if(byteaddress < 0 || byteaddress > 4095 || (byteaddress%4) != 0){ 77          (*memerror) = true; 78      } 79      else{ 80          store((byteaddress/4), value, memerror); 81          (*memerror) = false; 82      } 83    84 85  } 86  #endif 87 88 

memory.h:

1   #define memsize 1024     //1024 words of memory 2   #ifndef memory_h 3   #define memory_h 4  5   unsigned int fetch(int address, bool * memerror); 6   void store(int address, unsigned int value, bool * memerror); 7   unsigned char getbyte(int byteaddress, bool * memerror); 8   void putbyte(int byteaddress, unsigned char value, bool * memerror); 9   void clearmemory(); 10  unsigned int getword(int byteaddress, bool * memerror); 11  void putword(int byteaddress, unsigned int value, bool * memerror); 12  #endif  

you should examine arrays then. far, common error either (a) aren't long enough, or (b) calling function assumes string/array 0 (null) terminated, 0 hasn't been added. string in c null terminated c string functions work.

a couple of example trouble spots...

47                 char addressstring[3]; 48                 for(j = 0; j < 3; j++){ //creates char array of address on current line. 49                      addressstring[j] = inputline[j+4]; 50                 } 51                 byteaddress = atoi(addressstring); //turns array int pass putbyte 

you copying 3 bytes input line addressstring. array has enough space hold characters , has no null termination. subsequent atoi call can go off never-never land (potential segfault).

here, datastring array of characters:

53                 unsigned char datastring[12]; 54                 for(k = 0; k < 12; k++) { 55                      datastring[k] = (unsigned char)intline[k+9]; 56                 } 57  

here, you're treating datastring if single character:

58                 unsigned char data = datastring; 59  60                 if(checkline(inputline) == false){ 61                     printf("error on line %d:\n",linenumber); 62                     printf("%s\n",inputline); 63                     return 0; 64                 } 65                  66                 else{ 67                     putbyte(byteaddress, datastring, &memerror); 68                 } 

this may not cause segfault, incorrect.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -