Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. data ARR _ SIZE: . word 2 0 MAX _ ELEMENT _ IN _ ARR: . word 3 3 0 0 ARR: # Initialize

.data
ARR_SIZE: .word 20
MAX_ELEMENT_IN_ARR: .word 3300
ARR: # Initialize ARR array
.word 7,17,13,18,14,15,16,1,20,10,200,3300,250,12,11,9,8,19,2,3
COUNT: # Initialize COUNT array
.word 1000,1999,25,90,100,45,67,89,12,34,56,78,90,123,456,789,321,654,987,432
RESULT: # Initialize RESULT array
.space 80 # Space for 20 words (4 bytes per word, 20 words =80 bytes)
TEMP_ARR: # Temporary array to store counts
.space 13200 # Space for MAX_ELEMENT_IN_ARR words (4 bytes per word, 3300 words =13200 bytes)
.text
.globl _start
_start:
# Initialize temporary array
la t0, TEMP_ARR
li t1,0
li t2,0
li t3,0
li t4,0
li t5,0
# Load constants
la t6, ARR_SIZE
lw t6,0(t6)
la t7, MAX_ELEMENT_IN_ARR
lw t7,0(t7)
# Count occurrences of each element in ARR
la t8, ARR
la t9, COUNT
loop_count:
lw t10,0(t8) # Load element from ARR
lw t11,0(t9) # Load count for current element
addi t9, t9,4 # Move to next count
addi t8, t8,4 # Move to next element
slli t12, t10,2 # Multiply element by 4 to get byte offset
add t12, t12, t0 # Calculate address for temporary array
sw t11,0(t12) # Store count in temporary array
add t1, t1, t11 # Accumulate total count
addi t3, t3,1 # Increment loop counter
blt t3, t6, loop_count
# Compute starting index for each element in RESULT
li t3,0
la t9, TEMP_ARR
la t12, RESULT
loop_start_index:
lw t10,0(t9) # Load count
add t10, t10, t2 # Add previous count
add t10, t10, t3 # Add loop counter
sw t10,0(t12) # Store starting index in RESULT
addi t12, t12,4 # Move to next index in RESULT
add t2, t2, t10 # Update previous count
addi t3, t3,1 # Increment loop counter
blt t3, t6, loop_start_index
# Place elements from ARR into RESULT
la t8, ARR
li t3,0
loop_place_elements:
lw t10,0(t8) # Load element from ARR
la t11, COUNT
slli t12, t3,2 # Multiply loop counter by 4 to get byte offset
add t11, t11, t12 # Calculate address for count
lw t12,0(t11) # Load count
la t13, RESULT
loop_place_element:
lw t14,0(t13) # Load starting index
add t15, t14, t12 # Calculate index for placement
sw t10,0(t15) # Place element in RESULT
addi t13, t13,4 # Move to next index in RESULT
sub t12, t12,1 # Decrement count
bgez t12, loop_place_element
addi t8, t8,4 # Increment ARR address
addi t3, t3,1 # Increment loop counter
blt t3, t6, loop_place_elements
# Store RESULT back into ARR
la t8, ARR
la t9, RESULT
li t3,0
loop_store_result:
lw t10,0(t9) # Load element from RESULT
sw t10,0(t8) # Store element back into ARR
addi t8, t8,4 # Move to next element in ARR
addi t9, t9,4 # Move to next element in RESULT
addi t3, t3,1 # Increment loop counter
blt t3, t6, loop_store_result
# Exit program
li a7,10
ecall
how to convert this program.s to hex file?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions