Advertisement
kernel_memory_dump

cpp

May 4th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 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; // brojac mem  lokacija
  16.     string sourceLine;
  17.     long lineNum;
  18.     string symbol;
  19.     string msg;
  20.     string executable;
  21.     /******************************************************************************************
  22.     * Add your variables here
  23.     *******************************************************************************************/
  24.     int brParam = 0;
  25.  
  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.             // directive not found in current line
  47.             if (getSymbol(symbol, sourceLine)){
  48.                 if (symbolExists(symbol)){
  49.                     msg = "Symbol already exist: " + symbol;
  50.                     addError(lineNum, sourceLine, msg);
  51.                 } else {
  52.                     pushSymbol(symbol, memLocCounter,section);
  53.                 }
  54.             }
  55.  
  56.                
  57.  
  58.             if (getExecutable(executable, sourceLine, lineNum,false))
  59.                 memLocCounter+=4;
  60.             break;
  61.  
  62.         case D_ORG:
  63.             // if the directive is .org
  64.             if (checkEnoughParams(lineNum, sourceLine, params, 1)) // check if the line with directive is correct
  65.             {
  66.                 changeSectionAndLocation(params.top(), section, memLocCounter); // current section is updated due to .org directive (data/text section)
  67.             }
  68.             break;
  69.  
  70.         case D_WORD:
  71.             // .word directive => extract the value as first parameter, convert it to number and add to literal table
  72.             pushLiteral(memLocCounter, constConv(params.top()));
  73.             if (getSymbol(symbol, sourceLine)){
  74.                 if (symbolExists(symbol)){
  75.                     msg = "Symbol already exist: " + symbol;
  76.                     addError(lineNum, sourceLine, msg);
  77.                 } else {
  78.                     pushSymbol(symbol, memLocCounter,section);
  79.                 }
  80.             }
  81.            
  82.             brParam = 0;
  83.            
  84.             while(!params.empty()){
  85.                 brParam++;
  86.                 params.pop();
  87.             }
  88.             memLocCounter = memLocCounter +  brParam * 4;
  89.             // Extra question: what if .word directive can have more than one parameter?
  90.             break;
  91.  
  92.         case D_SPACE:
  93.             // .space directive => just check if it is followed by a number (single parameter)
  94.             checkEnoughParams(lineNum, sourceLine, params, 1);
  95.             memLocCounter += constConv( params.top()); // .space 7
  96.             if (getSymbol(symbol, sourceLine)){
  97.                 if (symbolExists(symbol)){
  98.                     msg = "Symbol already exist: " + symbol;
  99.                     addError(lineNum, sourceLine, msg);
  100.                 } else {
  101.                     pushSymbol(symbol, memLocCounter,section);
  102.                 }
  103.             }
  104.             break;
  105.  
  106.         case D_GLOBL:
  107.             // .globl directive => get the symbol as first parameter, check if exists in global symbols table and add it to the table
  108.             if (checkEnoughParams(lineNum, sourceLine, params, 1))
  109.             {
  110.                 if (globalSymbolExists(params.top()))
  111.                 {
  112.                     msg = "Global symbol already exist: " + params.top();
  113.                     addError(lineNum, sourceLine, msg);
  114.                 }
  115.                 else
  116.                 {
  117.                     pushGlobalSymbol(params.top(), memLocCounter);
  118.                 }
  119.             }
  120.             break;
  121.  
  122.         case D_EXTERN:
  123.             // .extern directive => get the symbol as first parameter, check if exists in extern symbols table and add it to the table
  124.             if (checkEnoughParams(lineNum, sourceLine, params, 1))
  125.             {
  126.                 if (externSymbolExists(params.top()))
  127.                 {
  128.                     msg = "Extern symbol already exist: " + params.top();
  129.                     addError(lineNum, sourceLine, msg);
  130.                 }
  131.                 else
  132.                 {
  133.                     pushExternSymbol(params.top(), memLocCounter);
  134.                 }
  135.             }
  136.             break;
  137.  
  138.         default:
  139.             // error!
  140.             break;
  141.         }
  142.     }
  143.  
  144.     return !errorsFound();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement