How can I translate this mips code into binary/hexadecimal

  • Thread starter Thread starter John_Brown
  • Start date Start date
  • Tags Tags
    Code Mips
Click For Summary
SUMMARY

This discussion focuses on converting MIPS assembly instructions into hexadecimal format, specifically the branch not equal (bne) instruction. The examples provided include the instruction "bne $t2, $zero, previous instruction," which translates to 0x1540 FFFE, and "bne $t2, $zero, current instruction," resulting in 0x1540 FFFF. The immediate value (Imm16) for both cases is derived from the program counter's offset, with the previous instruction being 8 bytes lower in memory, resulting in an immediate value of 0xFFFE for the first case and 0xFFFF for the second. The discussion clarifies the significance of immediate values in branch instructions.

PREREQUISITES
  • Understanding of MIPS assembly language syntax
  • Familiarity with hexadecimal and binary number systems
  • Knowledge of MIPS instruction formats and encoding
  • Concept of program counter (PC) in assembly language
NEXT STEPS
  • Study MIPS instruction encoding techniques
  • Learn about the MIPS program counter and its role in branching
  • Explore the differences between immediate and indirect addressing in MIPS
  • Practice converting various MIPS instructions to hexadecimal format
USEFUL FOR

Students learning MIPS assembly language, software engineers working with low-level programming, and anyone interested in understanding instruction encoding and branching in MIPS architecture.

John_Brown
Messages
2
Reaction score
0
Hello

I have two doubts, In an exercise I have to convert a MIPS instruction into hexadecimal code. I understand a part of it.
The instruction given by the professor is:
Exercise 2 : bne $t2, $zero, previous instruction
The solution is
Co =0b0 00101, $rs =0b0 1010,$rt =0b0 0000, Imm16 = 0b1111 1111 1111 1110= 0xFFFE
So the whole instruction is : 0x1540 FFFE.

I understand that bne = 000101, and $t2 occupies the 10th position that $t2 is 01010. But I do not understand why Imm16 = 0b1111 1111 1111 1110= 0xFFFE
Can anybody help me?
The previous instruction (since it makes reference to the previous instruction it may be usefull) was :
Exercice 1 addi $t3 , $t2 ,-1 and the solution :
Co =0b0 01000,$rs =0b0 1010,$rt =0b0 1011,Imm16 =0b1111 1111 1111 1111
And the instruction : 0x214B FFFF
Another doubt is (more or less the same) :
I am given this :
Exercise 3: bne $t2, $zero, current instruction and the solution is :
Co =0b0 00101, $rs =0b0 1010,$rt =0b0 0000, Imm16 = 0b1111 1111 1111 1110= xFFFF So the whole isntruction is 0x1540 FFFF. What I do not understand here is the same, why does Imm16 = 0b1111 1111 1111 1110 ??

Anybody can help me?
 
Physics news on Phys.org
John_Brown said:
Hello

I have two doubts, In an exercise I have to convert a MIPS instruction into hexadecimal code. I understand a part of it.
The instruction given by the professor is:
Exercise 2 : bne $t2, $zero, previous instruction
The solution is
Co =0b0 00101, $rs =0b0 1010,$rt =0b0 0000, Imm16 = 0b1111 1111 1111 1110= 0xFFFE
So the whole instruction is : 0x1540 FFFE.
Imm16 means "immediate 16-bit address." This is in contrast to an indirect address.

For this instruction, what I believe happens is that when this instruction is read, the program counter (PC) is at the beginning of the next instruction. If the number in $t2 isn't zero, control branches to the preceding instruction, which is 8 bytes (or 2 machine words) lower in memory. If the number in $t2 is zero, control continues from the next instruction, which is the address in the PC.

The branch instructions use offsets in units of 32-bit machine words. So a branch to the previous instruction takes an offset of -2, which is 0xFFFE.
John_Brown said:
I understand that bne = 000101, and $t2 occupies the 10th position that $t2 is 01010. But I do not understand why Imm16 = 0b1111 1111 1111 1110= 0xFFFE
Can anybody help me?
The previous instruction (since it makes reference to the previous instruction it may be usefull) was :
Exercice 1 addi $t3 , $t2 ,-1 and the solution :
Co =0b0 01000,$rs =0b0 1010,$rt =0b0 1011,Imm16 =0b1111 1111 1111 1111
And the instruction : 0x214B FFFF
Here 0xFFFF is not an address - it is -1. If I recall what this does, it adds -1 to register $t2, and then copies that to $t3.
John_Brown said:
Another doubt is (more or less the same) :
I am given this :
Exercise 3: bne $t2, $zero, current instruction and the solution is :
Co =0b0 00101, $rs =0b0 1010,$rt =0b0 0000, Imm16 = 0b1111 1111 1111 1110= xFFFF
You have a mistake here. The number in binary should be 0b1111 1111 1111 1111.
John_Brown said:
So the whole isntruction is 0x1540 FFFF. What I do not understand here is the same, why does Imm16 = 0b1111 1111 1111 1110 ??
Again, no it's not. 0xFFFF = 0b1111 1111 1111 1111
John_Brown said:
Anybody can help me?
 
Thank you very much for your help!
 

Similar threads

Replies
5
Views
11K