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 / Ass.c next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  3.6 KB  |  181 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 "Ass.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. void assemble(pHandle theProgram)
  44.     {
  45.     passOne(theProgram);
  46.     createObjectSpace((*theProgram)->objectCode);
  47.     passTwo(theProgram);
  48.  
  49.     }
  50.     
  51. void passOne(pHandle theProgram)
  52.     {
  53.     str255 aSection;
  54.     pdpTableRef theOpcode;
  55.     int sectionOk;
  56.     
  57.     rBlock resultBlock;
  58.     short resultSize;
  59.     
  60.     sHandle sourceCode;
  61.     oHandle objectCode;
  62.     lHandle labelHeader;
  63.     
  64.     sourceCode = (*theProgram)->sourceCode;
  65.     objectCode = (*theProgram)->objectCode;
  66.     labelHeader = (*theProgram)->labelHeader;
  67.     
  68.     (*sourceCode)->line = 0;
  69.     
  70.     if (sourceCode != nil && objectCode != nil && labelHeader != nil)
  71.         {
  72.         
  73.         (*objectCode)->address = 0;
  74.         (*objectCode)->position = 0;
  75.         (*sourceCode)->line = 1;
  76.         do
  77.             {
  78.             getSection(theProgram,aSection);
  79.             if (!AsmError) 
  80.                 {
  81.                 if (aSection[0] == ';') 
  82.                     {    
  83.                     /* a comment has been encountered */
  84.                     
  85.                     nextLine(sourceCode);
  86.                     }  
  87.                 else
  88.                     {    
  89.                     if (aSection[lengthOfString(aSection)-1] == ':')
  90.                         {    
  91.                         /* a defining label has been found */
  92.                         
  93.                         newLabel(theProgram,aSection,(*objectCode)->address);                    
  94.                         }
  95.                     
  96.                     else
  97.                         {    
  98.                         if (theOpcode = getOpcode(aSection))
  99.                             {    
  100.                             /* an opcode has been found */
  101.                             
  102.                             assembleOpcode(theOpcode,1,resultBlock,&resultSize,theProgram);
  103.                             (*objectCode)->position += resultSize;
  104.                             (*objectCode)->address += resultSize;
  105.                             }
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         while (theOpcode != end && AsmError==noErr);
  111.         (*(*theProgram)->objectCode)->size = (*(*theProgram)->objectCode)->position;
  112.         }
  113.     else
  114.         AsmError = NotEnoughMemory;
  115.     }
  116.     
  117.  
  118. void passTwo(pHandle theProgram)
  119.     {
  120.     str255 aSection;
  121.     pdpTableRef theOpcode;
  122.     int sectionOk;
  123.     
  124.     rBlock resultBlock;
  125.     short resultSize;
  126.     
  127.     sHandle sourceCode;
  128.     oHandle objectCode;
  129.     lHandle labelHeader;
  130.     
  131.     sourceCode = (*theProgram)->sourceCode;
  132.     objectCode = (*theProgram)->objectCode;
  133.     labelHeader = (*theProgram)->labelHeader;
  134.     
  135.     (*sourceCode)->line = 0;
  136.     
  137.     if (sourceCode != nil && objectCode != nil && labelHeader != nil)
  138.         {
  139.         (*objectCode)->position = 0;
  140.         (*objectCode)->address = (*objectCode)->startAddress;
  141.         (*sourceCode)->caret = 0;
  142.         (*sourceCode)->line = 1;
  143.         
  144.         do
  145.             {
  146.             theOpcode = 0;
  147.             getSection(theProgram,aSection);
  148.             if (!AsmError) 
  149.                 {
  150.                 if (aSection[0] == ';') 
  151.                     {    
  152.                     /* a comment has been encountered */
  153.                     
  154.                     nextLine(sourceCode);
  155.                     }  
  156.                 else
  157.                     {    
  158.                     if (aSection[lengthOfString(aSection)-1] != ':')
  159.                         {    
  160.                         if (theOpcode = getOpcode(aSection))
  161.                             {    
  162.                             /* an opcode has been found */
  163.                             
  164.                             assembleOpcode(theOpcode,2,resultBlock,&resultSize,theProgram);
  165.                             
  166.                             addBlock(objectCode,resultBlock,resultSize);
  167.                             (*objectCode)->address += resultSize;
  168.                             (*objectCode)->position += resultSize;
  169.                             }
  170.                         }
  171.                     }
  172.                 }
  173.             }
  174.         while (theOpcode != end && AsmError == noErr);
  175.         }
  176.     else
  177.         AsmError = NotEnoughMemory;
  178.     }
  179.  
  180.  
  181.