Question

In: Computer Science

Write a RARS assembly language program to solve the following: For a set of integers stored...

Write a RARS assembly language program to solve the following:

For a set of integers stored in an array, calculate the sum of the positive numbers and the sum of the negative numbers, storing the results into memory.

In the data segment of the program, define an array consisting of 25 integers (words) that are a mix of positive and negative values. You can select any values you wish but try to create a balance of positive and negative numbers. Also create data definitions for the two sums that you will calculate in your program. These values should also be defined as words and initialized to zero.

In the text segment, read the numbers from the array one at a time. Include in your program the necessary logic to correctly add the values to the appropriate sum. After all the values have been read and the sums calculated, store the sums to memory in the locations you allocated in the data segment.

Reading the array and calculating the sums should be done within a single loop. Storing the values to memory should be done after the loop.

Solutions

Expert Solution

Greetings!!

Code:

#VARIABLE AND MESSAGES

.data

array: .word 0:25

prompt: .ascii "Enter a positive or negative number \n"

sum_pos: .word 0

possum: .ascii "\nSum of positive numbers: "

sum_neg: .word 0

negsum: .ascii "\nSum of negative numbers: "

#CODE SEGMENT

.text

#MAIN STARTS HERE

main:

#DISPLAY PROMPT MESSAGE

la x10,prompt             #load the address of prompt message

li x17,4                        #parameter for display message

ecall                            #display system call

#INITIALIZATION

la x6,array                   #load the address of the array into x6

li x30,25                      #load count into x30

#PROCESSING….

loop:

beq x30,x0,done         #check loop count is 0 or not. If 0 then goto done

#READ NUMBER FROM THE USER

li x17,5                        #paramter for reading integer from the user            

ecall                            #read number into register 10

add x5,x0,x10             #save the number read into register x5

sw x5,0(x6)                 #store the number into the array

andi x7,x5,0x80          #check the MSB

beq x7,x0,pos              #if the MSB is 0, number is positive and goto label pos

add x28,x28,x5           #otherwise the number is negative and add it to register x28

j skip                           #skip the next line

pos:

add x29,x29,x5           #if the number is positive add it to register x29

skip:

addi x6,x6,4                #increment the array index

addi x30,x30,-1           #decrement the loopcount

j loop                           #repeat the loop

done:

#DISPLAY SUM OF POSITIVE NUMBERS MESSAGE

la x10,possum             #address of the message

li x17,4                        #paramter

ecall                            #display

$DISPLAY SUM OF POSITIVE NUMBERS

add x10,x0,x29           #load the sum into register x10

li x17,1                        #parameter for display integer

ecall                            #display

#DISPLAY SUM OF NEGATIVE NUMBERS MESSAGE

la x10,negsum            #load address

li x17,4                        #parameter

ecall

$DISPLAY SUM OF POSITIVE NUMBERS

add x10,x0,x28           #load the sum of -ve numbers to x10

li x17,1                        #parameter

ecall                            #display

#EXIT FROM THE PROGRAM

li x17,10                      #exit code into register x17

ecall

Output screenshots:

Hope this helps


Related Solutions

Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
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.
1. Write an assembly language program that prompts the user for and reads four integers (x1,...
1. Write an assembly language program that prompts the user for and reads four integers (x1, y1, x2, y2) which represent the coordinates of two points. Make sure you keep track of which number is which. 2. Treat the line between the points as the radius of a sphere and compute the surface area of the sphere. Print the output with a label, such as “The surface area of the sphere is: …”. Hint: The distance between the points is...
HCS12 Assembly Language: 1. Write a program to convert a decimal number stored at memory location...
HCS12 Assembly Language: 1. Write a program to convert a decimal number stored at memory location $1010 into a binary number. Store the result in memory location $2000
Must use AT&T x64/GNU Assembly syntax. Write an assembly language program that reads in two integers,...
Must use AT&T x64/GNU Assembly syntax. Write an assembly language program that reads in two integers, A and B, and uses them to compute the following expressions. You must use the same values for A and B throughout all three expressions. 1) A * 5 2) (A + B) - (A / B) 3) (A - B) + (A * B)
Write an assembly language program code to clear and set bit 7th and 19th in a...
Write an assembly language program code to clear and set bit 7th and 19th in a 32-bit variable called N.
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT