我目前正在做一個作業分配,它將一個位值(0x0或0x1)插入到第n個位置。MIPS程序集沒有正確移動
.macro insert_to_nth_bit($regD, $regS, $regT, $maskReg)
# regD: bit pattern in which to be inserted at nth position
# regS: position n (0-31)
# regT: the bit to insert (0x0 or 0x1)
# maskReg: temporary mask
# Let's say regD = 00000101
sllv $maskReg, $maskReg, $regS # Let's say regS = 2, the second position at regD(0)
# maskReg = 00000010 after shifting
not $maskReg, $maskReg # maskReg = 11111101
and $regD, $regD, $maskReg # regD = 00000101
sllv $regT, $regT, $regS # regT = 00000001, we want to insert 1 into the 2nd position at regD
# regT = 00000010 after shifting
or $regD, $regD, $regT # 00000101 OR 00000010 = 00000111. The bit is what i wanted
.end_macro
這是我寫來測試它的宏觀
.text
.globl main
la $t0, 0x00000101 #t0 = 00000101
la $t1, 2 # nth position = 2
la $t2, 0x1 # insert 0x1
la $t3, 1 # maskReg = 00000001
insert_to_nth_bit($t0, $t1, $t2, $t3)
print_int($t0)
exit
print_int
和exit
另外兩個小宏
結果,我得到的是261,這是00000105之後,我轉換爲十六進制。當我調試它時,我注意到當談到第一班時,00000001和左移2變成了00000005,這使整個事情變得糟糕。我不知道我的宏的邏輯是錯誤的還是我測試宏的方式是錯誤的,所以它搞砸了我的輸出?
1左移兩次是4,或者說到0x101你得到0x105。問題是什麼? –
@SamiKuhmonen我想我只是想通了,而不是將2傳入$ t1,我應該將4傳入$ t1。我只是想知道這個宏的邏輯是正確的嗎? – user21478621