如何让这个LC3程序代替乘法?

问题描述 投票:0回答:1

我想学习如何在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个回答
2
投票

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

有很多注意事项:如果乘数是负数,那么我们要么将其否定以用于倒数,要么用倒数代替--无论哪种方式,最后的结果都会被否定。

由于乘法是换算式的,我们可以考虑使用较小的(绝对)值作为乘数,这样可以减少迭代次数。  但是,如果要获得更优化的乘法,我们将改用另一种算法,即 移加.  请注意,这种算法通常是针对硬件实现提出的,在硬件中,节省宝贵的寄存器位是很重要的,而对于软件来说,这并不是一个真正重要的问题。

© www.soinside.com 2019 - 2024. All rights reserved.