# How can I translate this mips code into binary/hexadecimal

• John_Brown
In summary, the conversation is about converting a MIPS instruction into hexadecimal code and understanding the use of Imm16 (immediate 16-bit address) in branch instructions. The solution for Exercise 2 is given as an example, with a detailed explanation of how the Imm16 value is calculated. Similar doubts are raised for Exercise 3, with further clarification provided.
John_Brown
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?

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!

## 1. How do I convert MIPS code into binary?

To convert MIPS code into binary, you need to first understand the MIPS instruction set and how it is represented in binary. Each MIPS instruction is made up of six fields, each representing different parts of the instruction. By understanding the binary representation of each field, you can convert MIPS code into binary by breaking down each instruction into its respective fields and translating them into binary.

## 2. Can I use an online converter to translate MIPS code into hexadecimal?

Yes, there are several online converters available that can help you translate MIPS code into hexadecimal. These converters typically require you to input your MIPS code and then provide the corresponding hexadecimal translation. However, it is important to understand the underlying process of converting MIPS code into hexadecimal to ensure accuracy.

## 3. Is there a specific formula for translating MIPS code into binary or hexadecimal?

There is no specific formula for translating MIPS code into binary or hexadecimal. It involves an understanding of the MIPS instruction set and how it is represented in binary and hexadecimal. With practice and familiarity, you will be able to recognize and convert MIPS instructions into their binary or hexadecimal equivalents.

## 4. What is the importance of translating MIPS code into binary or hexadecimal?

Translating MIPS code into binary or hexadecimal is essential for understanding and working with low-level programming languages. It allows you to directly manipulate the machine code and understand how the instructions are executed by the processor. This knowledge is crucial for developing efficient and optimized programs.

## 5. Are there any tips for efficiently translating MIPS code into binary or hexadecimal?

One tip for efficiently translating MIPS code into binary or hexadecimal is to break down the instructions into their respective fields and convert each field separately. This can help in avoiding errors and ensuring accuracy. Additionally, familiarizing yourself with the MIPS instruction set and practicing regularly can improve your speed and accuracy in translating MIPS code into binary or hexadecimal.

• Engineering and Comp Sci Homework Help
Replies
5
Views
10K