Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################################
- # Part A:
- # #Declare a static array of 10 words
- # Read values into the static array, then print the array
- #
- # Part B:
- # Read an array length from the console, then allocate
- # a dynamic array of the given length
- # Read values into the dynamic array, then print the array
- #
- # Note: Specifications for argument registers ($a0, $a1)
- # for read_array and print_array ARE DIFFERENT!
- # You MAY NOT repurpose them
- #
- ###########################################################
- # Register Usage
- # $t0 Dynamic Length (Part B)
- # $t1
- # $t2
- # $t3
- # $t4
- # $t5
- # $t6
- # $t7
- # $t8
- # $t9
- ###########################################################
- .data
- myArray: .word 0:10 #static array for part A
- aIntro_p: .asciiz "This is Part A. Please enter 10 integers:\n";
- bIntro_p: .asciiz "\n\nThis is Part B. Please enter an integer for the length of the array.\nLength: ";
- bEnter_p: .asciiz "\nEnter your integers:\n"
- End_p: .asciiz "\nThe array contains:\n";
- ###########################################################
- .text
- main:
- ### Part A ###
- # Note argument registers! Attention to detail is critical
- # Use read_array to fill the static array
- # Use print_array to print the static array
- la $a0, aIntro_p
- li $v0, 4
- syscall
- li $a0, 10 #array length IN $a0
- la $a1, myArray #since static, base address loaded as such
- jal read_array
- la $a0, End_p
- li $v0, 4
- syscall
- la $a0, myArray #base address of static
- li $a1, 10
- jal print_array
- ### Part B ###
- # Allocate a dynamic array
- # Note argument registers! Attention to detail is critical
- # Use read_array to fill the dynamic array
- # Use print_array to prin the dynamic array
- la $a0, bIntro_p
- li $v0, 4
- syscall
- li $a0, 0 #reset
- li $a1, 0 #reset
- li $v0, 5
- syscall
- move $t0, $v0 #moves length into $t0 for allocation, keeps length there
- li $v0, 9 #allocation, sets base address into $v0
- move $a0, $t0
- syscall #base address is now in $v0
- move $t1, $v0 #base now in $t1
- move $a0, $t0 #length ($t0) goes into $a0 before reading
- move $a1, $t1 #base address ($t1) goes into $a1 before reading
- jal read_array
- la $a0, End_p
- li $v0, 4
- syscall
- #move $a0, $a1 #first moves base address out of $a1 and into $a0 for the print subprogram
- #move $a1, $t1 #moves the length of the array back out of the temp and into and argument register
- #jal print_array
- li $v0, 10
- syscall # Halt
- ###########################################################
- ###########################################################
- # Subprogram Description
- # Prints an array of words
- ###########################################################
- # Arguments In and Out of subprogram
- #
- # $a0 Base address
- # $a1 Array length
- # $a2
- # $a3
- # $v0
- # $v1
- # $sp
- # $sp+4
- # $sp+8
- # $sp+12
- ###########################################################
- # Register Usage
- # $t0 Base Address
- # $t1 Length
- # $t2 Constant 4
- # $t3 Count
- # $t4
- # $t5 Current Word
- # $t6
- # $t7
- # $t8
- # $t9
- ###########################################################
- .data
- ###########################################################
- .text
- print_array:
- # Print words from the array, separated by a space
- move $t0, $a0 #load base
- move $t1, $a1 #load length
- li $t2, 4 #load constant 4
- li $t3, 0 #set count variable to zero
- #i = b + s * n
- paFor:
- bge $t3, $t1, paEndFor #checking count
- mul $t4, $t2, $t3
- add $t4,$t0, $t4
- lw $t5, 0($t4) #load the word from the array
- move $a0, $t5
- li $v0, 1 #print int from loaded
- syscall
- li $v0, 1
- move $a0, $t5
- li $v0, 11
- li $a0, 32 #ascii value for space
- syscall
- addiu $t3, $t3, 1
- b paFor
- paEndFor:
- jr $ra
- ###########################################################
- ###########################################################
- # Subprogram Description
- # Reads words into an array
- ###########################################################
- # Arguments In and Out of subprogram
- #
- # $a0 Array length
- # $a1 Base address
- # $a2
- # $a3
- # $v0
- # $v1
- # $sp
- # $sp+4
- # $sp+8
- # $sp+12
- ###########################################################
- # Register Usage
- # $t0 Length
- # $t1 Index
- # $t2
- # $t3
- # $t4
- # $t5
- # $t6
- # $t7
- # $t8
- # $t9 Count
- ###########################################################
- .data
- myBaseHolder: .word 0 #declare word to hold base
- ###########################################################
- .text
- read_array:
- # Read words from the console, store them in
- # the array until the array is full
- li $t0, 0
- li $t1, 0
- move $t0, $a0 #length
- move $t1, $a1 #base address
- li $t9, 0 #makes sure count is reset before engaging
- sw $t1, myBaseHolder #save the base address into the holder word
- rWhile:
- bge $t9, $t0, endR #branch to end if count > length
- li $v0, 5 #call for an int from console
- syscall
- sw $v0, 0($t1) #saves the word from the console into the array
- addiu $t9, $t9, 1 #count++
- addiu $t1, $t1, 4 #increments the address
- b rWhile
- endR:
- jr $ra
- ###########################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement