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, egst 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 "pathenamesdat"
where path is the full path to enames.dat,
eg Windows: c:UsersnameDesktopcspt
Macs: Usersnamedesktopcspt
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 traversehead 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 IO:
# 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
input: space
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
break
strdupinput
head getnodes head
I while true
close file close
traversehead print
return
nodeaddress getnodecstring s address list
allocate node malloc
node.data
node.next list
return node
void printcstring
output s syscall
void traverseaddress list, address procrecursive
if list
traverselistnext
proclistdatajalr instruction
r
Damuirant
CONTENT OF FILEIO.S:
# fileio.s file io 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
# a: fd file descriptor
# a: buf
# return:
# v: number of characters read, error, eof
#
globl fgetln
fgetln: move $t $a # save a
li $a # byte at a time
do: li $v
syscall
blez $veof # eof or error
lb $t$a
addiu $a $a
bne $t
do
eof: sub $v $a $t # number of characters read
beqz $vendif
sb $zero, $a # null byte
endif: move $a $t # restore a
jr $ra
# int fd opencstring filename, int mode
#
# parameter:
# a: file name
# a: rdonly, wronly, rdwr
# return:
# v: fd file descriptor if error
#
globl open
open: li $v
syscall
jr $ra
# void fcloseint fd close file
#
# parameter:
# a: fd
#
globl close
close:
li $v
syscall
jr $ra
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
