Assembleur final

This commit is contained in:
Nogard 2026-03-21 19:52:18 +01:00
parent a4cc3b36a6
commit fe9ca66500
3 changed files with 40 additions and 32 deletions

22
Epreuve2.asm Normal file
View 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

View File

@ -155,12 +155,13 @@ instructions = {
}
labels = {}
lastLabel = ""
def valueToInt(arg):
try:
return int(arg)
except:
return ord(arg)
return ord(arg[1])
def registerToDec(reg):
return int(reg[1])
@ -280,19 +281,18 @@ def convertInsCMP(args):
def convertInsLDR(args):
idReg0 = registerToDec(args[0])
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):
idReg0 = registerToDec(args[0])
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):
return {"opcode": [0b11110000]}
idReg0 = registerToDec(args[0])
return {"opcode": [0b11110000 | idReg0]}
def convertInsTIM(args):
value = valueToInt(args[0])
return {"opcode": [0b11111000, value]}
@ -356,6 +356,7 @@ def decodeInstruction(args, ins):
pass
def decodeLine(line, PC):
global lastLabel, labels
commentPos = line.find(";")
if (commentPos != -1):
line = line[:line.find(";")]
@ -371,7 +372,8 @@ def decodeLine(line, PC):
#print(args)
if (testArgIsLabel(INS, twoDotsIncluded=True)):
labels[INS[:-1]] = PC
lastLabel = INS[:-1]
labels[lastLabel] = PC
return
instruction = None
@ -389,24 +391,25 @@ def decodeLine(line, PC):
def assemble(path):
global lastLabel, labels
PC = 0
assemble1st = []
bytecode = []
with open(path, "r") as file:
# 1er lecture, pre-compilation
for line in file:
print(line, end="")
ret = decodeLine(line, PC)
if (ret != None):
assemble1st.append(ret)
if (not "DB" in ret):
PC += len(ret["opcode"])
assemble1st.append(ret)
print(" ==> ", ret)
print("\n\n\n\n\n\n")
print(assemble1st)
print("Labels : ", labels)
bytecode = []
# Expansion des labels
for item in assemble1st:
if ("label" in item):
labelIndex = labels[item["label"]]
@ -414,10 +417,11 @@ def assemble(path):
if (item["opcode"][index] == "label"):
item["opcode"][index] = labelIndex
bytecode.extend(item["opcode"])
pass
print("\n\n\n\n\n\n")
print(assemble1st)
print(bytecode)
return bytecode
if (__name__ == "__main__"):
@ -431,26 +435,8 @@ if (__name__ == "__main__"):
print(path)
assemble(path)
print("Labels :", labels)
code = assemble(path)
with open(path + ".bin", "wb") as file:
file.write(bytes(code))
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])

BIN
out.bin Normal file

Binary file not shown.