【发布时间】:2022-12-03 06:56:52
【问题描述】:
如何解决下面的问题。答案在通行证中
# The OAM class defines the OAM model.
class OAM():
# The constructor defines three instance variables: the debug
# flag, which regulates the level of output produced at runtime,
# the labels dictionary, which defines the mapping from labels to
# memory locations, and the list representing memory. It also
# initializes the OAM memory (a list) and the label reference
# table (a dictionary), with the standard names for I/O (stdin,
# stdout) included.
def __init__(self, debug=False):
self.debug = debug # Run time output
self.pc = 1 # Program counter
self.ar = '?' # Address register
self.ir = '?' # Instruction register
self.acc = '?' # Accumulator
self.b = '?' # B register
self.mem = [] # Memory
self.labels = {'stdin':0, 'stdout':0} # Labels, including I/O
# The verbose() method toggles the debug variable, which governs
# run time reporting status.
def verbose(self):
self.debug = not self.debug
# The run() method initalizes the machine (but doesn't clear
# memory or labels) and then implements the
# fetch/increment/execute cycle.
def run(self):
self.pc = 1
self.ar = '?'
self.ir = '?'
self.acc = '?'
self.b = '?'
while self.pc > 0:
self.fetch()
self.increment()
self.execute()
if self.debug:
print("Processing halted.")
# The fetch() method implements the fetch cycle.
def fetch(self):
self.ar = self.pc
self.ir = self.read()
if self.debug:
print("Fetch: AR = {} IR = {}".format(self.ar, ' '.join(self.ir)))
# The increment() method implements the increment cycle.
def increment(self):
self.pc = self.pc + 1
if self.debug:
print(" Increment: PC = {}".format(self.pc))
# The execute() method implements the execute cycle, dispatching
# to the appropriate method as per the first part of the
# IR. Returns a Boolean indicating whether execution should
# continue.
def execute(self):
# Check for a match, report an issue
if self.debug:
print(" Execute: IR = '{}'".format(self.ir))
try:
exec('self.' + self.ir[0] + '()')
except:
if self.debug:
print("Abort: ill-formed instruction IR = '{}'".format(self.ir))
self.pc = 0
这是需要回答的问题。
# The resolve() method resolves a reference to a memory location,
# which may be an integer or a reference label, such as may be
# found in an instruction, and returns an int.
def resolve(self, address):
pass
所以看起来我们需要使用 resolve() 方法。这就是问题所在。 resolve() 方法解析对内存位置的引用,它可以是整数或引用标签,例如可以在指令中找到,并返回一个 int。 我很困惑,不知道如何解决。
【问题讨论】:
-
在我看来它应该获取一个地址并返回该地址的数据,但它也可能要求将标签解析为地址。从您提供的上下文中不清楚。
-
请阅读有关如何提出好问题的指南 (stackoverflow.com/help/how-to-ask)。这违背了那篇文章中的几乎所有原则:标题没有描述具体问题;这不是最小的可重现错误;你直接进入一大块代码;在确定已经存在的任何东西都无法帮助您之前,您没有解释您已经尝试过的东西或您搜索过的东西。
标签: python