Compiler Subleq Backend
In order to convert Simple Compiler to a Subleq compiler, I have to replace the Simple Compiler Assembler (OpCode) Lister and Assemble (OpCode) Interpreter with a Subleq OpCode exporter. I will use an external Subleq assembler and interpreter. The Subleq OpCode exporter is called the Subleq backend. The Subleq backend contains all the Subleq macros, but only composite macros (which takes an operand) are accessible. Here is the list of Subleq macros:
- jump
- clear
- not
- shl
- inc
- dec
- chs
- label
- return
- putc
- getc
- wrtInt
- rdInt
- sub
- add
- copy
- ncopy (negative copy)
- jlez
- jgez
- jeqz
- jgtz
- jltz
- jnez
- jmin
- store
- absolute
- xor
- and
- or
- nand
- nor
- new sub
- new add
- system
- movAxVar
- movVarAx
- movAxImm
- movAxBx
- movBxAx
- pushAx
- pushBx
- pushFx
- popAx
- popBx
- popFx
- addBx
- subBx
- mulBx
- divBx
- jmp
- jz
- wrtAx
- wrtLn
- rdAx
- halt
- cmpAx
- orAxAx
- xorAxAx
- setEq
- setNE
- setLT
- setLE
- setGT
- setGE
- HALT (0)
- OUTPUT (-1)
- INPUT (-2)
- Z (zero)
- T (temp)
- t (tmp)
- P (+1)
- N (-1)
- the CPU model:
- Ax
- Bx
- Fx
- SP
- DP
- and few useful constants:
- _SP (-32)
- _DP (-16384)
- _MIN (-32768)
- _CR (13)
- _LF (10)
- _SPACE (32)
- _MINUS (45)
- _ZERO (48)
- _NINE (57)
The Subleq system also has a point to pointer copy (PPC) routine (as it is assumed the monitor code will be stored in ROM and pointer to pointer copy requires self modifying code).
I will likely add many more constants to support string output later.
I have added the binary operation macros:
- XOR Ax,Bx
- AND Ax,Bx
- OR Ax,Bx
- NAND Ax,Bx
- NOR Ax,Bx
- NOT Ax
- SHL Ax
- SHR Ax (to be added)
These bit operations don't update the flag register.
Integer Minimum Value
The minimum integer value (MIN) for a 16 bit integer is -32768. Subleq has a major problem with MIN. It treats MIN as equal to 0 unless special precautions are taken. This is not usually a problem unless the code uses the sign bit (such as for bit operations etc.). In the end I rewrote all the test macros (i.e. jeqz, jnez, jlez, jltz, jgez and jgtz) to be MIN aware.
I also used MIN as the return value for multiplication overflow and attempt to divide by zero. There is a "jmin" macro if required.
Working Subleq Backend
I have got a working Subleq backend test-bed (TestMacro2Subleq), for example the following Subleq macros:
compositeMacro("system",0); compositeMacro("rdAx",0); compositeMacro("pushAx",0); compositeMacro("pushAx",0); compositeMacro("rdAx",0); compositeMacro("movBxAx",0); compositeMacro("popAx",0); compositeMacro("pushBx",0); // iMul compositeMacro("mulBx",0); compositeMacro("wrtInt",0); compositeMacro("wrtLn",0); // iDiv compositeMacro("popBx",0); compositeMacro("popAx",0); compositeMacro("divBx",0); compositeMacro("wrtInt",0); compositeMacro("wrtLn",0); compositeMacro("movAxBx",0); compositeMacro("wrtInt",0); compositeMacro("wrtLn",0);
Produces after assembly and interpretation:
C:\AlanX\TestSubleq>TestMacro2Subleq 1>Test.sasm C:\AlanX\TestSubleq>subleq_asm -i Test.sasm -o Test.code -l Test.list C:\AlanX\TestSubleq>Subleq_Int -i Test.code -l Test.list -t Test.trace SUBLEQ Interpreter 6 <- Entered integer 2 <- Entered integer 12 -> Integer multiplication (=6*2) 3 -> Integer division (=6/2) 0 -> Integer remainder (=6%2) Interpreter finished C:\AlanX\TestSubleq>pause Press any key to continue . . .
The Subleq code produced is too long to list here.
AlanX
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.