Chapter 9

Heap Memory


# Program that allocates heap memory to store the user input string

.text
main:
	la $a0, prompt1
	li $a1, 80	# max size of the string (amount of bytes to allocate)
	jal PromptString
	move $a0, $v0
	jal PrintString	
	
	la $a0, prompt2
	li $a1, 80
	jal PromptString
	move $a0, $v0
	jal PrintString
	
	jal Exit
.data
	prompt1: .asciiz "Enter the first string: "
	prompt2: .asciiz "Enter the second string: "

.text
# Input: $a0 = prompt to print
#        $a1 = the maximum size of the string (how much memory to allocate)
# Output: $v0 = the address of the entered string
PromptString:
	addi $sp, $sp, -12 # push stack
	sw $ra, 0($sp)     # sp[0] = $ra
	sw $a1, 4($sp)     # sp[1] = $a1
	sw $s0, 8($sp)     # sp[2] = $s0
	
	li $v0, 4	# print the prompt
	syscall		# ($a0 already has the string to print address)
	
	li $v0, 9	# 9 = heap allocation system call
	lw $a0, 4($sp)  # $a0 = sp[1] = $a1 = maximum string size
	syscall
	move $s0, $v0  # $s0 = $v0 = the result allocated address
	
	move $a0, $v0 # $a0 = $v0 = address to store read in string
	li $v0, 8 # 8 = read in string
	lw $a1, 4($sp) # $a1 = orig $a1 = sp1 = max string size
	syscall
	
	move $v0, $a0 # save string address to return
	
	lw $ra, 0($sp) # pop stack]
	lw $s0, 8($sp)
	addi $sp, $sp, 12
	jr $ra

.include "utils.asm"

Integer Array Program

Allocates an array of 3 integer words (12 bytes) then prints the entries out

# Program that allocates heap memory to store the user input string

.text
main:
	# allocate the array
	li $a0, 3	# the number of items in the array
	li $a1, 4	# the size of each item
	jal AllocateArray

	# put some numbers in the array
	move $t0, $v0   # $t0 = allocated array address
	la $a0, prompt1
	jal PrintString
	jal PromptInt
	sw $v0, 0($t0)  # $t0[0] = $v0
	jal PromptInt
	sw $v0, 4($t0)  # $t0[1] = $v0
	jal PromptInt
	sw $v0, 8($t0)	# $t0[2] = $v0

	# print the array
	move $a0, $t0   # the array base address
	li $a1, 3	# the number of int entries
	jal PrintIntArray

	
	
	jal Exit

.data
	prompt1: .asciiz "Enter an array entry: "

.text
# Input: $a0 = number of items in array
#        $a1 = the size of each item
# Output: $v0 = address of result array
AllocateArray:
	addi $sp, $sp, -4	# push stack
	sw $ra, 0($sp)
	
	# calculate the total array byte size
	# = num elements * element size
	mul $a0, $a0, $a1
	li $v0, 9
	syscall
	
	lw $ra, 0($sp)		# pop stack
	addi $sp, $sp, 4
	jr $ra
	
# Input: $a0 = base array address
#        $a1 = array size
PrintIntArray:
	addi $sp, $sp, -16	# push stack
	sw $ra, 0($sp)
	sw $s0, 4($sp)
	sw $s1, 8($sp)
	sw $s2, 12($sp)
	
	move $s0, $a0		# $s0 = array base
	move $s1, $a1		# $s1 = array size
	li $s2, 0
	
PrintIntArray_loop:
	sge $t0, $s2, $s1	# $t0 = ($s2 >= $s1 ? 1 : 0)
	bnez $t0, PrintIntArray_endloop # if reached end of array goto end
	
	sll $t0, $s2, 2		# array index *= 4, since each int is 4 bytes
	add $t0, $t0, $s0	# address of next element (s0 = array base)
	lw $a0, 0($t0)		# $a0 = array entry value
	jal PrintInt
	
	addi $s2, $s2, 1	# increase array index (index++)
	b PrintIntArray_loop

PrintIntArray_endloop:	
	lw $ra, 0($sp)		# pop stack
	lw $s0, 4($sp)
	lw $s1, 8($sp)
	lw $s2, 12($sp)
	addi $sp, $sp, 16
	jr $ra

.include "utils.asm"