[英]Python “local variable 'pc' referenced before assignment” issue in basic decomplier
A friend and myself are working on creating a basic proof-of-concept decompiler that takes a string of hex values and returns a more readable version. 一个朋友和我本人正在努力创建一个基本的概念验证反编译器,该反编译器将接受一串十六进制值并返回更具可读性的版本。 Our code is listed below
我们的代码在下面列出
testProgram = "00 00 FF 55 47 00"
# should look like this
# NOP
# NOP
# MOV 55 47
# NOP
pc = 0
output = ""
def byte(int):
return testProgram[3 * int:3 * int + 2]
def interpret():
currentByte = byte(pc)
if currentByte == "00":
pc += 1
return "NOP"
if currentByte == "FF":
returner = "MOV " + byte(pc + 1) + " " + byte(pc + 2)
pc += 3
return returner
while(byte(pc) != ""):
output += interpret() + "\n"
print(output)
however, running the code tells us this 但是,运行代码可以告诉我们
Traceback (most recent call last):
File "BasicTest.py", line 62, in <module>
output += interpret() + "\n"
File "BasicTest.py", line 50, in interpret
currentByte = byte(pc)
UnboundLocalError: local variable 'pc' referenced before assignment
Because pc is a global variable, shouldn't it be usable from anywhere? 因为pc是全局变量,所以它不应该在任何地方都可以使用吗? Any and all help is appreciate - if you spot other errors, feel free to leave a comment pointing them out!
感谢您提供所有帮助-如果发现其他错误,请随时发表评论指出错误!
Been seeing this a lot lately. 最近经常看到这种情况。 When you do
当你做
if currentByte == "00":
pc += 1 # <----------
return "NOP"
You're assigning to the local variable pc
, but pc
isn't declared yet in the local scope. 您正在分配给局部变量
pc
,但是在局部作用域中尚未声明pc
。 If you want to modify the global pc
you need to declare that explicitly at the top of the function 如果要修改全局
pc
,则需要在函数顶部显式声明
global pc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.