Hello all, I am trying to write code for these three functions.
1. readdata: The readdata function has two parameters ($a0 and $a1) which contain the addresses of two integers. The function is to prompt and read two integers. The strings for the prompts are provided in the data segment. Once the integers are read, the function readdata must then call the function order which places the values in the ascending order. As this function calls another function, you must store $ra on the runtime stack at the beginning of the function and restore it the end. This function must access the memory ONLY through the parameters in $a0 and $a1.
2. order: The function order has two parameters ($a0 and $a1) that contain the addresses of two integers. The function must arrange the values referred to by the parameters so they are in ascending order. To do this, you must read the values from memory into the registers $s0 and $s1, compare them and save them in ascending order. As $s0 and $s1 are “saved” registers, they must be stored on the runtime stack at the beginning of the function and restored to the original values at the end. As this is a leaf function, the $ra does not need to be stored.
3. getsum: The getsum function has two parameters ($a0 and $a1). This function must determine the sum of the numbers from the first value in $a0 through the second value in $a1. For example, if the values are 5 and 8, then the sum is 26. This value must be returned using the register $v0. Do not use any saved registers in this leaf function.
Not required, but good to do: this function is easily done recursively. Give it a try!
Requirements:
1. each function must have a comment with a description of the purpose of the function
2. all instructions must be commented.
3. parameters must be passed using the registers specified
4. return value must be returned using the register specified
5. $s0 and $s1 must be saved on the stack, used and restored from the stack in the function order
6. $ra must be saved on the stack and restored from the stack in function readdata
I wrote code for the first two functions. However, when I try to run it I get this error:
line 79: Runtime exception at 0x004000ac: store address not aligned on word boundary 0x00000001. Does anybody know what this could be. My code is below:
.data
.word 0
.word 0
prompt1: .asciiz "Enter a number "
prompt2: .asciiz "Enter another number "
result: .asciiz "The sum of the numbers is "
.text
.globl main
main:
li $s0, 1 #initialize for saving registers test
li $s1, 2 #initialize for saving registers test
#note: at this point, the values of $s0 and $s1 now should be 1 and 2
#read data
lui $a0, 0x1001 #address of first integer loaded as parameter
addu $a1, $a0, 4 #address of second integer loaded as parameter
jal readdata #jump to the readdata function
#note: at this point, the values of $s0 and $s1 again should be 1 and 2 and
# the input values should be stored into memory in ascending order
#get sum
lui $t0, 0x1001 #set address of first integer
lw $a0, ($t0) #get first number
lw $a1, 4($t0) #get second number
jal getsum #call getsum function
move $s2, $v0 #get the return value
#print results
la $a0, result #get string for result
li $v0, 4 #set command to print string
syscall #print string
move $a0, $s2 #get saved value of sum
li $v0, 1 #set command to print integer
syscall #print integer
#exit
li $v0, 10 #set command to end program
syscall #end program
# Your code goes here.
readdata:
addi $sp, $sp, -4
sw $ra, ($sp)
# sw $a0, 4($sp)
# sw $a1, 8($sp)
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5
syscall
move $a0, $v0
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5
syscall
move $a1, $v0
jal order
# lw $a0, 4($sp)
# lw $a1, 8($sp)
lw $ra, ($sp)
jr $ra
order:
addi $sp, $sp, -8
sw $s0, 4($sp)
sw $s1, 8($sp)
lw $s0, ($a0)
lw $s1, ($a1)
bgt $s0, $s1, swapval
jal ret
swapval:
move $t1, $s0
move $s0, $s1
move $s1, $t1
jal ret
ret:
lw $s0, 4($sp)
lw $s1, 8($sp)
jr $ra
getsum: