In: Computer Science
Loop: sll $t1, $s3, 2
Add $t1, $t1, $s6
Lw $t0, 0($t1)
Bne $t0, $s5, Exit
Addi $s3, $s3, 1
j Loop
How is this MIPS instruction set translated to machine code with the loop starting at 80000 in memory?
- MIPS instruction is byte-addressable, that means that each memory address references an 8-bit Binary Number.
Now, Let's start with the question,
Loop starts at 80000 in memory - Given
All the label needs to be inserted in Symbol Table with their addresses.
Here, Loop and Exit are labeled.
Suppose Loop Takes 8 bits and at one address, we could store one instruction.
Then, 80001 is used for SLL and so on.
Phase 1
Each address is assigned to each instruction and then Loop and Exit would be inserted in Symbol Table with their addresses.
Constant would be converted to their binary equivalent and stored in the assigned address.
OPCODE - MACHINE INSTRUCTION CODE
80000 - Loop
80001 - sll ( Opcode of SLL would be stored in the memory)
80002 - $t1 ( Value of t1 would be stored in the memory)
80003 - $s3 ( Value of s3 would be stored in the memory)
80004 - 2
80005 - Add ( Opcode of ADD would be stored in the memory)
80006 - $t1 ( Value of t1 would be stored in the memory)
80007 - $t1 ( Value of t1 would be stored in the memory)
80008 - $s6 ( Value of s6 would be stored in the memory)
80009 - Lw ( Opcode of LW would be stored in the memory)
80010 - $t0 ( Value of t0 would be stored in the memory)
80011 - 0 + $t1 ( Value of ( 0 + t1) would be stored in the memory)
80012 - Bne ( Opcode of BNE would be stored in the memory)
80013 - $t0 ( Value of t0 would be stored in the memory)
80014 - $s5 ( Value of s5 would be stored in the memory)
80015 - Exit
80016 - Addi ( Opcode of ADDI would be stored in the memory)
80017 - $s3 ( Value of s3 would be stored in the memory)
80018 - $s3 ( Value of s3 would be stored in the memory)
80019 - 1
80019 - j ( Opcode of J would be stored in the memory)
Phase 2
Each instruction would be executed line by line.
Now, it would start at 80000 from Loop, then it goes to SLL - (
SLL, ADD, etc are machine instructions So their code would be
fetched from memory and stored in the memory in Phase 1)
Then in Phase 2, Each instruction would be executed
accordingly.
For labels, the value would be fetched from the symbol table and
then executed. That's how the compiler distinguishes between labels
and instructions.
If it's in the symbol table, it's label otherwise it's an
instruction.