3
       .data
VALS:  .half 0xbead, 0xface
RES:   .space 4
    .text
    la     $t0,VALS
    lh    $t1,($t0)
    lhu    $t2,2($t0)
    sll    $t2,$t2,16
    or    $t2,$t1,$t2
    jal    AVG
    .word  -2
    .word  -6
    la    $t4,RES
       sw  $v0,($t4)
       li     $v0,10
    syscall
AVG:   lw  $v0,($ra)
    lw    $t3,4($ra)
    add    $v0,$v0,$t3
    sra    $v0,$v0,1
    addi   $ra,$ra,8
    jr     $ra

在这个 MIPS 代码中,.word 部分有什么作用?我的模拟器无法处理文本部分中的数据指令。指令的作用是什么?使用 .word -2 和 .word -6 而不是 .word -8 有什么意义?

4

1 回答 1

4

该指令将字值 -2 和 -6 放入代码流中。如果你查看这个块的实际二进制表示,你会发现,在指令编码的中间,一些 FFFFFFFEFFFFFFFA 或 FEFFFFFFFFFFFFFF 取决于字节序。

汇编器将分别发出 2 个字的值为 -2 和 -6 的数据,与 -8 的单个字完全不同。

如果您查看 AVG: 标签上的内容,您会注意到它使用

lw $v0, ($ra)
lw $t3, 4($ra)

这会从返回地址(即您从哪里跳转,即从嵌入在代码段中的数据)加载寄存器 v0 和 t3 中的 2 个字。所以... v0 在其中得到 -2,而 t3 得到 -6。还要注意代码段如何在返回之前将 8 添加到 $ra 以跳过嵌入的数据。

简而言之,它是一种编码常量值的方法,作为代码流的一部分加载到寄存器中。

Now, what the code then does is add the 2 together, shift right, before returning (I assume implementing Average). It does not make much sense in this specific case to do that much work, when you could simply directly compute the average at compile time (or if you write asm directly, in your head). I assume AVG is supposed to be called from many places, but even then, since it expects its values from the code segment (usually readonly), I fail to see the point to compute math on constant values.

于 2010-03-04T11:20:03.700 回答