Question

In: Computer Science

Translate the C function code below to the MIPS True Assembler Language code (machine instructions only)....

Translate the C function code below to the MIPS True Assembler Language code (machine instructions only). The function code should follow the conventions for MIPS function calls including passing parameters and returning results. Your function code must be written with the minimum number of machine instructions to be executed and without any use of MIPS pseudo-instructions.

Myfunction(unsigned int a, unsigned int b, unsigned int c) {

int i=0;
while (a > c) {

a /= b;

i++; }

return i; }

Solutions

Expert Solution

Greetings!!
Code:

.text
main:
addi $a0,$0,100       #load a into a0 for passing parameter
addi $a1,$0,4       #load b into a1 for passing parameter
addi $a2,$0,5       #load c into a2 for passing parameter
addi $a3,$0,0       #load i into a3 for passing parameter
#function call
jal Myfunction       #function call
#termination
addi $v0,$0,10
syscall
#function definition
Myfunction:
loop:
#check a>c
slt $1,$a0,$a2
bne $1,$0,done
#division a=a/b
bne $a1,$0,d
break
d:
div $a0,$a1
mflo $a0
#i++
addi $a3,$a3,1
#repeat
j loop
done:
#return result to main
add $v0,$0,$a3
jr $ra

Machine code:

20040064
20050004
20060005
20070000
0c100007
2002000a
0000000c
0086082a
14200006
14a00001
0000000d
0085001a
00002012
20e70001
08100007
00071020
03e00008

Output screenshot:

Hope this helps


Related Solutions

An assembler translates assembly language to machine language instructions. A disassembler can be used to translate...
An assembler translates assembly language to machine language instructions. A disassembler can be used to translate the other way, from machine language to assembly language. For example, our 8086 emulator shows both the machine language bytes and the disassembler output while running a program (it also shows the original assembly code). The disassembled code is usually harder to read than the original assembly language program because some things are missing or may be incorrect. Write down one of the things...
Question: In the following MIPS assembly code, translate all the instructions to their corresponding machine code...
Question: In the following MIPS assembly code, translate all the instructions to their corresponding machine code in hexadecimal format. This code is stored in the memory from address 0x2fff0004. Loop: lw $t0, 4($s0)             addi $t1, $t1, -15             sll $t1, $t1, 2             beq $t1, $s1, Exit             addi $s0, $s0, 4             j Loop Exit: …
Translate the following C code to MIPS assembly. The main function and subfunction are translated to...
Translate the following C code to MIPS assembly. The main function and subfunction are translated to two separate .asm files. Finish the assembly code segment for the above requirement. int main() { int x=2; int y=1; int z=0; z=Subfunc(x,y); printf(“Value of z is: %d”, z); } int Subfunc(int x, int y) { int t1=0; t1=x+y+100; return t1;} File 1: .data str: .asciiz "The value of z:" .text #.globl main main: addi $s0, $0,2 #x addi $s1, $0,1 #y addi $s2,...
Translate following C# code into MIPS assembly language int recursive(int x) { return (x >= 0xFF)...
Translate following C# code into MIPS assembly language int recursive(int x) { return (x >= 0xFF) ? (recursive(x - 3) + recursive(x - 2)) : ((x >= 0xF) ? recursive(x - 1) + 1 : 1);
6. What is the MIPS machine language binary code for these three instructions? lw   $t0, 1200($t1)...
6. What is the MIPS machine language binary code for these three instructions? lw   $t0, 1200($t1) #Temporary reg $t0 gets A[300] add $t0, $s2, $t0 #Temporary reg $t0 gets h+A[300] sw   $t0, 1200($t1) #Stores h+A[300]$t0 back into A[300]
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 =...
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 = 1 - In four separate instructions add 2, then 3, then 4, then 5 into register $t0 $t0 = $t0 + 2 + 3 + 4 + 5 - Copy the result from register $t0 into register $t1 2. Convert the following into MIPS assembler language: - Set register $t0 = 0 - Initialize the register $t1 = 10 - Use register $t2 as...
I have to translate C++ code into MIPS. It is expected to have an output of:...
I have to translate C++ code into MIPS. It is expected to have an output of: Value of a: 25 Value of b: 31 Value of c: 18 Value of d: 49 Here is the C++ code: I need to translate this C++ into MIPS code. #include using namespace std; int main(void) { int a = 5; int b = 6; int c = 7; int d; d = -1; if ( a < 10){ a++; }else{ a--; } d...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1,...
Translate the following C code into MIPS assembly code. Assume a in $s0, i in $s1, and base address of b[] in $s2, respectively. Note that the element’s data type in array b is integer (word). Please do not change the C code structure and please comment your MIPS code. for (i = 100; i>a; i--) {          b[i] = 16*i; b[i+1] = 16*(i+1); b[i+2] = 2*i + 16; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT