程序代写 buffer: .space 50000

buffer: .space 50000

.globl read_mtx_file

Copyright By PowCoder代写 加微信 powcoder

# read_mtx_file
# a0: address to string containing filename
# a1: number of elements in matrix
# a2: allocated memory address of matrix array
# a2: allocated memory address of matrix array
read_mtx_file:
# Move arguments to save registers
mv s9, a0
mv s10, a1
mv s11, a2

###############################################################
# Open the file for reading
li a7, 1024 # system call for open file
li a1, 0 # Open for reading (flags are 0: read, 1: write)
ecall # open a file (file descriptor returned in a0)
mv s6, a0 # save the file descriptor
###############################################################
# Read from file just opened
li a7, 63 # system call to read from the file
mv a0, s6 # file descriptor
la a1, buffer # address of buffer to which we write
li a2, 50000 # hardcoded buffer length
ecall # read from file
###############################################################
# Close the file
li a7, 57 # system call for close file
mv a0, s6 # file descriptor to close
ecall # close file
###############################################################

# data from the file is read as ASCII characters, this routine converts it into integers
la s0, buffer
mv s3, s0 # store buffer address in s3
add s1, x0, x0
addi s4, s10, -1 # number of iterations – 1
add s2, x0, s4
addi s5, x0, 57 # digit 9
# for loop which iters a2 times
addi t0, x0, 10
add t1, x0, x0
addi t2, x0, 1
add t3, x0, x0 # to hold number of digits in a number
addi t4, x0, 10 # separator: ‘\n’

conv: lbu t5, 0(s0)
beq t5, t4, sep
bgt t5, s5, garbage # anything greater than digit 9 is considered as garbage
addi t6, x0, 48
sub t5, t5, t6
blt t5, x0, done
mul t1, t1, t0
add t1, t1, t5
addi s0, s0, 1
addi t3, t3, 1
jal x0, conv

addi s0, s0, 1
jal x0, conv

slli s7, s1, 2 # multiply count by 4 for word adressing
add s8, s7, s11 # add index offset to matrix base address
sw t1, 0(s8) # store the integer number
addi s3, s3, 4 # next word address for store
addi s0, s0, 1 # next byte
addi s1, s1, 1 # loop increment
ble s1, s2, loop

mv a2, s11

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com