I have made a MARIE code that takes the input of 8 information bits and converts it to 12-bit hamming code. How to shorten this code? I am using the same XOR function for each parity bit with different value of each bit. I have first taken input for 8 bits in a loop and stored it at different addresses like an array. Then, I found the XOR of different parity bits (knowing the positions beforehand) and substituted the parity bits in the main information word (using the addresses of each position). Finally gave an output of the hamming code.
I have posted the code below:
'''
ORG 100
getInput, LoadI data
Skipcond 000
Input
Store temp
LoadI data
Skipcond 000
Load temp
StoreI data
Load data
Add one
Store data
Clear
Load size
Subt one
Store size
Skipcond 400
Jump getInput
Load dataTop
Add two
Store data
Load sizeMax
Store size
p1loop, LoadI data
Add p1
Store p1
Load data
Add two
Store data
Load size
Subt two
Store size
Skipcond 400
Jump p1loop
XOR, Load p1
Subt two
Store p1
Skipcond 000
Jump XOR
Add two
Store p1
Load dataTop
Store data
Load p1
StoreI data
Load dataTop
Add two
Store data
LoadI data
Store p2
Load sizeMax
Subt 2
Store size
p2loop, Load data
Add three
Store data
LoadI data
Add p2
Store p2
Load data
Add one
Store data
LoadI data
Add p2
Store p2
Load size
Subt four
Store size
Skipcond 400
Jump p2loop
XOR2, Load p2
Subt two
Store p2
Skipcond 000
Jump XOR2
Add two
Store p2
Load dataTop
Add one
Store data
Load p2
StoreI data
Load dataTop
Add four
Store data
Load three
Store size
p3loop, LoadI data
Add p3
Store p3
Load data
Add one
Store data
Load size
Subt one
Store size
Skipcond 400
Jump p3loop
Load dataTop
Add eleven
Store data
LoadI data
Add p3
Store p3
XOR3, Load p3
Subt two
Store p3
Skipcond 000
Jump XOR3
Add two
Store p3
Load dataTop
Add three
Store data
Load p3
StoreI data
Load dataTop
Add eight
Store data
Load eight
Store size
p4loop, LoadI data
Add p4
Store p4
Load data
Add one
Store data
Load size
Subt one
Store size
Skipcond 400
Jump p4loop
XOR4, Load p4
Subt two
Store p4
Skipcond 000
Jump XOR4
Add two
Store p4
Load dataTop
Add seven
Store data
Load p4
StoreI data
Load dataTop
Store data
Load sizeMax
Store size
ShowOutput, Clear
LoadI data
Output
Load data
Add one
Store data
Clear
Load size
Subt one
Store size
Skipcond 400
Jump ShowOutput
Halt
one, DEC 1
two, DEC 2
three, DEC 3
four, DEC 4
seven, DEC 7
eight, DEC 8
eleven, DEC 11
p1, DEC 0
p2, DEC 0
p3, DEC 0
p4, DEC 0
sizeMax, DEC 12
size, DEC 12
dataTop, HEX 1B0
temp, DEC 0
data, HEX 1B0
DEC -1
DEC -1
DEC 1
DEC -1
DEC 1
DEC 1
DEC 1
DEC -1
DEC 1
DEC 1
DEC 1
DEC 1
'''
Some small points:
XOR4, Load p4
Subt two
Store p4
Skipcond 000
Jump XOR4
Add two
Store p4
Don't need to both load & store p4 inside the loop, so this would also work:
Load p4
XOR4,
Subt two
Skipcond 000
Jump XOR4
Add two
Store p4
For another point, you don't really need p2 through p4, because when the code is done with p1
, it never uses it again, instead switching to p2
, finishes with that then moves on to p3
. The only benefit of having p2 through p4 is that they are initialized to 0. So, the code could use just p1 everywhere as long as it is cleared before looping.
I mention that because one approach to making the code smaller is to share the same code for all 4 looping operations. That would require some abstraction, and removing the unnecessary variables can help with that.
Otherwise, most of the various loops read the same except for the constants they use. So, perhaps put the constants in a table, and then an outer loop of around the shared code so it runs 4 times, using different constants from the table.
The 4 loops do have some other differences, and perhaps those differences could also be selected from the table or perhaps by testing the outer loop variable, within some if-then type constructs in the inner loop.