Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I ' m running this MIPS code and I ' m getting an error ( Memory address out of bounds ) , can someone
Im running this MIPS code and Im getting an error Memory address out of bounds" can someone help me fix this code. The code is supposed to implement a linked list to store element names from a periodic table file
enamesdat The program will then print the names in the periodic table constructed. The output
MUST be in the order the elements were read in from the file.
HERE IS THE CODE:
data
HEADERSTR: asciiz "Elements by F Last v
NEWLINE: asciiz
SPACE: asciiz
ptfname: asciiz C:pathenamesdat"
# Global variables
head: word
linebuf: space
text
globl main
main:
# Print header
li $v # syscall for print string
la $a HEADERSTR # load address of header string
syscall
# Open file
la $a ptfname
jal open
move $s $v # Store file descriptor
readloop:
# Read a line from the file
la $a linebuf
li $a
move $a $s # Pass file descriptor
jal fgetln
# Check if EOF
beq $v $zero, closefile
# Allocate memory for the string
move $a $v # Pass the address of the string
jal strdup
move $s $v # Store the pointer to the duplicated string
# Create a new node and add to the head of the list
jal getnode
j readloop
closefile:
move $a $s # Pass file descriptor
jal close
# Traverse the list and print elements
move $a $s # Pass head of the List
jal traverse
# Exit
li $v
syscall
getnode:
# Allocate memory for the node
li $a
jal malloc
move $s $v # Store the address of the new node
# Set node.data s
sw $s$s
# Set node.next head
sw $s$s
# Update head
move $s $s
jr $ra
traverse:
# Recursive traversal
beq $a $zero, endtraverse
lw $t$a # Load next node
jal traverse # Recursive call
lw $a$a # Load data
jal print # Print data
endtraverse:
jr $ra
print:
# Print length
move $a $a # Copy address of the string
jal strlen # Get length of the string
move $a $v # Pass length to printlen
# Print :
li $v
la $a SPACE
syscall
# Print string
li $v
syscall
# Print newline
li $v
la $a NEWLINE
syscall
jr $ra
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started