Question

In: Computer Science

Translate the following C code to MIPS assembly. int a = 1; int b = 2;...

  1. Translate the following C code to MIPS assembly.

int a = 1;

int b = 2;

if (a<b)

          a=a+1;

b = b + a;

printf("The value of b is: %d", b);

  1. Translate the following C code to MIPS assembly.

int a = 2;

int b = 2;

if (a<b)

          a=a+1;

else

          a=a-1;

b = b + a;

printf("The value of b is: %d", b);

Solutions

Expert Solution

Answer:

Note: I am writing the given code in main() function and also included #include <stdio.h> header file for proper compilation.

1)

.LC0:

.ascii "The value of b is: %d\000"

main:

daddiu $sp,$sp,-48

sd $31,40($sp)

sd $fp,32($sp)

sd $28,24($sp)

move $fp,$sp

lui $28,%hi(%neg(%gp_rel(main)))

daddu $28,$28,$25

daddiu $28,$28,%lo(%neg(%gp_rel(main)))

li $2,1 # 0x1

sw $2,0($fp)

li $2,2 # 0x2

sw $2,4($fp)

lw $3,0($fp)

lw $2,4($fp)

slt $2,$3,$2

beq $2,$0,.L2

nop

lw $2,0($fp)

addiu $2,$2,1

sw $2,0($fp)

.L2:

lw $3,4($fp)

lw $2,0($fp)

addu $2,$3,$2

sw $2,4($fp)

lw $2,4($fp)

move $5,$2

ld $2,%got_page(.LC0)($28)

daddiu $4,$2,%got_ofst(.LC0)

ld $2,%call16(printf)($28)

move $25,$2

1: jalr $25

nop

move $2,$0

move $sp,$fp

ld $31,40($sp)

ld $fp,32($sp)

ld $28,24($sp)

daddiu $sp,$sp,48

j $31

nop

2)

.LC0:

.ascii "The value of b is: %d\000"

main:

daddiu $sp,$sp,-48

sd $31,40($sp)

sd $fp,32($sp)

sd $28,24($sp)

move $fp,$sp

lui $28,%hi(%neg(%gp_rel(main)))

daddu $28,$28,$25

daddiu $28,$28,%lo(%neg(%gp_rel(main)))

li $2,2 # 0x2

sw $2,0($fp)

li $2,2 # 0x2

sw $2,4($fp)

lw $3,0($fp)

lw $2,4($fp)

slt $2,$3,$2

beq $2,$0,.L2

nop

lw $2,0($fp)

addiu $2,$2,1

sw $2,0($fp)

b .L3

nop

.L2:

lw $2,0($fp)

addiu $2,$2,-1

sw $2,0($fp)

.L3:

lw $3,4($fp)

lw $2,0($fp)

addu $2,$3,$2

sw $2,4($fp)

lw $2,4($fp)

move $5,$2

ld $2,%got_page(.LC0)($28)

daddiu $4,$2,%got_ofst(.LC0)

ld $2,%call16(printf)($28)

move $25,$2

1: jalr $25

nop

move $2,$0

move $sp,$fp

ld $31,40($sp)

ld $fp,32($sp)

ld $28,24($sp)

daddiu $sp,$sp,48

j $31

nop


Related Solutions

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...
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
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);
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++;
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
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...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
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