Reputation: 55
I'm trying to store and then load an array and I am getting a memory out of bounds error. The program will load and run but anytime I try to access the array for output I get the error.
I want to be able to input a name into one array and a number into another so i can access them seperately. I've purposefully allowed plenty of extra space when my pointer moves in case there is a longer name. There should be enough space for 10 names allotted, but when I try to call the first name that was just input i get a memory out of bounds. I'm assuming I'm not storing the input correctly in the first place but I've stored a string without issue in the same manner before. I also get a bad address/stack read when I try to output the numbers at the end.
.text
.globl __start
start:
la $a0,startPrompt # display welcome prompt
li $v0,4
syscall
li $v0,5 # get employee counter
syscall
move $t9,$v0 # store employee count in an unused register
move $t0,$t9 # move number of employees for loop counter
la $t1,emp # store array address in t1
la $t2,tips # move tips array to t2 for use
la $t3,hours # move hours array to t3 for use
jal inputEmp # jump and link to input employee names
jal reset # jump to reset pointers
jal reload # jump to reload arrays
la $a0,crlf # skip a line
li $v0,4
syscall
#################
# Display Results
#################
dispResult:
la $a0,crlf # skip a line
li $v0,4
syscall
lw $a0,($t1) # display employee's name
li $v0,4
syscall
la $a0,disp1 # display first part of message
li $v0,4
syscall
lw $a0,($t3) # display hours worked
li $v0,1
syscall
la $a0,disp2 # display second part of message
li $v0,4
syscall
lw $t4,($t3) # load hours worked for use
mul $t8,$t4,$t7 # t8 <------ (tip point) * (hours worked)
move $a0,$t8 # display
li $v0,1
syscall
jal movePoint # move to increase pointers
bgtz $t0,dispResult # repeat loop if counter > 0
li $v0,10 # end program
syscall
#################
# Input Emp Names
#################
inputEmp:
la $a0,namePrompt # display name prompt
li $v0,4
syscall
la $a0,($t1) # get name input and store in array
li $a1,16
li $v0,8
syscall
#################
# Input Tip/Hours
#################
inputTip:
la $a0,hourPrompt1 # display first part of hour prompt
li $v0,4
syscall
lw $a0,($t1) # load first employee's name
li $v0,4
syscall
la $a0,hourPrompt2 # display second part of hour prompt
li $v0,4
syscall
li $v0,5 # get employee hour
syscall
sw $v0,($t3) # store tips in array
la $a0,tipPrompt # display tip prompt
li $v0,4
syscall
li $v0,5 # get employee tip
syscall
sw $v0,($t2)
jal movePoint # increase pointers
bgtz $t0,inputEmp # loop to inputTip is counter is above 0
jal reset # reset pointers
la $a0,crlf # skip a line
li $v0,4
syscall
#################
# Calc Total Tips
#################
calcTipTotal:
lw $t4,($t2) # load word into t4 for use
add $t6,$t6,$t4 # add array point to total tips
jal movePoint # jump and link to move pointers
beqz $t0,calcTipTotal # branch to calc point if counter is 0
jal reset # jump to reset pointers
##################
# Calc tip point
##################
div $t7,$t6,$t0 # divide total tips by employees and store in t7
jr $ra
##################
# Reset Pointers
##################
reset:
li $t1,0 # reset name array pointer
li $t2,0 # reset tip array pointer
li $t3,0 # reset hours array pointer
move $t0,$t9 # reset loop counter
jr $ra # jump to return address
##################
# Move Pointers
##################
movePoint:
addi $t1,$t1,16 # move array pointer names
add $t2,$t2,4 # move array pointer tips
add $t3,$t3,4 # move array pointer hours
sub $t0,$t0,1 # subtract one from counter
jr $ra # jump to return address
.data
hours: .space 80
tips: .space 80
emp: .space 160
startPrompt: .asciiz "Good day! How many employees worked today?\t"
namePrompt: .asciiz "Enter an employee's name: "
hourPrompt1: .asciiz "Enter "
hourPrompt2: .asciiz "'s hours: "
tipPrompt: .asciiz "\nAnd their tips: "
crlf: .asciiz "\n"
disp1: .asciiz "\t-Worked Hours: "
disp2: .asciiz "\n\t\tTip Out:"
Upvotes: 1
Views: 2347
Reputation: 1469
You try to write to the data section. But the data section is read only. You have to write in the bss section, there you have read and write access.
Upvotes: 1