【问题标题】:Multiply using addition and a restricted set of instructions使用加法和一组受限指令进行乘法
【发布时间】:2013-12-14 20:20:19
【问题描述】:

我正在使用 Logisim 构建 CPU 电路。我的 CPU 只有 2 个通用寄存器和一个 16 字节 RAM。我已经对以下指令集进行了编码(Rxy 表示两个寄存器之一)

• ADD Rxy, Rxy (add Rxy and Rxy and store result inside the first register) 
• SUB Rxy, Rxy (same but with sub)
• ST Rxy, Address (save value of Rxy into RAM address)
• LD Rxy, Address (load into Rxy value at RAM address)
• BZ Rxy, Address (branch to address if value of Rxy is zero)

我想我可以使用递减第二个加数直到它达到 0 并且在每一步将第一个加数添加到自身。

For example, 5*3 = 5+5+5 ; 3-1-1-1

但我不确定我的指令是否允许这个程序...如果 Rxy 等于 0,我只有一个分支,而如果 not 等于 0,我想分支。

我的程序目前看起来像这样:

假设 R1 预加载了第二个加数(迭代左计数)

(A) LD R0, Address      # constant 1
    SUB R1, R0      # decrement iteration left
    ST R1, Address      # save iteration count in memory
    LD R0, Address      # Load first addend
    LD R1, Address      # load current total
    ADD R0, R1      # do addition
    ST R0, Address      # save new current total

BZ R1, (B)          # if iteration is 0, then display and end, else add

(B)
    STOP

有没有办法循环使用我的指令集?

【问题讨论】:

  • 程序计数器是您 CPU 架构中的通用寄存器吗?如果是这样,您可以通过将地址从内存加载到该寄存器来执行无条件分支。否则,您可以将零加载到备用寄存器中并使用BZ
  • PC 是另一个寄存器。当程序执行 BZ 时,控件写入它(如果 R0 == 0,它将把 Address 放入 PC 中)。主要问题是如果剩余计数是 not 我想循环,如果是 BZ 将循环..
  • 当然,但是如果您有办法进行无条件分支(我们称之为B,即使在您的情况下它可能是指令的组合),您可以模拟BNZ使用BZ skip / B loop / skip:
  • 不幸的是,我没有实现无条件分支。我同意这会简单得多,但有很多因素导致我无法添加任何新指令。
  • 我发表的第一条评论包含了如何进行无条件分支的建议。如果这些都不可能,您可能会不走运。

标签: cpu mips cpu-registers instruction-set


【解决方案1】:

你可以改变

BZ R1, (B)

(B)

BZ R1, (B)
LD R0, Address      # constant 1
SUB R0, R0
BZ R0, (A)

(B)

【讨论】:

  • 这是一种非常优雅的方法!我结束了实施 BNZ 而不是 BZ :/
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2011-02-16
  • 2016-02-28
  • 2022-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多