Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I ' m running this MIPS code and I ' m getting an error ( Memory address out of bounds ) , can someone

I'm running this MIPS code and I'm 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
enames.dat. 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
HEADER_STR: .asciiz "Elements by F. Last v0.1
"
NEWLINE: .asciiz "
"
SPACE: .asciiz ""
ptfname: .asciiz "C:path/enames.dat"
# Global variables
head: .word 0
linebuf: .space 80
.text
.globl main
main:
# Print header
li $v0,4 # syscall for print string
la $a0, HEADER_STR # load address of header string
syscall
# Open file
la $a0, ptfname
jal open
move $s0, $v0 # Store file descriptor
read_loop:
# Read a line from the file
la $a0, linebuf
li $a1,80
move $a2, $s0 # Pass file descriptor
jal fgetln
# Check if EOF
beq $v0, $zero, close_file
# Allocate memory for the string
move $a0, $v0 # Pass the address of the string
jal strdup
move $s1, $v0 # Store the pointer to the duplicated string
# Create a new node and add to the head of the list
jal getnode
j read_loop
close_file:
move $a0, $s0 # Pass file descriptor
jal close
# Traverse the list and print elements
move $a0, $s2 # Pass head of the List
jal traverse
# Exit
li $v0,10
syscall
getnode:
# Allocate memory for the node
li $a0,8
jal malloc
move $s2, $v0 # Store the address of the new node
# Set node.data = s
sw $s1,($s2)
# Set node.next = head
sw $s0,4($s2)
# Update head
move $s0, $s2
jr $ra
traverse:
# Recursive traversal
beq $a0, $zero, end_traverse
lw $t0,4($a0) # Load next node
jal traverse # Recursive call
lw $a0,($a0) # Load data
jal print # Print data
end_traverse:
jr $ra
print:
# Print length
move $a0, $a0 # Copy address of the string
jal strlen # Get length of the string
move $a1, $v0 # Pass length to print_len
# Print ':'
li $v0,4
la $a0, SPACE
syscall
# Print string
li $v0,4
syscall
# Print newline
li $v0,4
la $a0, NEWLINE
syscall
jr $ra

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Database Security Applications And Trends

Authors: Michael Gertz, Sushil Jajodia

1st Edition

1441943056, 978-1441943057

More Books

Students also viewed these Databases questions