diff --git a/code.eda b/code.eda index 32ed74a..7881c57 100755 --- a/code.eda +++ b/code.eda @@ -1,30 +1,39 @@ -AND GR1, 0 +#AND GR1, 0 +# +#LOAD GR0, *0xFE +#AND GR0, 0xF +#STORE GR0, *0xFF +#ADD GR1, *0xFF +# +#LOAD GR0, *0xFE +#LSR GR0, *4 +#AND GR0, 0xF +#STORE GR0, *0xFF +#ADD GR1, *0xFF +# +#LOAD GR0, *0xFE +#LSR GR0, *8 +#AND GR0, 0xF +#STORE GR0, *0xFF +#ADD GR1, *0xFF +# +#LOAD GR0, *0xFE +#LSR GR0, *12 +#AND GR0, 0xF +#STORE GR0, *0xFF +#ADD GR1, *0xFF +# +#STORE GR1, *0xFF +# +LOAD GR0, *0x00 +LOAD GR1, *0x00 -LOAD GR0, *0xFE -AND GR0, 0xF -STORE GR0, *0xFF -ADD GR1, *0xFF +CMP GR0, GR1 +BEQ @TEST -LOAD GR0, *0xFE -LSR GR0, *4 -AND GR0, 0xF -STORE GR0, *0xFF -ADD GR1, *0xFF +LOAD GR0, *0xFF -LOAD GR0, *0xFE -LSR GR0, *8 -AND GR0, 0xF -STORE GR0, *0xFF -ADD GR1, *0xFF - -LOAD GR0, *0xFE -LSR GR0, *12 -AND GR0, 0xF -STORE GR0, *0xFF -ADD GR1, *0xFF - -STORE GR1, *0xFF - -HALT GR0, *0 +TEST: +HALT diff --git a/compiler.kt b/compiler.kt index 9c22795..3602a03 100644 --- a/compiler.kt +++ b/compiler.kt @@ -18,38 +18,44 @@ enum class Mode(val id: Int) { DIRECT(0), IMMEDIATE(1), INDIRECT(2), INDEXED(3) } -interface Instruction { - fun getData(insns: Iterable): Short +abstract class Instruction(val words: Int) { + fun getData(insns: Iterable): ShortArray } -class Label(val name: String): Instruction { - override fun getData(insns: Iterable) = 0.toShort() +class Label(val name: String): Instruction(0) { + override fun getData(insns: Iterable) = ShortArray(0) } -class Immediate(val data: Short): Instruction { - override fun getData(insns: Iterable) = data +class Immediate(val data: Short): Instruction(1) { + override fun getData(insns: Iterable) = ShortArray(1){ data } } -class Operation(val code: OpCode, val reg: Int, val m: Mode, val adr: AddressReference): Instruction { +class Operation(val code: OpCode, val reg: Int, val m: Mode, val adr: AddressReference, val immediate: Immediate? = null): Instruction(if(m == Mode.IMMEDIATE) 2 else 1) { constructor(val code: OpCode): this(code, 0, Mode.DIRECT, AddressReference(0)) + + init { + if(m == Mode.IMMEDIATE && immediate == null) + throw IllegalArgumentException("No immediate argument passed!") + } override fun getData(insns: Iterable): Short { - return code.opcode - .and(0b1111) - .shl(12) - .or( - if(code.useReg) reg.and(0b11).shl(10) - else 0 - ) - .or( - if(code.useM) m.id.and(0b11).shl(8) - else 0 - ) - .or( - if(code.useADR) adr.getAddress(insns).and(0b11111111) - else 0 - ) - .toShort() + return shortArrayOf(code.opcode + .and(0b1111) + .shl(12) + .or( + if(code.useReg) reg.and(0b11).shl(10) + else 0 + ) + .or( + if(code.useM) m.id.and(0b11).shl(8) + else 0 + ) + .or( + if(code.useADR) adr.getAddress(insns).and(0b11111111) + else 0 + ) + .toShort() + ) } } @@ -89,4 +95,6 @@ fun main(args: Array){ } - +fun Array.compile(): Array { + +} diff --git a/compiler.py b/compiler.py index 4d5646d..a96ef40 100755 --- a/compiler.py +++ b/compiler.py @@ -3,14 +3,16 @@ # Compiles ED-Assembly to Machinecode # -# LOAD GRx, M, ADR -# STORE GRx, M, ADR -# ADD GRx, M, ADR -# SUB GRx, M, ADR -# AND GRx, M, ADR -# LSR GRx, M, Y # STEG +# LOAD GRx, ADR +# STORE GRx, ADR +# ADD GRx, ADR +# SUB GRx, ADR +# AND GRx, ADR +# LSR GRx, Y # STEG # BRA ADR # BNE ADR +# BEQ ADR +# CMP GRx, GRy # # One machine instruction @@ -127,6 +129,7 @@ OPCODE_TABLE = { "BNE" : 0b0111, "CMP" : 0b1000, "BEQ" : 0b1001, + "BLT" : 0b1010, "HALT" : 0b1111, } @@ -231,6 +234,19 @@ def parse_instruction(args): """ Parse an assembly instruction into an instruction object. """ + if args[0] in ["HALT"]: + return Instruction(OPCODE_TABLE[args[0]], 0, 0, 0) + if args[0] in ["LOAD", "STORE", "ADD", "SUB", "AND", "LSR"]: + return parse_tripple_instuction(args) + if args[0] in ["BRA", "BNE", "BEQ", "BLT"]: + return parse_jump(args) + if args[0] in ["CMP"]: + return parse_dual_registers(args) + else: + print("Foregotten instruction: ", args[0]) + assert False + +def parse_tripple_instuction(args): if len(args) < 3: raise SyntaxError("Not enough arguments.") if len(args) > 3: @@ -242,6 +258,24 @@ def parse_instruction(args): address, mode = parse_operand(args[2]) return Instruction(opcode, register, mode, address) +def parse_jump(args): + if len(args) < 2: + raise SyntaxError("Not enough arguments.") + if len(args) > 2: + raise SyntaxError("Trash at end of line \"{}\"".format(" ".join(args[3:]))) + opcode = OPCODE_TABLE[args[0]] + address, mode = parse_operand(args[1]) + return Instruction(opcode, 0, ADDRESS_MODES["DIRECT"], address) + +def parse_dual_registers(args): + if len(args) < 3: + raise SyntaxError("Not enough arguments.") + if len(args) > 3: + raise SyntaxError("Trash at end of line \"{}\"".format(" ".join(args[3:]))) + opcode = OPCODE_TABLE[args[0]] + register_a = REGISTERS[args[1].upper()] + register_b = REGISTERS[args[2].upper()] + return Instruction(opcode, register_a, register_b, 0, 1) def main(): success = True diff --git a/qs.c b/qs.c new file mode 100644 index 0000000..0c3e650 --- /dev/null +++ b/qs.c @@ -0,0 +1,34 @@ +#include + +void quicksort(int *data, int low, int high){ + while(low < high){ + int pivot = data[high]; + int i = low; + + for(int j = low; j < high; ++j){ + if(data[j] < pivot){ + int tmp = data[j]; + data[j] = data[i]; + data[i] = tmp; + i += 1; + } + } + + int tmp = data[high]; + data[high] = data[i]; + data[i] = tmp; + + quicksort(data, low, i - 1); + low = i + 1; + } +} + +int main(int argc, char ** argv){ + int data[26] = {1, 2, 0, 3, 5, 9, 8, 7, 6, 11, 10, 4, 5, 6, 7, 9, 4, 6, 8, 7, 2, 4, 1, 12, 40, 25}; + for(int i = 0; i < 26; ++i) + printf("#: %d\n", data[i]); + + quicksort(data, 0, 25); + for(int i = 0; i < 26; ++i) + printf("%: %d\n", data[i]); +}