Question

In: Computer Science

Assemby language - MIPS Your assembly should implement the C code directly – i.e.,do not ’optimize’...

Assemby language - MIPS

Your assembly should implement the C code directly – i.e.,do not ’optimize’ the C code to change the order of operations or reduce computations. Write MIPS assembly code implementing the following C/C++ statement:

y = 13 - 11*x;

One way of doing the multiply without a multiply instruction is by using many add instructions (x+x+...+x). For this problem you should do it with fewer additions.

Hint: We know how to express any integer as a sum of powers of 2 (e.g.,7 = 1+2+4), and we also know how to multiply by powers of 2 using repeated addition. This gives us a method to multiply by 11 (applying distributivity), and this technique is in fact an underlying principle of many hardware multipliers

Thank you!

Solutions

Expert Solution

CODE :

la $t0,x($gp) // Load the value of x into register using global pointer
sll $t1,$t0,3 // Logical left shift of x and stored in t1
sll $t2,$t0,1 // Logical left shift of x and stored in t2
add $t0,$t1,$t0 // Adding t0 and t1
add $t0,$t2,$t0 // Adding t2 to t0
mul $t0,$t0,-1 // Multiplying t0 by -1
addi $t0,$t0,13 // Adding 13 to the multiplied value
la $t1,y // Loading the address of y
sw $t0, 0($t1) // Storing the value back into y

Explanation :

  1. Firstly the value of variable x is loaded into the t0 reg using global pointer.
  2. The "sll" operation stands for shift left logical which shifts the bits of current register to left. The operation of left shifting is similar to that of multiplication with powers of 2. Which means that a left shift by 1 will multiple by 21 and a left shift of 2 will multiply by 22.
  3. We can represent the value 11 as ( 8 + 2 + 1 ) = 23 + 21 + 20.
  4. So first we left shift the value in t0 (x) by 3 and store it in t1.
  5. Then we left shift the value in t0 (x) by 1 and store it in t2.
  6. Then we add all these values and store them back into t0.
  7. Multiply the value in t0 by -1 using mul instruction.
  8. Add the value 13 to the t0 reg using addi.
  9. Store the result back into variable y using sw instruction.

Doing this, we will have performed the given C code of y = 13 - 11*x; in MIPS using the least number of instructions. The key point to notice in this is the use of left shift operator to simplify the operations. This is a very useful technique used and makes the code very fast. It also reduces the number of instructions without using the "mul" operation.


Related Solutions

This is to be done with MIPS assembly language. Write MIPS code which is equivalent to...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to the follow java program: int day = (int)(Math.random() * 7); switch (day) { case 1: System.out.println(“Monday”); break case 2: System.out.println(“Tuesday”); break case 3: System.out.println(“Wednesday”); break case 4: System.out.println(“Thursday”); break case 5: System.out.println(“Friday”); break case 6: System.out.println(“Saturday”); break case 0: System.out.println(“Sunday”); break }
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer"...
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer" to calculate the square root for an-8 bit integer using The Radix-2 SRT-Redundant and Non-Redundant Algorithm to approximate square root
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two...
I'm trying to code in MIPS (MIPS Assembly Language) to calculate the hamming distance between two integers. Ideally, the program would ask for the user to type in the two integers. Then, the program would calculate the hamming distance. Afterward, it would ask if you'd like to find another hamming distance. If the user says yes, it would loop back to the beginning and ask for two new integers. Below is the code that I've created so far. Guidance with...
Write a MIPS assembly language to transpose a square integer matrix in code
Write a MIPS assembly language to transpose a square integer matrix in code
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
Use MARS to write and simulate a MIPS assembly language program to implement the strlen function....
Use MARS to write and simulate a MIPS assembly language program to implement the strlen function. The program should ask for a user's input and print the length of the user's input. Write the main function to call strlen. The main function should: - Prompt the user for input - Pass that input to strlen using registers $a0. - Preserve (i.e. push onto the stack) any T registers that the main function uses. The strlen function should: - Preserve any...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
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:" <<...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
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++;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT