Assembleur final
This commit is contained in:
parent
a4cc3b36a6
commit
fe9ca66500
22
Epreuve2.asm
Normal file
22
Epreuve2.asm
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
_main:
|
||||||
|
MOV R0 0 ; a
|
||||||
|
MOV R1 1 ; b
|
||||||
|
OUT R0 ; print 0
|
||||||
|
|
||||||
|
_loop:
|
||||||
|
OUT R1 ; print b
|
||||||
|
MOV R2 0 ; 0
|
||||||
|
MOV R3 R0 ; c = a
|
||||||
|
SUB R2 R1 ; 0 - b
|
||||||
|
SUB R3 R2 ; a - (0 - b) = a - -b = a + b
|
||||||
|
|
||||||
|
MOV R0 R1 ; a = b
|
||||||
|
MOV R1 R3 ; b = c
|
||||||
|
|
||||||
|
CMP R1 R0
|
||||||
|
JLT _end ; end si b < a
|
||||||
|
JMP _loop
|
||||||
|
|
||||||
|
_end:
|
||||||
|
RET
|
||||||
@ -155,12 +155,13 @@ instructions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
labels = {}
|
labels = {}
|
||||||
|
lastLabel = ""
|
||||||
|
|
||||||
def valueToInt(arg):
|
def valueToInt(arg):
|
||||||
try:
|
try:
|
||||||
return int(arg)
|
return int(arg)
|
||||||
except:
|
except:
|
||||||
return ord(arg)
|
return ord(arg[1])
|
||||||
|
|
||||||
def registerToDec(reg):
|
def registerToDec(reg):
|
||||||
return int(reg[1])
|
return int(reg[1])
|
||||||
@ -280,19 +281,18 @@ def convertInsCMP(args):
|
|||||||
def convertInsLDR(args):
|
def convertInsLDR(args):
|
||||||
idReg0 = registerToDec(args[0])
|
idReg0 = registerToDec(args[0])
|
||||||
idReg1 = registerToDec(args[1])
|
idReg1 = registerToDec(args[1])
|
||||||
return {"opcode": [0b10110000 | (idReg0 << 2) | (idReg1), valueToInt(args[2]), "label"], "label": args[0], "offset": 0}
|
return {"opcode": [0b10110000 | (idReg0 << 2) | (idReg1), "label"], "label": args[2], "offset": 0, "DB_Update": True}
|
||||||
|
|
||||||
def convertInsSTR(args):
|
def convertInsSTR(args):
|
||||||
idReg0 = registerToDec(args[0])
|
idReg0 = registerToDec(args[0])
|
||||||
idReg1 = registerToDec(args[1])
|
idReg1 = registerToDec(args[1])
|
||||||
return {"opcode": [0b01110000 | (idReg0 << 2) | (idReg1), valueToInt(args[2]), "label"], "label": args[0], "offset": 0}
|
return {"opcode": [0b01110000 | (idReg0 << 2) | (idReg1), "label"], "label": args[2], "offset": 0, "DB_Update": True}
|
||||||
|
|
||||||
def convertInsOUT(args):
|
def convertInsOUT(args):
|
||||||
|
idReg0 = registerToDec(args[0])
|
||||||
return {"opcode": [0b11110000]}
|
return {"opcode": [0b11110000 | idReg0]}
|
||||||
|
|
||||||
def convertInsTIM(args):
|
def convertInsTIM(args):
|
||||||
|
|
||||||
value = valueToInt(args[0])
|
value = valueToInt(args[0])
|
||||||
return {"opcode": [0b11111000, value]}
|
return {"opcode": [0b11111000, value]}
|
||||||
|
|
||||||
@ -356,6 +356,7 @@ def decodeInstruction(args, ins):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def decodeLine(line, PC):
|
def decodeLine(line, PC):
|
||||||
|
global lastLabel, labels
|
||||||
commentPos = line.find(";")
|
commentPos = line.find(";")
|
||||||
if (commentPos != -1):
|
if (commentPos != -1):
|
||||||
line = line[:line.find(";")]
|
line = line[:line.find(";")]
|
||||||
@ -371,7 +372,8 @@ def decodeLine(line, PC):
|
|||||||
#print(args)
|
#print(args)
|
||||||
|
|
||||||
if (testArgIsLabel(INS, twoDotsIncluded=True)):
|
if (testArgIsLabel(INS, twoDotsIncluded=True)):
|
||||||
labels[INS[:-1]] = PC
|
lastLabel = INS[:-1]
|
||||||
|
labels[lastLabel] = PC
|
||||||
return
|
return
|
||||||
|
|
||||||
instruction = None
|
instruction = None
|
||||||
@ -389,24 +391,25 @@ def decodeLine(line, PC):
|
|||||||
|
|
||||||
|
|
||||||
def assemble(path):
|
def assemble(path):
|
||||||
|
global lastLabel, labels
|
||||||
PC = 0
|
PC = 0
|
||||||
assemble1st = []
|
assemble1st = []
|
||||||
|
bytecode = []
|
||||||
with open(path, "r") as file:
|
with open(path, "r") as file:
|
||||||
# 1er lecture, pre-compilation
|
# 1er lecture, pre-compilation
|
||||||
for line in file:
|
for line in file:
|
||||||
print(line, end="")
|
print(line, end="")
|
||||||
ret = decodeLine(line, PC)
|
ret = decodeLine(line, PC)
|
||||||
if (ret != None):
|
if (ret != None):
|
||||||
|
PC += len(ret["opcode"])
|
||||||
assemble1st.append(ret)
|
assemble1st.append(ret)
|
||||||
if (not "DB" in ret):
|
|
||||||
PC += len(ret["opcode"])
|
|
||||||
print(" ==> ", ret)
|
print(" ==> ", ret)
|
||||||
print("\n\n\n\n\n\n")
|
print("\n\n\n\n\n\n")
|
||||||
print(assemble1st)
|
print(assemble1st)
|
||||||
|
|
||||||
print("Labels : ", labels)
|
print("Labels : ", labels)
|
||||||
|
|
||||||
bytecode = []
|
# Expansion des labels
|
||||||
for item in assemble1st:
|
for item in assemble1st:
|
||||||
if ("label" in item):
|
if ("label" in item):
|
||||||
labelIndex = labels[item["label"]]
|
labelIndex = labels[item["label"]]
|
||||||
@ -414,10 +417,11 @@ def assemble(path):
|
|||||||
if (item["opcode"][index] == "label"):
|
if (item["opcode"][index] == "label"):
|
||||||
item["opcode"][index] = labelIndex
|
item["opcode"][index] = labelIndex
|
||||||
bytecode.extend(item["opcode"])
|
bytecode.extend(item["opcode"])
|
||||||
pass
|
|
||||||
print("\n\n\n\n\n\n")
|
print("\n\n\n\n\n\n")
|
||||||
print(assemble1st)
|
print(assemble1st)
|
||||||
print(bytecode)
|
print(bytecode)
|
||||||
|
return bytecode
|
||||||
|
|
||||||
|
|
||||||
if (__name__ == "__main__"):
|
if (__name__ == "__main__"):
|
||||||
@ -431,26 +435,8 @@ if (__name__ == "__main__"):
|
|||||||
print(path)
|
print(path)
|
||||||
|
|
||||||
|
|
||||||
assemble(path)
|
code = assemble(path)
|
||||||
print("Labels :", labels)
|
with open(path + ".bin", "wb") as file:
|
||||||
|
file.write(bytes(code))
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
#decodeLine(" MOV R4 R2 ; COMMENTAIRE OUAISSSSSSSSS", 1)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# print(instructions)
|
|
||||||
|
|
||||||
|
|
||||||
# ligne = "MOV R0 R1"
|
|
||||||
|
|
||||||
# ins = instructions["MOV"]
|
|
||||||
|
|
||||||
|
|
||||||
# print("\n\n\n")
|
|
||||||
# print(ins)
|
|
||||||
# print(ins["args"])
|
|
||||||
# print(ins["args"][0])
|
|
||||||
# print(ins["args"][1])
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user