kernel_memory_dump

C++ BONA TO STO VALJA

Mar 12th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #include "passI.h"
  2.  
  3. #include "assemblerLib.h"
  4.  
  5. using namespace std;
  6.  
  7.  
  8. bool passI()
  9. {
  10.     DIRECTIVE_T directive;
  11.     PARAM_STACK params;
  12.     SECTION_T section = S_TEXT;
  13.     SOURCE_LIST* sourceList;
  14.     SOURCE_LIST::iterator sourceListIter;
  15.     long memLocCounter;
  16.     string sourceLine;
  17.     long lineNum;
  18.     string symbol;
  19.     string msg;
  20.     string executable;
  21.     /******************************************************************************************
  22.     * Add your variables here
  23.     *******************************************************************************************/
  24.     // DODATO
  25.     int brParam = 0;
  26.     sourceList = getSourceList();   // get source code loaded in STL list, where each element is SOURCE_LINE,
  27.                                     // structure consisted of line number and source line string
  28.  
  29.     memLocCounter = 0; // memory locations counter start at 0
  30.  
  31.     for (sourceListIter = sourceList->begin(); sourceListIter != sourceList->end(); sourceListIter++)
  32.     {
  33.         // for each line from source code
  34.         sourceLine = sourceListIter->sourceLine;    // get source line
  35.         lineNum = sourceListIter->lineNumber;       // get line number
  36.  
  37.         getParams(params, sourceLine, lineNum); // get params from current source line e.g.:
  38.                                                 // from line "add $t3, $t2, $0", extracted parametrs should be "$t3", "$t2", and "$0"
  39.                                                 // from line "value: .word 9", extracted parametrs should be "9"
  40.  
  41.         directive = getDirective(sourceLine, lineNum); // extract directive from source line (string following ".")
  42.  
  43.         switch (directive)
  44.         {
  45.         case D_NONE:
  46.             // DODAJEMO 3 IF
  47.                     // directive not found in current line
  48.             if (getSymbol(symbol, sourceLine)){
  49.                 if (symbolExists(symbol)){
  50.                     msg = "Symbol already exist: " + symbol;
  51.                     addError(lineNum, sourceLine, msg);
  52.                 } else {
  53.                     pushSymbol(symbol, memLocCounter,section);
  54.                 }
  55.             }
  56.  
  57.                
  58.  
  59.             if (getExecutable(executable, sourceLine, lineNum,false))
  60.                 memLocCounter+=4;
  61.             break;
  62.  
  63.         case D_ORG:
  64.             // if the directive is .org
  65.             if (checkEnoughParams(lineNum, sourceLine, params, 1)) // check if the line with directive is correct
  66.             {
  67.                 changeSectionAndLocation(params.top(), section, memLocCounter); // current section is updated due to .org directive (data/text section)
  68.             }
  69.             break;
  70.  
  71.         case D_WORD:
  72.             // .word directive => extract the value as first parameter, convert it to number and add to literal table
  73.             pushLiteral(memLocCounter, constConv(params.top()));
  74.             // Extra question: what if .word directive can have more than one parameter?
  75.             // dodatni shiet
  76.             // SHIIIIETTTT
  77.             if (getSymbol(symbol, sourceLine)){
  78.                 if (symbolExists(symbol)){
  79.                     msg = "Symbol already exist: " + symbol;
  80.                     addError(lineNum, sourceLine, msg);
  81.                 } else {
  82.                     pushSymbol(symbol, memLocCounter,section);
  83.                 }
  84.             }
  85.            
  86.             brParam = 0;
  87.            
  88.             while(!params.empty()){
  89.                 brParam++;
  90.                 params.pop();
  91.             }
  92.             memLocCounter = memLocCounter +  brParam * 4;
  93.             break;
  94.  
  95.         case D_SPACE:
  96.             // .space directive => just check if it is followed by a number (single parameter)
  97.             checkEnoughParams(lineNum, sourceLine, params, 1);
  98.             // JESI LUD
  99.             // EV BONA
  100.             memLocCounter += constConv( params.top()); // .space 7
  101.             if (getSymbol(symbol, sourceLine)){
  102.                 if (symbolExists(symbol)){
  103.                     msg = "Symbol already exist: " + symbol;
  104.                     addError(lineNum, sourceLine, msg);
  105.                 } else {
  106.                     pushSymbol(symbol, memLocCounter,section);
  107.                 }
  108.             }
  109.             break;
  110.  
  111.         case D_GLOBL:
  112.             // .globl directive => get the symbol as first parameter, check if exists in global symbols table and add it to the table
  113.             if (checkEnoughParams(lineNum, sourceLine, params, 1))
  114.             {
  115.                 if (globalSymbolExists(params.top()))
  116.                 {
  117.                     msg = "Global symbol already exist: " + params.top();
  118.                     addError(lineNum, sourceLine, msg);
  119.                 }
  120.                 else
  121.                 {
  122.                     pushGlobalSymbol(params.top(), memLocCounter);
  123.                 }
  124.             }
  125.             break;
  126.  
  127.         case D_EXTERN:
  128.             // .extern directive => get the symbol as first parameter, check if exists in extern symbols table and add it to the table
  129.             if (checkEnoughParams(lineNum, sourceLine, params, 1))
  130.             {
  131.                 if (externSymbolExists(params.top()))
  132.                 {
  133.                     msg = "Extern symbol already exist: " + params.top();
  134.                     addError(lineNum, sourceLine, msg);
  135.                 }
  136.                 else
  137.                 {
  138.                     pushExternSymbol(params.top(), memLocCounter);
  139.                 }
  140.             }
  141.             break;
  142.  
  143.         default:
  144.             // error!
  145.             break;
  146.         }
  147.     }
  148.  
  149.     return !errorsFound();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment