Tutorials - Generating code

Tutorials - generating code

To finish the assembler we need to add code generation. This includes various things:

  • raw code - as stored in the memory of the SIC/XE computer,
  • object code - as stored in the object files,
  • other reports such as log and listing files.

Generating raw code

We will again use the visitor.

a) In the Code class we define a method

  • byte[] emitCode()

which returns the raw code of the whole program as an array of bytes. It should be possible to copy such code into the memory of SIC/XE machine and execute it.

b) In the class Node we implement node visitation based on the method:

  • void emitCode(byte[] data, int pos)

which generates a code for the corresponding instruction into the data array at the position pos.

c) The method emitCode() is of course virtual and it has to be override in the subclasses. Hints:

  • we have to generate length() bytes,
  • (ordinary) directives generate nothing,
  • each format must be dealt with separately,
  • memory directives generate data or fill in zeros.

Generating object code

a) In the class Code write a method:

  • String emitText()

which returns the contents of the object file.

Expand the main program to write this contents into the corresponding file. Do not forget H and E records. By the standard T records should not be too long (one line should be fully visible on the screen).

b) In the class Node add the method

  • void emitText(StringBuffer buf)

which appends buf with hexadecimal representation of the raw code.

Hint: for almost all of the instructions you don't need to write this methiod, but instead you can convert raw code to hex.

c) However, for memory directives you need to override emitText(...).

  • Directives BYTE and WORD generate hexadecimal representation of the data,
  • but directives RESB and RESW generate nothing.

Generating other files

To debug methods for generation raw and object code is pretty hard. It is very useful if you add reports such as list of symbols, listing with object code etc. For examples see SicTools assembler.

a) Write a method:

  • String dumpSymbols()

which returns a list (as a string which can be written to a file) of all defined symbols.

b) Write a method:

  • String dumpCode()

which returns generated code including addresses and corresponding instructions. See listing files of SicTools assembler.

Hints:

  • first you write the address of the instructions
  • then the object code
  • followed by the pretty print of the instruction

c) Expand the main program to generate the corresponding files.

Last modified: Sunday, 9 December 2018, 3:06 AM