Tutorials - Generating code
Tutorials - generating code
- 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
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()
- 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.