Translating C Code to MIPS Code to Machine Language with Machine Instruction in Binary and Hex Format

The code won’t be exactly translated to MIPS code but similar code is written so the output is same as the c code.

C Code:

#include<stdio.h>

int main(){

    int a = 2;

    // Prints 20 in Hexadecimal which is equivalent to 32 in Decimal
    printf("%x\n", a << 4);

    return 0;
}

Let, $t0 represent a and $t1 represent the output value in Hex.

MIPS Code:

# This is Comment.
# $0 register is always 0.

ori $t0, $0, 2
sll $t1, $t0, 4

The first line of of MIPS code is or immediate. It can be used to load the value 2 into register $t0. Any value or’ed with another value is the same value. Remember $t0 in represents a in the C Code above.

The sll command shifts contents register $t0 by 4 bits. It is equivalent to, $t1 = $t1 << 4. 2 Shifted by 4 bits in decimal is 32 and in Hexadecimal is 20.


Code Execution in MARS:

No lines executed:
mips shifting register values no line executed
mips shifting register values no line executed
First Line executed:
mips shifting register values one line executed
mips shifting register values one line executed
Second Line executed:
mips shifting register values two lines executed
mips shifting register values two lines executed

Machine Language:

Here the first line of MIPS code ori is I-Format and the second line of code sll is R-type / Format instruction.

For the first line,
ori $t0, $0, 2
Machine Instruction in Binary:

Since ori is an I type instruction it will have 4 fields. It has opcode, rs, rt and immediate. $t0 is numbered 8, $0 register is 0. Opcode for ori is 13.

001101 00000 01000 0000000000000010
opcode rs rt immediate
6 bits 5 bits 5 bits 16 bits

In order to convert the binary to Hex format instruction make group in 4 bits.

Machine Instruction Bits in Group of four:
0011 0100 0000 1000 0000 0000 0000 0010
which in Hexadecimal is,
0x34080002

It matches the instruction in the pictures above. Look at the codes column.


Similarly for the 2nd line,
sll $t1, $t0, 4
Machine Instruction in Binary:

Since ori is an I type instruction it will have 6 fields. It has opcode, rs, rt, rd, shamt (shift amount) and funct. Here rs is not used so set to 0. $t0 to $t7 registers are numbered from 8 to 15. So $t0 is numbered 8, $t1 is 9. The Opcode for sll ( Shift Left Logical ) is 0 and funct ( function ) is also 0.

000000 00000 01000 01001 00100 000000
opcode rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

In order to convert the binary to Hex format instruction again make group in 4 bits.

Machine Instruction Bits in Group of four:
0000 0000 0000 1000 0100 1001 0000 0000
which in Hexadecimal is,
0x00084900

Which again matches the instruction in the code column shown above.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s