I am converting a assembling language into binary and then into hex I am having a few problems,
My first problem is below, this code works fine however I have a variable that you will see below
add=commands[2] this seems to break the below code and when I put JMP 1000 in the input I get this error.
Traceback (most recent call last):
File "C:Desktopcomputational1111.py", line 164, in <module>
add = (commands[2])
IndexError: list index out of range
if len(commands) < 3 and commands[0] == "JMP" :
output = "10000000"
elif len(commands) < 3 and commands[0] == "JPZ":
output = "10000001"
elif len(commands) < 3 and commands[0] == "JPP":
output = "10000010"
elif len(commands) < 3 and commands[0] == "JPN":
output = "10000011"
else:
#remove the trailing comma from commands[1]
commands[1] = commands[1][:-1]
if I remove add it works is there something I am missing with this.
Finally
I am having a issue with this piece of code.
if (commands[0] =="STORE"):
print("This is the binary :", output_store, output3, HS, add2)
BintoHex3 = output_store + output3 + HS + add2
Hex3 =binToHex(BintoHex3)
print("This is the hexidecmial :" +Hex3 + " " + add2)
Then I input STORE 1000, B into the input I get this error
Traceback (most recent call last):
File "C:Desktopcomputational1111.py", line 230, in <module>
Hex3 =binToHex(BintoHex3)
File "C:Desktopcomputational1111.py", line 52, in binToHex
returnString += binToHexDictionary.get(binaryString[marker:marker+4])
TypeError: can only concatenate str (not "NoneType") to str
However if I type into the input LOAD B, 1000 this works fine and is the same code as used for STORE.
I don't see where I am going wrong with this.
Any help would be great and a huge help thank you.
# Conversion between Hex and Binary
binToHexDictionary = {
"0000": "0",
"0001": "1",
"0010": "2",
"0011": "3",
"0100": "4",
"0101": "5",
"0110": "6",
"0111": "7",
"1000": "8",
"1001": "9",
"1010": "A",
"1011": "B",
"1100": "C",
"1101": "D",
"1110": "E",
"1111": "F",
}
# a map for the other way
hexToBinDictionary = {
"0": "0000",
"1": "0001",
"2": "0010",
"3": "0011",
"4": "0100",
"5": "0101",
"6": "0110",
"7": "0111",
"8": "1000",
"9": "1001",
"A": "1010",
"B": "1011",
"C": "1100",
"D": "1101",
"E": "1110",
"F": "1111",
}
def binToHex(binaryString):
# make sure the string has blocks of 4 bits
while len(binaryString)%4 != 0:
binaryString = "0"+binaryString
# now go through the string and convert each 4 blocks to a Hex digit using the dictionary defined at the start of this file.
marker = 0
returnString = ""
while marker < len(binaryString):
returnString += binToHexDictionary.get(binaryString[marker:marker+4])
marker += 4
return returnString
def binToDec(binString):
# we could validate the string here to make sure it's only 1s and 0s
# we want to work from the right hand side of the string to the left
# the first value will be worth 1 so have a variable for that
columnValue = 1
numberValue = 0
# now loop from the length of the string to zero, subtracting by 1 each time
for i in range(len(binString),0,-1):
# if there is a 1, add the column value to the total
if(binString[i-1] == "1"):
numberValue += columnValue
# increase the column value for the next one along
columnValue *= 2
# return the value now we've calculated it
return numberValue
def decToBin(decString):
# we can use the repeated division to calculate this.
# first convert the string to a decimal number
decimalNumber = int(decString)
outputString = ""
# we keep dividing until we end up with 1/2
while(decimalNumber / 2 > 0.5):
# % (modulus) returns the remainder when dividing so add that to the binary string
#print(decimalNumber%2)
outputString += str(decimalNumber%2)
# now we have the remainder, we just want to do the division,
# cast to an int so we don’t have a decimal answer
decimalNumber = int(decimalNumber / 2)
# we stopped the loop on 1 so add this to the end
outputString += "1"
# finally we want to read the remainders backward,
# so we reverse the string before returning it
return outputString[::-1]
def decToSBin(decString):
# we can use the repeated division to calculate this.
# first convert the string to a decimal number
decimalNumber = int(decString)
outputString = ""
isNegative = False
if decimalNumber < 0:
isNegative = True
# we need to convert it as a positive number
# then afterwards flip the bits and add one.
# if we subtract one from the positive value,
# this has the same effect
decimalNumber = (decimalNumber * -1) - 1;
# we keep dividing until we end up with 1/2
while (decimalNumber > 0):
# % (modulus) returns the remainder when dividing so add that to the binary string
outputString += str((decimalNumber%2))
# now we have the remainder, we just want to do the division,
# but subtract 1 if the number is odd so we end up with a whole number
decimalNumber = int(decimalNumber / 2)
# add a zero to the end because we're dealing with signed binary
outputString += "0"
# this needs to be grouped into 8 bits
while len(outputString) % 8 != 0:
outputString += "0"
# finally, if we were originally dealing with a negative number, flip the bits.
if(isNegative):
output = ""
index = 0
while index < len(outputString):
if outputString[index] == "0":
output += "1"
else:
output += "0"
index = index + 1
else:
output = outputString
# lets reverse the string to return it
return output[::-1]
#get input from the user
userInput = (input("Please enter your command followed by two numbers: "))
#break this command up into parts by finding the spaces
commands = userInput.split(" ")
output = ("")
output2 =("")
output3 = ("")
output_jump = ("")
output_number = ("")
output_number_neg = ("")
output_store=("")
output_load=("")
add = (commands[2])
add2 = (commands[1])
HS= ("110")
Hex =("")
Hex2 = ("")
Hex3 = ("")
#look at the instruction size
if len(commands) < 3 and commands[0] == "JMP" :
output = "10000000"
elif len(commands) < 3 and commands[0] == "JPZ":
output = "10000001"
elif len(commands) < 3 and commands[0] == "JPP":
output = "10000010"
elif len(commands) < 3 and commands[0] == "JPN":
output = "10000011"
else:
#remove the trailing comma from commands[1]
commands[1] = commands[1][:-1]
#check the opcode
if(commands[0] == "ADD"):
output = "00"
else:
if(commands[0] == "MOVE"):
output = "01"
if (commands[0] =="STORE"):
output_store = "01"
if (commands[0] =="LOAD"):
output_load = "01"
if (commands[1] == "A"):
output2 = "000"
elif (commands[1] == "B"):
output2 = "001"
elif (commands[1] == "C"):
output2 = "010"
elif (commands[1] == "D"):
output2 = "011"
elif (commands[1] == "E"):
output2 = "100"
if (commands[2] == "A"):
output3 = "000"
elif (commands[2] == "B"):
output3 = "001"
elif (commands[2] == "C"):
output3 = "010"
elif (commands[2] == "D"):
output3 = "011"
elif (commands[2] == "E"):
output3 = "100"
if (commands[2].isdigit()):
output_number = "101" + "00000" + decToBin(commands[2])
elif(commands[2].lstrip("-").isdigit()):
output_number_neg = "101" +"00000" + decToSBin(commands[2])
if (commands[0] =="STORE"):
print("This is the binary :", output_store, output3, HS, add2)
BintoHex3 = output_store + output3 + HS + add2
Hex3 =binToHex(BintoHex3)
print("This is the hexidecmial :" +Hex3 + " " + add2)
if(commands[0] =="LOAD"):
print("This is the binary :", output_load, output2,HS,add)
BintoHex2 = output_load + output2 +HS + add
Hex2 =binToHex(BintoHex2)
print("This is the hexidecmial :", Hex2 + " " + add)
else:
print("This is the Binary :", output, output2, output_number, output_number_neg, output3)
BintoHex1 = output + output2 + output3 + output_number + output_number_neg
Hex = binToHex(BintoHex1)
print ("This is the hexidecmial :", Hex)
question from:
https://stackoverflow.com/questions/65847979/issue-with-output-converting-binary-into-assembly-language-and-then-to-hex-in-py