Advertisement
gur111

Itamar Pvt - getOpCode

Aug 5th, 2020 (edited)
1,611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1.  
  2. #include "data.h"
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. typedef struct {
  8.     const char *name;
  9.     const int opcode;
  10.     const int func;
  11. } OpInfo;
  12.  
  13. const OpInfo OPERATIONS[] = {
  14.     {"mov", 0, 0},  {"cmp", 1, 0},  {"add", 2, 1},
  15.     {"sub", 2, 2},  {"lea", 4, 0},  {"clr", 5, 1},
  16.     {"not", 5, 2},  {"inc", 5, 3},  {"dec", 5, 4},
  17.     {"jmp", 9, 1},  {"bne", 9, 2},  {"jsr", 9, 3} {"red", 12, 0},
  18.     {"prn", 13, 0}, {"rst", 14, 0}, {"stop", 15, 0}};
  19.  
  20. const char *REGISTER_NAMES[] = {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7"};
  21.  
  22. int getOpCode(const char *name) {
  23.     int i;
  24.     for (i = 0; i < sizeof(OPERATIONS) / sizeof(OpInfo); i++) {
  25.         if (strcmp(name, OPEARTIONS[i].name) == 0) {
  26.             return OPERATIONS[i].opcode;
  27.         }
  28.     }
  29.     fprintf(stderr, "Error in line %d. Opcode not detected.\n", 0/* TODO:
  30.     Replace 0 with the line number (maybe use external variable) */);
  31.     return -1;
  32. }
  33.  
  34. int getFunc(const char *name) {
  35.     int i;
  36.  
  37.     for (i = 0; i < sizeof(OPERATIONS) / sizeof(OpInfo); i++) {
  38.         if (strcmp(name, OPEARTIONS[i].name) == 0) {
  39.             return OPERATIONS[i].func;
  40.         }
  41.     }
  42.     fprintf(stderr, "Error in line %d. Funcion not detected.\n", 0/* TODO:
  43.     Replace 0 with the line number (maybe use external variable) */);
  44.     return -1;
  45. }
  46.  
  47. void fillZeros(char *array, int size) {
  48.     int i;
  49.  
  50.     for (i = 0; i < size; i++) {
  51.         array[i] = '0';
  52.     }
  53. }
  54.  
  55. flag isLabel(char *str) {
  56.     int len = strlen(str);
  57.     // Make sure the label has : at the end
  58.     if (str[len - 1] != ':') {
  59.         return FALSE;
  60.     }
  61.  
  62.     // Make sure that the length is at most 31 (excluding :)
  63.     if (len > 32) {
  64.         return FALSE;
  65.     }
  66.  
  67.     // Make sure, the label starts with a letter
  68.     if ((str[0] < 'a' || str[0] > 'z') && (str[0] < 'A' || str[0] > 'Z')) {
  69.         return FALSE;
  70.     }
  71.  
  72.     // Check the rest of the characters (excluding : at the end; len - 1)
  73.     for (i = 1; i < len - 1; i++) {
  74.         // If the character is neither a letter nor a digit, return false
  75.         if ((str[i] < 'a' || str[i] > 'z') && (str[i] < 'A' || str[i] > 'Z') &&
  76.             (str[i] < '0' || str[i] > '9')) {
  77.             return FALSE;
  78.         }
  79.     }
  80.  
  81.     // Make sure we're not a saved instruction
  82.     for (i = 0; i < sizeof(OPERATIONS) / sizeof(OpInfo); i++) {
  83.         // Compare only until the character before the ":"
  84.         if (strncmp(str, OPEARTIONS[i].name, len - 1) == 0) {
  85.             return FALSE;
  86.         }
  87.     }
  88.  
  89.     // Make sure we're not a register
  90.     for (i = 0; i < sizeof(REGISTER_NAMES) / sizeof(char *); i++) {
  91.         if (strncmp(str, REGISTER_NAMES[i], len - 1) == 0) {
  92.             return FALSE;
  93.         }
  94.     }
  95.  
  96.     return TRUE;
  97. }
  98.  
  99. int main() {
  100.     char str[] = "abcdef";
  101.     printf("%s", str);
  102. }
  103.  
  104. // a = "hello world"
  105. // b = "hello israel"
  106. // c = "hello world"
  107. // strncmp(a, c, 500) // == 0
  108. // strncmp(a, b, 5) // == 0
  109. // strcmp(a, b) // != 0
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement