Add in CMI instruction.

This commit is contained in:
Edvard Thörnros 2019-04-05 16:51:50 +02:00
parent 52ae1cc1fa
commit b94229c504
2 changed files with 76 additions and 1 deletions

View File

@ -130,6 +130,7 @@ OPCODE_TABLE = {
"CMP" : 0b1000,
"BEQ" : 0b1001,
"BLT" : 0b1010,
"CMI" : 0b1011,
"HALT" : 0b1111,
}
@ -242,10 +243,25 @@ def parse_instruction(args):
return parse_jump(args)
if args[0] in ["CMP"]:
return parse_dual_registers(args)
if args[0] in ["CMI"]:
return parse_compare_imediate(args)
else:
print("Foregotten instruction: ", args[0])
assert False
def parse_compare_imediate(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]]
if args[1].upper() not in REGISTERS:
raise SyntaxError("Invalid register name" + args[1])
register = REGISTERS[args[1].upper()]
address, mode = parse_operand(args[2])
return Instruction(opcode, register, ADDRESS_MODES["DIRECT"], address)
def parse_tripple_instuction(args):
if len(args) < 3:
raise SyntaxError("Not enough arguments.")
@ -273,9 +289,11 @@ def parse_dual_registers(args):
if len(args) > 3:
raise SyntaxError("Trash at end of line \"{}\"".format(" ".join(args[3:])))
opcode = OPCODE_TABLE[args[0]]
if args[1].upper() not in REGISTERS:
raise SyntaxError("Invalid register name" + args[1])
register_a = REGISTERS[args[1].upper()]
register_b = REGISTERS[args[2].upper()]
return Instruction(opcode, register_a, register_b, 0, 1)
return Instruction(opcode, register_a, register_b, 0)
def main():
success = True

57
sorter.eda Normal file
View File

@ -0,0 +1,57 @@
# Beatuifull
LOAD GR0 *0x00E0 # I
LOAD GR1 *0x00E0 # J
OUTER:
CMI GR0 0xFF
BEQ @END
STORE GR0 *@I
LOAD GR2 **@I
STORE GR1 *@I
INNER:
ADD GR1 1
STORE GR1 *@J
LOAD GR3 **@J
CMP GR2 GR3
BLT @INNER
STORE GR2 **@J
STORE GR3 **@I
LOAD GR2 **@I
CMI GR1 0xFF
BNE @INNER
BRA @OUTER
END:
HALT
# for (int i = 0; i < length; i++)
# {
# for (int j = i; j < length;)
# {
# j++
# if (data[i] < data[j])
# {
# int a = data[i];
# data[i] = data[j];
# data[j] = a;
# }
# }
# }
I:
. 0020
J:
. 0000
A: # Pointer to value at I
. 0000
B: # Pointer to value at J
. 0000
LAST:
. 00FF