-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler_frontend.py
More file actions
102 lines (80 loc) · 3.33 KB
/
Copy pathassembler_frontend.py
File metadata and controls
102 lines (80 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import streamlit as st
from io import BytesIO
from assembler import init, look_for_loops_or_labels, assemble_line, remove_comments
def assemble_code(lines):
init()
look_for_loops_or_labels(lines)
machine_code = []
for line in lines:
clean_line = remove_comments(line)
if clean_line.strip() == "":
continue
machine_code_line = assemble_line(line)
if machine_code_line and not machine_code_line.startswith("ERROR"):
machine_code.append(machine_code_line)
elif machine_code_line.startswith("ERROR"):
machine_code.append(f"{line.strip()} --> {machine_code_line}")
return machine_code
def main():
st.set_page_config(page_title="TinyRISC Assembler", layout="wide")
st.title("🛠 TinyRISC Assembler")
st.write(
"Upload your `.asm` file or write code manually. You can also edit uploaded code before assembling."
)
uploaded_code = ""
uploaded_file = st.file_uploader("📁 Upload Assembly File (.asm)", type=["asm"])
if uploaded_file:
uploaded_code = uploaded_file.read().decode("utf-8")
st.subheader("✍️ Edit or Write Assembly Code")
code_area = st.text_area(
"Assembly Code", value=uploaded_code, height=400, key="code_editor"
)
if st.button("▶️ Assemble Code"):
if not code_area.strip():
st.warning("Please upload or write some Assembly code first.")
return
lines = code_area.splitlines()
try:
machine_code = assemble_code(lines)
st.session_state["machine_code"] = (
machine_code # 🔥 Save machine code to session state
)
# Convert to binary
binary_data = bytearray()
for code_line in machine_code:
if code_line.startswith("ERROR") or "-->" in code_line:
continue
try:
byte_value = int(code_line, 2)
binary_data += byte_value.to_bytes(4, byteorder="big")
except ValueError:
continue
st.session_state["binary_data"] = (
binary_data # 🔥 Save binary data to session state
)
st.success("✅ Assembly successful!")
except Exception as e:
st.error(f"❌ Error during assembly: {e}")
# Show machine code and download buttons AFTER assembly
if "machine_code" in st.session_state:
machine_code = st.session_state["machine_code"]
output_text = "\n".join(machine_code)
st.subheader("📄 Machine Code Output")
st.code(output_text, language="text")
mc_buffer = BytesIO(output_text.encode("utf-8"))
st.download_button(
"⬇️ Download .mc File",
data=mc_buffer,
file_name="output.mc",
mime="text/plain",
)
if "binary_data" in st.session_state and st.session_state["binary_data"]:
bin_buffer = BytesIO(st.session_state["binary_data"])
st.download_button(
"⬇️ Download .bin File",
data=bin_buffer,
file_name="output.bin",
mime="application/octet-stream",
)
if __name__ == "__main__":
main()