繁体   English   中英

MIPS 汇编字符串数组

[英]MIPS assembly string array

我正在尝试存储然后加载一个数组,但出现内存越界错误。 该程序将加载并运行,但每当我尝试访问数组以进行输出时,我都会收到错误消息。

我希望能够在一个数组中输入一个名称,在另一个数组中输入一个数字,以便我可以单独访问它们。 当我的指针移动时,我故意留出足够的空间,以防有更长的名称。 应该有足够的空间来分配 10 个名字,但是当我尝试调用刚刚输入的第一个名字时,我的内存超出了界限。 我假设我一开始没有正确存储输入,但是我之前以相同的方式存储了一个没有问题的字符串。 当我尝试在最后输出数字时,我也会读取错误的地址/堆栈。

.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:"

您尝试写入数据部分。 但是数据部分是只读的。 你必须在 bss 部分写,在那里你有读写权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM