[英]MIPS Storing polynomials and branches
我有一个将两个多项式相除的 mips 汇编程序,但我不确定如何存储值和余数。 我一直无法理解 sw 指令的偏移部分是如何工作的。 欢迎任何帮助。
## Evaluate (8x^2-3x+12)/(3x^2+2x-16)
.text
.globl main
main:
lui $10, 0x1000 # Init base register
lw $11, 0($10) # Load x
sll $0,$0,0 # noop
ori $12,$0,0 # Init the accumulator
# during the "load delay slot"
ori $13,$0,2 # Evaluate second term coefficient
mult $11,$13 # 2x
mflo $13 # assume 32 bit result
ori $14,$0,16 # register 14 = 16
addu $12,$12,$13 # accumulator = 2x
subu $12,$12,$14 # accumulator = 2x-16
mult $11,$11 # x^2
mflo $11 # assume 32 bit result
ori $13,$0,3 # evaluate third term coefficient
mult $11,$13 # 3x^2
addu $12,$12,$13 # accumulator = 3x^2+2x-16
ori $15,$0,12 # init the accumulator
# during the "load delay slot"
ori $13,$0,3 # Evaluate second term coefficient
mult $11,$13 # 3x
mflo $13 # assume 32 bit result
subu $15,$15,$13 # accumulator = -3x+12
mult $11,$11 # x^2
mflo $11 # assume 32 bit result
ori $13,$0,8 # third term coefficient
mult $11,$13 # 8x^2
mflo $13 # assume 32 bit result
addu $15,$15,$13 # accumulator = 8x^2-3x+12
addu $13,0 # make temp 0
beq $12,$13,equal # branch if denom is 0
sll $0,$0,0 # branch delay slot
addu $16,0 # set Error to 0
div $15,$12 # divide the two accumulators
mflo $12 # the quotient is in $12
mfhi $15 # the remainder is in $15
sw $12,8($10) # store $12 in ratio
sw $15,12($10) # store $15 in remain
ori $16,$0,1 # set Error to 1
sw $16,4($10) # store 1 in error
j cont
sll $0,$0,0 # branch delay slot
equal: ori $16,$0,1 # set Error to 1
sw $16,4($10) # store 1 in error
cont: sll $0,$0,0 # noop
.data
x: .word 1 # Edit this line to change x
error: .word 0 # Error value is placed here
ratio: .word 0 # Ratio value is placed here
remain: .word 0 # Remainder value placed here
## End of file
您没有提到您使用的是什么环境,但如果它的 MARS 或 SPIM ,例如,那么数据部分( .data
)从地址0x10010000
开始,而不是0x10000000
,所以第一个全局变量将位于那里而不是你所在的位置思维。
因此,您的初始lui
1。这将使访问您的全局变量变得困难。 您是否成功地将x
从0($10)
加载到$11
中? 如果不是,这就是原因。
无论您使用的是 MARS 还是 SPIM 或其他东西,使用la $10, x
而不是原始的lui
可能会更好。
lw
和sw
指令提供了一个简单的基址寄存器加位移寻址模式,仅此而已。 处理器计算有效地址(发送到 memory 单元用于读取或写入的地址)作为指令中指定的基址寄存器的内容,加上指令的符号扩展 16 位立即数。 因此,您可以从基址寄存器达到 +/-32k。 如果$10
中的基数指向x
,那么您正在正确使用商店。
但是,如果基址寄存器(此处$10
)指向0x10000000
您将无法访问您的变量x
因为您需要一个立即值 +65536 (又名0x10000
),该值大于 MIPS 可以在一条指令中为您提供的.
但是,许多人会改用数据标签,例如sw $16,error
来存储错误而不是sw $16,4($10)
。
可以肯定的是,使用 label 访问全局变量是伪指令,汇编程序将其扩展为两条指令,但它们更容易让我们在汇编中阅读——而且在使用标签时,我们可以移动东西,它们仍然可以工作,而使用偏移量,您必须正确排序/编号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.