dor-b
dor-b

Reputation: 75

Encode MIPS BEQ instruction to Hex machine code manually

I'm trying to translate the following command to Hex:

beq $s1,$t3,label

It's also given that the command address is 0x1500, and the label address is 0x1000.

So far i know that beq equals 4(hex) and the binary values of the registers.

I know that at first I need to convert to binary and then to Hex, but i can't understand what to do with the label address. Do i need to divide it by 4 to get the value?

Upvotes: 1

Views: 1423

Answers (1)

gusbro
gusbro

Reputation: 22585

BEQ opcode is 000100 (binary). The instruction format for BEQ is:

OpCode|SR|DR|Offset

where

  • OpCode(6 bits) is 000100
  • SR(5 bits) is 10001 for $s1
  • DR(5bits) is 01011 for $t3
  • offset(16 bits) is a 16 bits signed offset(shifted 2 times) assuming starting PC is the following instruction after the branch, should be (0x1000 - 0x1504)>>2 = -0x141, which written in A2 compliment is 1111111010111111

You can now concatenate the bit fields and write them in hexadecimal if you wish:

0001 0010 0010 1011 1111 1110 1011 1111 which is 0x122BFEBF

[edit: added explanation of how to compute the offset]

To compute the offset you have to subtract the value of PC+4 (where PC stands for the address of the branch instruction) and the address of the target location. Then divide that address by 4 (or shift right two times). As the offset is encoded in A2 compliment, if the result of the operation is negative you have to apply A2's compliment to get the encoded value.

Upvotes: 1

Related Questions