Question

In: Computer Science

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 swaps v[k] and v[k+1]

void swap(int v[], int k)

{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}

Solutions

Expert Solution

(1) Convert the following C language code to Assembly language code:

B[8] = A[i - j];

Suppose that variables i and j are in register $s3 and $s4 respectively. Base address of A and B arrays are assigned to $s6 and $s7 registers.

Answer:

sub $t0, $s3, $s4

sll $t0, $t0, 2

add $t0, $s6, $t0

lw $t1, 0($t0)

sw $t1, 32($s7)

Description:

sub $t0, $s3, $s4 ; subtract $s3 - $s4 and store in $t0 temporary register. $t0 = i - j

sll $t0, $t0, 2 ; shift left logical $t0 register by 2 bits. $t0 = [i-j] * 4

add $t0, $s6, $t0 ; addition of $s6 + $t0 and store in $t0 temporary register. Add array A's address.

lw $t1, 0($t0) ; load word from memory location 0($t0) to $t1 register. $t1 ←  A[i - j]

sw $t1, 32($s7) ; store word from $t1 to 32($s7) memory location. $t1 → B[8]

(2) Convert the following C language code to Assembly language code:

Procedure for swapping v[k] and v[k+1] where v, k and temp are assigned to $a0, $a1 and $t0 register respectively.

void swap(int v[], int k)

{

int temp;

temp = v[k];

v[k] = v[k+1];

v[k+1] = temp;

}

Answer:

swap: sll $t1, $a1, 2

add $t1, $a0, $t1

lw $t0, 0($t1)

lw $t2, 4($t1)

sw $t2, 0($t1)

sw $t0, 4($t1)

jr $ra

Description:

swap: sll $t1, $a1, 2 ; shift left logical $a1 register by 2 bits. $t1 = k * 4

add $t1, $a0, $t1 ; Addition of $a0 + $t1 and store in $t1 register. $t1 = v + k * 4

lw $t0, 0($t1) ; load word from memory location 0($t1) to $t0 register. $t0 = v[k]

lw $t2, 4($t1) ; load word from memory location 4($t1) to $t2 register. $t2 = v[k+1]

sw $t2, 0($t1) ; store word from $t2 to 0($t1) memory location. v[k] = $t2

sw $t0, 4($t1) ; store word from $t0 to 4($t1) memory location. v[k+1] = $t0

jr $ra ; jump register to return address in $ra. Return back to the calling routine.


Related Solutions

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...
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
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); 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);
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 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; }
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;}
4. Translate the following C code to MIPS assembly (in two separate files). Run the program...
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;} Submission file: Lab4_4a.asm and Lab4_4b.asm
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: …
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT