ぱたへね

はてなダイアリーはrustの色分けができないのでこっちに来た

Exercise 2.6

Some computers have explict instructions to extract an arbitrary field from a 32-bit register and to place it in the least significant bit of a register. The figure below shows the desired operatin

[Figure]

Find the shortest sequence of MIPS instructions that extracts a field for the constant value i = 5 and j = 22 from register $t3 and places it in register $t0(Hint:It can be done in two instructions.

register $t3の22bit目から5bit目までを$t0に入れる最短の命令を見つけなさい(ヒント:2命令)

Verilog だと t0 <= {14'h0000, t3[22:5]}; で簡単。

.data
a: .word 0xFFC0003F
.text
main:
	lw $t3, a
	sll $t0, $t3 9
	srl $t0, $t0 14

最終的に右シフトをすれば、LSB側はそろいます。教科書の図を見るとMSB側の関係のないbitを0にしないといけません。右シフトの前に左シフトを実行し、上位ビットを落としています。ヒントが無ければ、右シフト+論理演算で書いてしまいそうな問題でした。

# 1bit問題を読み違えていそうな気もするのですが、見逃してください。