【问题标题】:How to make this LC3 program multiply instead?如何让这个 LC3 程序成倍增加?
【发布时间】:2020-08-06 03:12:33
【问题描述】:

试图学习如何在 LC3 中进行乘法运算,但在修改我的旧程序时遇到了麻烦,该程序仅用于相加。我将如何修改这个程序以乘以 2 个给定的输入? 代码:

.ORIG x3000 ; begin at x3000

; input two numbers

IN ;input an integer character (ascii) {TRAP 23}

LD R3, HEXN30 ;subtract x30 to get integer

ADD R0, R0, R3

ADD R1, R0, x0 ;move the first integer to register 1

IN ;input another integer {TRAP 23}

ADD R0, R0, R3 ;convert it to an integer

; add the numbers

ADD R2, R0, R1 ;add the two integers

; print the results

LEA R0, MESG ;load the address of the message string

PUTS ;"PUTS" outputs a string {TRAP 22}

  ADD R0, R2, x0 ;move the sum to R0, to be output

  LD R3, HEX30 ;add 30 to integer to get integer character

  ADD R0, R0, R3

  OUT ;display the sum {TRAP 21}

     ; stop

HALT ;{TRAP 25}

; data

 MESG .STRINGZ "The sum of those two numbers is: "

 HEXN30 .FILL xFFD0 ; -30 HEX

 HEX30 .FILL x0030 ; 30 HEX

.END```

【问题讨论】:

    标签: loops math assembly ascii lc3


    【解决方案1】:

    在 LC-3 上进行乘法的最简单方法是重复加法。所以不断地对被乘数求和并减少乘数;当乘数被消耗(即零)时,迭代停止。

    有很多警告:如果乘数是负数,那么我们要么否定它以使用向下计数,要么改为向上计数 - 无论哪种方式,最终结果都会被否定。

    由于乘法是可交换的,我们可以考虑为乘数使用 lessor(绝对)值,以便完成更少的迭代。但为了获得更优化的乘法,我们将切换到一个完整的“另一种算法,shift and add”。请注意,此算法通常用于硬件实现,其中节省宝贵的寄存器位很重要,而对于软件来说,这并不是一个真正重要的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      相关资源
      最近更新 更多