home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Simulation / PDP-8 Simulator / Source Code / Assembler / Assemble.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  2.3 KB  |  113 lines  |  [TEXT/KAHL]

  1. /************************************************************
  2. *
  3. *
  4. *    Unit containing main assembling code.
  5. *
  6. *    by Adrian Bool in cooperation with Graham Cox.
  7. *
  8. *    copyright © phantasm coding 1992.
  9. *
  10. *
  11. ************************************************************/
  12.  
  13. #include "Global.h"
  14. #include "Assemble.h"
  15.  
  16. #include "Assemble.proto.h"
  17.  
  18. pHandle newProgram(void)
  19.     {
  20.     pHandle        temp;
  21.     
  22.     temp = (pHandle) NewHandle(sizeof(program)); 
  23.     
  24.     if (temp != nil) 
  25.         {
  26.         (*temp)->sourceCode = newSource();
  27.         (*temp)->objectCode = newObject();
  28.         (*temp)->labelHeader = newLabelList();
  29.         }
  30.     return(temp);
  31.     }
  32.     
  33.     
  34. void disposeProgram(pHandle theProgram)
  35.     {
  36.     disposeSource((*theProgram)->sourceCode);
  37.     disposeObject((*theProgram)->objectCode);
  38.     disposeLabelList((*theProgram)->labelHeader);
  39.     
  40.     DisposHandle((Handle) theProgram);
  41.     }
  42.     
  43.         
  44. void pass(pHandle theProgram , short pass)
  45.     {
  46.     str255 aSection;
  47.     int sectionOk;
  48.     opcode theOpcode;
  49.     
  50.     rBlock resultBlock;
  51.     short resultSize;
  52.     
  53.     sHandle sourceCode;
  54.     oHandle objectCode;
  55.     lHandle labelHeader;
  56.     
  57.     sourceCode = (*theProgram)->sourceCode;
  58.     objectCode = (*theProgram)->objectCode;
  59.     labelHeader = (*theProgram)->labelHeader;
  60.     
  61.     (*sourceCode)->line = 0;
  62.     
  63.     if (sourceCode != nil && objectCode != nil && labelHeader != nil)
  64.         {
  65.         (*objectCode)->address = (*objectCode)->startAddress;
  66.         (*objectCode)->position = 0;
  67.         (*sourceCode)->caret = 0;
  68.         (*sourceCode)->line = 1;
  69.         do
  70.             {
  71.             theOpcode = 0;
  72.             getSection(theProgram,aSection);
  73.             if (!AsmError) 
  74.                 {
  75.                 if (aSection[0] == ';') 
  76.                     {    
  77.                     /* a comment has been encountered */
  78.                     
  79.                     nextLine(sourceCode);
  80.                     }  
  81.                 else
  82.                     {    
  83.                     if (aSection[lengthOfString(aSection)-1] == ':')
  84.                         {    
  85.                         /* a defining label has been found */
  86.                         
  87.                         if (pass == 1) newLabel(theProgram,aSection,(*objectCode)->address);                    
  88.                         }
  89.                     
  90.                     else
  91.                         {    
  92.                         if (theOpcode = getOpcode(aSection))
  93.                             {    
  94.                             /* an opcode has been found */
  95.                             
  96.                             assembleOpcode(theOpcode,pass,resultBlock,&resultSize,theProgram);
  97.                             if (pass == 2) addBlock(objectCode,resultBlock,resultSize);
  98.                             (*objectCode)->position += resultSize;
  99.                             (*objectCode)->address += resultSize;
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.         while (theOpcode != end && AsmError==noErr);
  106.         (*(*theProgram)->objectCode)->size = (*(*theProgram)->objectCode)->position;
  107.         }
  108.     else
  109.         AsmError = NotEnoughMemory;
  110.     }
  111.     
  112.  
  113.