CD-ROMから
<§2.5> Consider the following code used to implement the instruction sllv $s0, $s1, $s2 # which uses the least significant 5 bits of the value in register $s2 to specify the amount register $s1 should be shifted left:
Add comments to the code and write a paragraph describing how it works.
アセンブラの各行にコメントをいれるという問題。
.data mask: .word 0xfffff83f .text main: lw $t0, mask # レジスタt0に、maskの値 0xfffff83fをロード。 lw $s0, shifter # レジスタs0に、「sll $s0,$s1,0」の機械語表現0x00118000をロード and $s0,$s0,$t0 # レジスタs0とレジスタt0のandを取って、レジスタs0に代入。0xfffff83fと0x00118000なので変化無し。 andi $s2,$s2,0x1f # レジスタs2と、即値0x1fのandを取ってs2に代入。s2が0ならば変化無し。 sll $s2,$s2,6 # レジスタs2を、6bit論理左シフトしてs2に代入。s2が0ならば変化無し。 or $s0,$s0,$s2 # レジスタs0とレジスタs2のorを取ってs0に代入。s2が0ならば変化無し。 sw $s0, shifter # shifterのアドレスへ、レジスタs0の内容をストアする。 # レジスタs0は、ロード時から変化していないので、変化無し shifter: sll $s0,$s1,0 # レジスタs1を0bitシフトして、レジスタs0へ代入。レジスタs0は0になる。
startはmainにラベルを変更しました。
プログラムとしては全然動いていないのですが、そういう問題なのでしょう。特定のアドレス、この場合はshifterの命令をレジスタに入れて、演算をしまた戻すことで自分自身のコードを書き換えることになります。存在しない命令を書いてしまうと、そこを実行したとたん未定義命令の例外が発生するので普通はやりません。