Question: Write in MIPS ASSEMBLY : ( plz note that enames.dat only contains the names and nothing more ) Description: Write a program that implements a

Write in MIPS ASSEMBLY :
(plz note that enames.dat only contains the names and nothing more)
Description:
Write a program that implements 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, eg.1st element printed first.
All nodes of the link list and string are to be allocated on the heap.
Nodes are to be added to the head of the list.
The input file must be declared in main with the label ptfname in a single line as:
ptfname: .ascitiz "path/enames.dat"
where path is the full path to enames.dat,
eg. Windows: c:/Users/name/Desktop/cs2640/pt
Mac0s: /Users/name/desktop/cs2640/pt
address getnode (address data, address next): returns an address to a new node
initialized with data and next
void traverse (address list, address proc) : traverses the list and calls proc passing
the data of the node visit, MUST use recursion to traverse from the last node to the first node.
main:
open and read lines from the file enames.dat
creates a link list of these lines head, the lines are to be created using strdup
call traverse(head, print) to output the lines - this must output the names in the order read in
void print (cstring source) : output source to the console using format: #$ where $ is the
string and # is the length of the string.
Procedures needed from the last project: strdup, strlen, malloc
Procedure open, close, fgetln are given in the file fileio.s
Required I/O:
# is the number of elements read from the file, the number before the element name is the length of
the name. F. Last is your name.
Hints:
head: .word 0
input: space 80
main:
open file enames.dat (open)
dol
get a line of input from the file (fgetln)
if no characters read from the file (return value from fgetln is 0)
break
s= strdup(input)
head = getnode(s, head)
I while (true)
close file (close)
traverse(head, print)
return
node-address getnode(cstring s, address list)
allocate node (malloc)
node.data =s
node.next = list
return node
void print(cstring s)
output s (syscall 4)
void traverse(address list, address proc)--recursive
if (list !=0)1
traverse(list.next)
proc(list.data)--jalr instruction
r
Damuirant 1?.
CONTENT OF FILEIO.S:
# fileio.s - file i/o library
# void fgetln (int fd, cstring buf)- get a line of text from a file into buf, full path
# *no* buffer overflow check
# (
included, null terminated)
# parameters
# a0: fd (file descriptor)
# a1: buf
# return:
# v0: number of characters read, -1->error, 0->eof
#
.globl fgetln
fgetln: move $t0, $a1 # save a1
li $a2,1 # 1 byte at a time
_do: li $v0,14
syscall
blez $v0,_eof # eof or error
lb $t1,($a1)
addiu $a1, $a1,1
bne $t1,'
',_do
_eof: sub $v0, $a1, $t0 # number of characters read
beqz $v0,_endif
sb $zero, ($a1) # null byte
_endif: move $a1, $t0 # restore a1
jr $ra
# int fd = open(cstring filename, int mode)
#
# parameter:
# a0: file name
# a1: 0->rdonly, 1->wronly, 2->rdwr
# return:
# v0: fd (file descriptor),-1 if error
#
.globl open
open: li $v0,13
syscall
jr $ra
# void fclose(int fd)- close file
#
# parameter:
# a0: fd
#
.globl close
close:
li $v0,16
syscall
jr $ra
Write in MIPS ASSEMBLY : ( plz note that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!