MIPS Assembly Code - Convert instruction code to binary

In summary: Or is there another way?In summary, MIPS assembly code can be used to write a machine code input through the SPIM simulator console. An example is provided.
  • #1
JSGhost
26
0

Homework Statement



Write a single line MIPS assembly code as an input through the SPIM simulator console and the program will output a 32 bit MIPS machine code through the console. I'm using PCSpim to write the code.

An example,

I input this instruction in the console.

addi $t0,$t0,1

and the console outputs.

00100001 00001000 00000000 00000001

I'm trying to write it for other instruction codes as well.

Homework Equations



MIPS Syscall Sheet
https://www.student.cs.uwaterloo.ca/~isg/res/mips/traps

The Attempt at a Solution



la $a0, 8 # string into $a0
syscall
move $a1, $a0, 8

I just need an example for one instruction like using addi as a guide and I can do the rest. Or point in the right direction. I get stuck after getting the input from console. Should I store the "whole" string to one address? I know there are different ways of doing it like taking each character and check it but I'd rather take the whole code through a string.
 
Physics news on Phys.org
  • #2
The link you gave won't be much help - it's just the MIPS system calls. What you need is the instruction set reference, showing how each instruction is encoded in a 32-bit number. Here's something that might be helpful - http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

Here's an example from that site.
ADDI -- Add immediate (with overflow)
Description:
Adds a register and a sign-extended immediate value and stores the result in a register

Operation:
$t = $s + imm; advance_pc (4);

Syntax:
addi $t, $s, imm

Encoding:
0010 00ss ssst tttt iiii iiii iiii iiii

Here's how you read it - going left to right, the first 6 bits are the the code for the ADDI instruction. The next 5 bits are the register you're adding the value to. The next 4 bits are the register in which the sum will be stored, and the last 12 bits are the immediate value (a constant) that gets added to the first register.

You need to take an input string, such as "addi $t0, $t0, 1" and parse it so that you can set the appropriate bits in the output string of 1s and 0s.
 
  • #3
Say for example, I put a string "addi $t0,$t0,1" in $a0. How do I put the binary form of that on the console? Do I need to make a comparison using function and define the binary form of that instruction using .asciiz?
 

1. How do I convert MIPS assembly code to binary?

To convert MIPS assembly code to binary, you need to understand the structure of a MIPS instruction and the corresponding binary representation. Each instruction is made up of 32 bits, with specific fields for the opcode, registers, and immediate values. You can use a MIPS assembly reference guide or an online MIPS assembler to help you with the conversion process.

2. What is the difference between MIPS instruction code and binary code?

MIPS instruction code is the human-readable representation of a MIPS instruction, while binary code is the machine-readable representation. MIPS instruction code uses a combination of letters, numbers, and symbols to represent the different parts of an instruction, while binary code uses only 1s and 0s. MIPS instruction code is easier for humans to understand, while binary code is what the computer actually executes.

3. Are there any tools available to help with converting MIPS instruction code to binary?

Yes, there are several tools available to help with converting MIPS instruction code to binary. Many online MIPS assemblers allow you to input assembly code and generate the corresponding binary code. Some programming editors also have built-in tools for converting assembly code to binary, such as the MIPS Assembler plugin for Visual Studio Code.

4. What are some common mistakes to avoid when converting MIPS instruction code to binary?

One common mistake to avoid is not properly understanding the structure of a MIPS instruction. Each field in an instruction has a specific size and purpose, so it's important to double-check your conversion to ensure that each field is correctly represented in the binary code. Another mistake is forgetting to include the necessary 0s and 1s in the binary code, as this can lead to incorrect instructions being executed by the computer.

5. Can I convert binary code back to MIPS instruction code?

Yes, it is possible to convert binary code back to MIPS instruction code. However, it requires a thorough understanding of the MIPS instruction set and the ability to interpret binary code. There are also online tools and programming editors that have the capability to convert binary code back to assembly code, which can be helpful for debugging purposes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
36K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top