You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24-11Lines changed: 24 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ Any empty files, fully commented files, or files without `.s` extension will not
58
58
59
59
## Instruction Format Functions
60
60
61
-
This package also offers instruction format-specific functions for individual lines of assembly. The instruction types supported are R, I, S, SB, U, and UJ. The outputs to these are written to text or binary files or printed to console, depending on how the constructor was initialized. For the below examples, they are being outputted to binary files.
61
+
This package also offers instruction format-specific functions for individual lines of assembly. The instruction types supported are R, I, S, SB, U, and UJ. The machine code is returned as a bitstring that can be printed to console. Check out the examples below for each instruction type:
62
62
63
63
### R_type()
64
64
@@ -70,8 +70,10 @@ Here is an example of translating `add x0 s0 s1`
70
70
71
71
from riscv_assembler.convert import AssemblyConverter
72
72
73
-
cnv = AssemblyConverter() #output to binary
74
-
cnv.R_type("add","x0","s0","s1") #convert the instruction
73
+
cnv = AssemblyConverter()
74
+
instr = cnv.R_type("add","x0","s0","s1") #convert the instruction
75
+
76
+
print(instr)
75
77
76
78
Note that the registers are being written as strings. The package maps them correctly to their respective binary values (ex. `s0` maps to `x8`).
77
79
@@ -86,7 +88,9 @@ Here is an example of translating `addi x0 x0 32`
86
88
from riscv_assembler.convert import AssemblyConverter
87
89
88
90
cnv = AssemblyConverter() #output to binary
89
-
cnv.I_type("addi","x0","32","x0") #convert the instruction
91
+
instr = cnv.I_type("addi","x0","32","x0") #convert the instruction
92
+
93
+
print(instr)
90
94
91
95
Note that the immediate is a string, not just a number. This was implemented this way for seamless integration with the convert() function, there is an easy workaround for using it on its own.
92
96
@@ -101,7 +105,9 @@ Here is an example of translating `sw x0 0(sp)`
101
105
from riscv_assembler.convert import AssemblyConverter
102
106
103
107
cnv = AssemblyConverter() #output to binary
104
-
cnv.S_type("sw","x0","sp","0") #convert the instruction
108
+
instr = cnv.S_type("sw","x0","sp","0") #convert the instruction
109
+
110
+
print(instr)
105
111
106
112
### SB_type()
107
113
@@ -114,7 +120,9 @@ Here is an example of translating `beq x0 x1 loop`:
114
120
from riscv_assembler.convert import AssemblyConverter
115
121
116
122
cnv = AssemblyConverter() #output to binary
117
-
cnv.SB_type("beq","x0","x1","loop") #convert the instruction
123
+
instr = cnv.SB_type("beq","x0","x1","loop") #convert the instruction
124
+
125
+
print(instr)
118
126
119
127
Note that the jump is written as a string, the appropriate instruction jump is calculated by the package.
120
128
@@ -129,7 +137,9 @@ Here is an example of converting `lui x0 10`:
129
137
from riscv_assembler.convert import AssemblyConverter
130
138
131
139
cnv = AssemblyConverter() #output to binary
132
-
cnv.U_type("lui","x0","10") #convert the instruction
140
+
instr = cnv.U_type("lui","x0","10") #convert the instruction
141
+
142
+
print(instr)
133
143
134
144
### UJ_type()
135
145
@@ -142,7 +152,9 @@ Here is an example of converting `jal a0 func`:
142
152
from riscv_assembler.convert import AssemblyConverter
143
153
144
154
cnv = AssemblyConverter() #output to binary
145
-
cnv.UJ_type("jal","func","a0") #convert the instruction
155
+
instr = cnv.UJ_type("jal","func","a0") #convert the instruction
156
+
157
+
print(instr)
146
158
147
159
## Helper Functions
148
160
@@ -153,12 +165,13 @@ Here are a few functions that might be useful:
153
165
This function simply prints the output type that has been initially selected. Example usage:
154
166
155
167
cnv = AssemblyConverter("bt") #initially write to binary and text file
0 commit comments