[英]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.