Write a Assembly LanguageMIPS code following Dynamic array

Write a Assembly Language(MIPS) code, following:

###########################################################

# Dynamic array

#

# Description:

# Declare two words in static memory: one to hold the base address of a dynamic array,

# the other to hold the length of a dynamic array. call allocate_array to allocate

# a dynamic array. Then store the array base address and length in static memory. call

# read_array to read all values into the array. call print_array to print all values

# from the array, then call sum_array to calculate sum of all values in the array and

# print returned sum value in main. Then call print_average subprogram which would

# calculate average (sum / count) up to 5 decimal points all using integer instructions.

#

# Note:

# Also, note that allocate_array subprogram takes as argument IN memory address that

# itself holds true address of array in memory and another memory address that itself

# holds true address of array size. However, read_array, print_array and sum_array take

# as argument IN true address of array address and true value of array size.

#

# High level design:

# allocate two static variables: array_pointer, array_size and initialize them to 0

#

# call: null <-- allocate_array(array_pointer, array_size) # allocate dynamic array

# ^ ^ # in heap using system call 9

# address of static variables

#

# call: null <-- read_array(array_pointer, array_size) # prompts and reads value

# ^ ^ # into the array

# true address of array true value of array size

#

# call: null <-- print_array(array_pointer, array_size) # prints the content of

# # the array

#

# sum <-- sum_array(array_pointer, array_size) # calculates the sum of

# # the array and returns

# # sum back to main

# print sum_p + returned sum value

#

# print average_p

#

# call: null <-- print_average(sum value, count value) # calculates average up to

# ^ ^ # 5 decimal points all

# value value # using integer instructions

#

###########################################################

# Register Usage

# $t0 Holds array sum

# $t1

# $t2

# $t3

# $t4

# $t5

# $t6

# $t7

# $t8

# $t9 temporarily

###########################################################

.data

array_pointer_p: .word 0 # holds address of dynamic array (address)

array_size_p: .word 0 # hold size of dynamic array (value)

sum_p: .asciiz \"Array sum: \"

average_p: .asciiz \"Array average: \"

newline_p: .asciiz \"\ \"

###########################################################

.text

main:

mainEnd:

li $v0, 10

syscall # Halt

###########################################################

# allocate_array subprogram

#

# Subprogram description:

# This subprogram will receive as argument IN two addresses of static variable.

# Then, it will prompt for array size and validates input to make sure array size

# is greater than zero ( > 0). Then, it will allocate a dynamic integer array

# (using system call 9) and store the address of dynamic array and array size

# into static variables (using provided references). This subprogram does not

# return anything as argument OUT.

#

###########################################################

# Arguments IN and OUT of subprogram

# $a0 Holds array pointer (address)

# $a1 Holds array size pointer (address)

# $a2

# $a3

# $v0

# $v1

# $sp

# $sp+4

# $sp+8

# $sp+12

###########################################################

# Register Usage

# $t0 Holds array pointer (address)

# $t1 Holds array size pointer (address)

# $t2 Holds array size, temporarily

###########################################################

.data

allocate_array_prompt_p: .asciiz \"Enter size of the array to allocate (size > 0): \"

allocate_array_invalid_p: .asciiz \"Array size you entered is incorrect (size > 0)!\ \"

###########################################################

.text

allocate_array:

allocate_array_end:

   jr $ra # jump back to the main

###########################################################

# read_array subprogram

#

# Subprogram description:

# This subprogram will receive as argument IN address of integer array and then

# prompts for integer and read integer for each array element (or index). This

# subprogram does not return anything as argument OUT.

#

###########################################################

# Arguments IN and OUT of subprogram

# $a0 Holds array pointer (address)

# $a1 Holds array size (value)

# $a2

# $a3

# $v0

# $v1

# $sp

# $sp+4

# $sp+8

# $sp+12

###########################################################

# Register Usage

# $t0 Holds array pointer (address)

# $t1 Holds array index

###########################################################

.data

read_array_prompt_p: .asciiz \"Enter an integer: \"

###########################################################

.text

read_array:

  

read_array_end:

jr $ra # jump back to the main

###########################################################

# print_array subprogram

#

# Subprogram description:

# This subprogram will receive as argument IN address of integer array and then

# iterates through array and prints all elements of array. This subprogram

# does not return anything as argument OUT.

#

###########################################################

# Arguments IN and OUT of subprogram

# $a0 Holds array pointer (address)

# $a1 Holds array size (value)

# $a2

# $a3

# $v0

# $v1

# $sp

# $sp+4

# $sp+8

# $sp+12

###########################################################

# Register Usage

# $t0 Holds array pointer (address)

# $t1 Holds array index

###########################################################

.data

print_array_array_p: .asciiz \"Array: \"

print_array_space_p: .asciiz \" \"

###########################################################

.text

print_array:

  

print_array_end:

jr $ra # jump back to the main

###########################################################

# sum_array subprogram

#

# Subprogram description:

# This subprogram will receive as argument IN address of integer array. then

# initializes sum to zero and then iterates through array and adds all elements

# of array to the sum. This subprogram returns sum as argument OUT.

#

###########################################################

# Arguments IN and OUT of subprogram

# $a0 Holds array pointer (address)

# $a1 Holds array size (value)

# $a2

# $a3

# $v0 return sum back to main

# $v1

# $sp

# $sp+4

# $sp+8

# $sp+12

###########################################################

# Register Usage

# $t0 Holds array pointer (address)

# $t1 Holds array index

# $t2 Holds sum value

# $t3 Holds array value at a certain index

###########################################################

.data

###########################################################

.text

sum_array:

sum_array_end:

  

jr $ra # jump back to the main

###########################################################

# print_average subprogram

#

# Subprogram description:

# This subprogram will receive as argument IN array sum and size values. The

# it will divide sum by count to calculate average up to 5 decimal points all

# using integer instructions.

#

# Note:

# divide sum by count and print quotient. Then print string: \".\" and then

# multiply remainder (of sum by count) by 100000 and then divide it by count

# and print out the quotient.

#

###########################################################

# Arguments IN and OUT of subprogram

# $a0 Holds array sum (value)

# $a1 Holds array size (value)

# $a2

# $a3

# $v0

# $v1

# $sp

# $sp+4

# $sp+8

# $sp+12

###########################################################

# Register Usage

# $t0 Holds array sum (value)

# $t1 Holds array size (value)

# $t2 Holds value 100000

# $t3 Holds 100000 * remainder of sum by count

###########################################################

.data

print_average_decimal_point_p: .asciiz \".\"

###########################################################

.text

print_average:

print_average_end:

jr $ra # jump back to the main

###########################################################

Solution

The base address of an array is passed to a function taking an array parameter (this means that the parameter points to the same array buckets as it argument, and thus through the parameter the function can modify the bucket values):

Array buckets are allocated to contiguous memory locations (addresses):

The address of bucket i is at a offset i from the base address of the array (the exact value of the address depends on the number of bytes in the type stored in each bucket):

For example, for an int array each int value is stored in 4 bytes, so the addresses of buckets might look something like this:

2-D Arrays

For Multi-dimentional Arrays specify the size of each dimension. For a statically declared 2 dimentional array:

To access individual bucket values, indicate both the row and the column index:

You can think of a 2-D array as a matrix of int values indexed by row and column index values:

Often nested loops are used to access individual bucket values:

Data layout in memory is in Row Major Order. This means that all of row 0\'s elements are first, followed by all of row 1\'s, ...

2-D Array Parameters

The same rules apply for passing a multi dimentional array argument as a 1-dimentional array argument: the parameter is passed the base address of the 2-D array (the parameter points to the argument\'s array buckets and thus can change their values)

However, only the first dimension size can be unspecified in the parameter definition: you need to indicate that the parameter is a 2-D array, and can leave the size of the first dimension unspecified (for good generic design), but the second dimension is needed by the compiler to generate the correct address:

Pointer Arithmetic

In general I recommend avoiding using pointer arithmetic to access array buckets: it is easy to make errors and very hard to debug when you do).

However, you can use a pointer to point to each bucket value and then increment the pointer to point to the next one. When incremented, a pointer points to the very next storage location of that type (the address the pointer holds is incremented by the size of the type it points to): when incremented an int pointer point points to the very next int storage address (the address 4 bytes beyond the current one); and when incremented a pointer to a char points to the very next char storage address (the address 1 byte beyond the current one).

dynamically allocated arrays

Dynamically allocated arrays are allocated on the heap at run time. The heap space can be assigned to global or local pointer variables that store the address of the allocated heap space (point to the first bucket). To dynamically allocate space, use calls to malloc passing in the total number of bytes to allocate (always use the sizeof to get the size of a specific type). A single call to malloc allocates a contiguous chunk of heap space of the passed size.

Some examples of declaration and use:

dynamically allocated 2D arrays

Dynamically declared 2D arrays can be allocated in one of two ways. For a NxM 2D array:

Depending on the method, the declaration and access methods differ.

Method 1: the memory efficient way

The Method 1 Way (single Malloc) and function parameters

The base address of an array allocated this way is a pointer to an int, so it can be passed to a function with an (int *) parameter:

Method 2: the \"can still use [r][c] syntax to access\" way

The Method 2 Way (array of arrays mallocs) and function parameters

The array argument\'s type is (int **), which is different from the Method 1 way\'s type. It therefore needs different functions for accessing array bucket values:

Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word
Write a Assembly Language(MIPS) code, following: ########################################################### # Dynamic array # # Description: # Declare two word

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site