0

I'm stuck at converting the below MIPS instruction to machine code

  sb $t3, 40($s2)
  beq $s0, $s1, Lab1
  j Lab1

  jr $s0

So far, I have

101000  10010       01011       101000
000100  10000       10001       0x00400000

How do I go from here? Since 0x00400000 is address not value, I don't think I translate it into binary. And so on... I can't really find an example to go on from here.

phuclv
  • 37,963
  • 15
  • 156
  • 475
user3754212
  • 31
  • 1
  • 9

3 Answers3

1

The encoding depends on the instruction type.

For relative branch like beq, the immediate is the offset so you need to specify the distance between the current instruction and the branch target. For example to skip next 3 instructions you need beq $s0, $s1, +3 which encodes as

opcode      $s0      $s1                     +3
000100    10000    10001    0000 0000 0000 0011

For absolute jump you need to make sure that the target and the current instruction's high 6 bits are the same. The immediate address is the target's low 26 bits shifted by 2. The j instruction's format is 0000 10ii iiii iiii iiii iiii iiii iiii so j 0x00400000 will be encoded as

0000 1000 0001 0000 0000 0000 0000 0000

You can read more about the encoding in this question as well as the courses here:

The instruction encoding is also available here

But why do you use both conditional branch and jump to Lab1 right beside each other? It's useless because eventually it will jump without doing anything

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

Immediate values in MIPS instructions are encoded directly as their binary representations.

Jason Baker
  • 2,471
  • 3
  • 23
  • 28
  • How can I do that when 0x00400000 is represented 010000000000000000000000 (24 bits) when in I-type it should be 16? how do I compress it? – user3754212 Jul 01 '14 at 01:13
  • 1
    If ``0x00400000`` represents the address of ``Lab1``, then you need to encode the number of instructions that you want to jump over, which I believe is just ``address of target instruction - address of branch instruction - 1``, but I might be wrong about that. However, if ``0x00400000`` already represents that jump, then it's not possible, and your code needs to be adjusted – Jason Baker Jul 01 '14 at 01:32
  • Actually, on reflection I realized that it would be possible to assemble if ``0x00400000`` was an offset, but it would require adding additional instructions to jump farther than 16 bits would normally allow – Jason Baker Jul 01 '14 at 16:20
0

Ah I think I'm getting a better idea now by looking at MIPS: Calculating BEQ into Hexadecimal Machine Code and Lưu's answer.

  beq $s0, $s1, Lab1
=>000100 10000 10001 0x00400000
=>0001 0010 0001 0001 (0x004000040 + 1 away instruction)
=>0001 0010 0001 0001 0000 0000 0000 0101
=12110001
  j Lab1
=>0000 1000 0001 0000 0000 0000 0000 0000
=08100000
  jr $s0
=>0000 0000 0000 0000 0000 0000 0000 1000
=02000008

So this is what I got. If any error please let me know.

Community
  • 1
  • 1
user3754212
  • 31
  • 1
  • 9