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,...
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 function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0 and initially containing 0 . Also assume base of array Arr is in $s3 while ( k < = 10 ) { Arr[k] = k ; k = k + 1; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT