Question

In: Computer Science

Write a MIPS assembly language program that implements the following pseudo-code operation: result = x +...

Write a MIPS assembly language program that implements the following pseudo-code operation:

result = x + y – z + A[j]

x and y should be in reserved memory words using the .word directive and labeled as x and y. Initialize x=10 and y=200.

Read in z from the console. Input the value -8. This is the value for z, not for –z. Store this value in memory with the label z. To begin, you could just initialize z to a value using .word directive and test your code.

Read in j from the console. The value of j should be in the range [0, 3]. Input the value 2. You do not need to check j for the proper range on input.

Reserve 4 words in memory for A with the label A. Assume A is referenced starting at A[0]. Initialize these 4 words to: 4, 9, 15, 20. E.g.,:

   A: .word 4, 9, 15, 20

Which number is A[j]? In order to get an element, you will need the load address instruction, which loads the address of a label into a register. E.g., to load the starting address of label A, A[0], into $t0, you would do the following:

           la $t0, A

You can then use $t0 as an address in the normal way for an lw or sw instruction, as needed.

Store the final result in a reserved memory word labeled result.

Your final data section might look like this:

         .data

x: .word 10

y: .word 200

z: .word 0

A: .word 4, 9, 15, 20

result: .word 0

prf: .asciiz "The result is: " #String for printing

crlf: .asciiz "\n" #For printing a cr/lf

Output the result to the console prefixed by a string that reads (without the quotes):

"The result is: "

The result should follow this string on the same line. E.g., the output to the console might be:

The result is: 5768

Check your data segment to be sure values are stored appropriately. Only the memory locations for z and result should be changed from initial values.

Solutions

Expert Solution

MIPS PROGRAM :-

   .data
x: .word 10
y: .word 200
z: .word 0
A: .word 4, 9, 15, 20
result: .word 0
prf: .asciiz "The result is: " #String for printing
crlf: .asciiz "\n" #For printing a cr/lf

.text
li $v0,5
syscall
move $t1,$v0       #reading value of z from console
la $t0,z
sw $t1,0($t0)       #storing the input value in z memory space

li $v0,5
syscall
move $t1,$v0       #reading value of j from console
sll $t1,$t1,#2       #calculating address offset according to index
la $t0,A           #loading starting address of A
add $t2, $t0, $t1   #calculating address of A[j]

lw $t3, 0($t2)       #A[j]

li $t1, 0

la $t0, A
lw $t2, 0($t0)       #A

add $t1, $t1, $t2   #result_val = A

la $t0, B
lw $t2, 0($t0)       #B

add $t1, $t1, $t2   #result_val = A + B

la $t0, Z
lw $t2, 0($t0)       #Z

sub $t1, $t1, $t2   #result_val = A + B - Z

add $t1, $t1, $t3 #result_val = A + B - Z + A[j]

la $t0, result      
sw $t1, 0($t0)       #storing the resultant value in result memory space

li $v0,4
la $a0,result
syscall

li $v0,1
move $a0,$t1
syscall

li $v0,10           #exit
syscall


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 }
Translate following pseudo-code to MIPS assembly language cout << “\n Please input a number for $s0”;...
Translate following pseudo-code to MIPS assembly language cout << “\n Please input a number for $s0”; cin >> $s0; cout << “\n Please input a number for $s1”; cin >> $s1; cout << “\n Please input a number for $s2”; cin >> $s2; $t0 = $s0 / 8 - 2 * $s1 + $s2; cout << “\n the Value of the expression “$s0 / 8 - 2 * $s1 + $s2” is ”; cout >> $t0; return;
Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
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...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13. I have another file where I save my vectors called, "vars.c" here I save: int a[] = { 14, 65, 9} int b[] =...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that sums up all the numeric characters present in a phrase ("string" that follows the "C" language convention). By For example, if the phrase is "Today is the 28th of month 9", the subroutine must perform the following sum: 2 + 8 + 9 = 19. The subroutine must receive the address of the first character of the corresponding phrase in the "stack", and return...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that sums up all the numeric characters present in a sentence (“string” that follows the “C” language convention). For example, if the phrase is "Today is the 21st of month 5", the subroutine must perform the following sum: 2 + 1 + 5 = 8. The subroutine must receive the address of the first character of the corresponding phrase in the " stack ”, and...
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT