问题描述
我有一个作业,我必须将MIPS指令转换成其十六进制机器代码.我知道如何转换add,addi,lw等指令就好了,但是当涉及到beq等指令时,我会感到困惑.我如何将这个beq转换为十六进制?
I have an assignment where I have to convert MIPS instructions into its hexadecimal machine code. I know how to convert the add, addi, lw, etc. instructions just fine, but when it gets to instructions like beq, I get confused. How would I convert this beq to hex?
0x00400108 beq $t3, $t5, NEXT
0x0040010C j END
NEXT的地址是
0x0040011C
?
我尝试过的事情:
beq操作码= 4
beq opcode = 4
$ t3 =寄存器11
$t3 = register 11
$ t5 =寄存器13
$t5 = register 13
NEXT = 0x0040011C-0x0040010C = 10(十六进制)= 16(十进制)
NEXT = 0x0040011C - 0x0040010C = 10 (hex) = 16 (decimal)
4 11 13 16 (decimal)
000100 01011 01101 0000 0000 0000 1000 (convert to binary)
0001 0001 0110 1101 0000 0000 0000 1000 (group into fours)
1 1 6 D 0 0 0 8 (hexadecimal)
但这是错误的...
推荐答案
在花了很长时间哑巴之后,我找到了正确的答案.
After spending a long time being dumb, I've found the correct answer.
摘要代码:
beq $t3, $t5, NEXT
[instruction 1]
[instruction 2]
[instruction 3]
[instruction 4]
NEXT: [instruction 5]
正如迈克尔所说,偏移量是分支指令之后的指令中的字数.指令1是beq之后的以下指令,因此从此处开始计数,直到NEXT.指令1和NEXT中有4条指令,因此beq的格式为:
As Michael said, the offset is the number of words from the instruction following the branch instruction. Instruction 1 is the following instruction after beq, so start counting from there till NEXT. There are 4 instructions from instruction 1 and NEXT, so the format for beq is now:
op | rs | rd | 16-bit constant or address
000100 | 01011 | 01101 | 0000 0000 0001 0000
其中rs是$ t3,而rd是$ t5.
Where rs is $t3 and rd is $t5.
重新组合并转换为十六进制:
Regrouped and converted into hex:
0001 | 0001 | 0110 | 1101 | 0000 0000 0001 0000
1 | 1 | 6 | D | 0 0 1 0
因此十六进制表示为116D0010.干杯.指令是单词,4条指令* 4字节= 16字节
So the hexadecimal representation is 116D0010. Cheers.Instructions are Words, 4 Instructions * 4 Bytes = 16 bytes
这篇关于MIPS:将BEQ计算为十六进制机器代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!