鲁巴校园论坛
公告
欢迎进入鲁巴校园论坛,网站正在进行更新修复

求调 匿名

匿名 · 2026-04-02 18:21:04 · 浏览 14 · 点赞 0
点赞 · 0
```
.globl read_matrix

.text
# ==============================================================================
# FUNCTION: Allocates memory and reads in a binary file as a matrix of integers
#
# FILE FORMAT:
# The first 8 bytes are two 4 byte ints representing the # of rows and columns
# in the matrix. Every 4 bytes afterwards is an element of the matrix in
# row-major order.
# Arguments:
# a0 (char*) is the pointer to string representing the filename
# a1 (int*) is a pointer to an integer, we will set it to the number of rows
# a2 (int*) is a pointer to an integer, we will set it to the number of columns
# Returns:
# a0 (int*) is the pointer to the matrix in memory
# Exceptions:
# - If malloc returns an error,
# this function terminates the program with error code 88.
# - If you receive an fopen error or eof,
# this function terminates the program with error code 90.
# - If you receive an fread error or eof,
# this function terminates the program with error code 91.
# - If you receive an fclose error or eof,
# this function terminates the program with error code 92.
# ==============================================================================
read_matrix:
# Prologue
addi sp,sp,-40
sw ra,0(sp)
sw s0,4(sp)
sw s1,8(sp)
sw s2,12(sp)
sw s3,16(sp)
sw s4,20(sp)
sw s5,24(sp)
sw s6,28(sp)
sw s7,32(sp)
sw s8,36(sp)
mv s0,a0
mv s1,a1
mv s2,a2
li s7,0
li s8,0

#fopen
mv a1,s0
li a2,0
jal fopen
li t0,-1
beq a0,t0,error90
mv s3,a0
#fread rows
mv a1,s3
mv a2,s1
li a3,4
jal fread
li t0,4
bne a0,t0,error91
#fread cols
mv a1,s3
mv a2,s2
li a3,4
jal fread
li t0,4
bne a0,t0,error91
#malloc
lw t1,0(s1)
lw t2,0(s2)
mul s5,t1,t2
slli s5,s5,2
mv a1,s5
jal malloc
beq a0,x0,error88
mv s4,a0
#fread matrix
mv a1,s3
mv a2,s4
mv a3,s5
jal fread
bne a0,s5,error91
#fclose
mv a1,s3
jal fclose
bne a0,x0,error92
# Return pointer to matrix
mv a0,s4
j exitReadMatrix
# Epilogue
error90:
li a1,90
jal exit2
error91:
li a1,91
jal exit2
error92:
li a1,92
jal exit2
error88:
li a1,88
jal exit2
exitReadMatrix:
lw ra,0(sp)
lw s0,4(sp)
lw s1,8(sp)
lw s2,12(sp)
lw s3,16(sp)
lw s4,20(sp)
lw s5,24(sp)
lw s6,28(sp)
lw s7,32(sp)
lw s8,36(sp)
addi sp,sp,40
ret
```

测试fclose error时报错:
```
AssertionError: Venus returned exit code 255 not 92.

Venus ran into a simulator error!
Attempting to access uninitialized memory between the stack and heap. Attempting to access '1' bytes at address '0x10008034'.
```
きみみゆら 2026-04-02 18:22:38
哥几个方便拉个群不,一个一个解决太慢了
匿名 2026-04-02 18:22:59
Markdown炸了
BBztk 2026-04-02 18:27:38
回复 @きみみゆら
1011559330 我们现在的群,也可以用这个,里面有一个拥有64G RAM 高级服务器的大佬
匿名 2026-04-02 18:40:10
<u>Hello</u>
匿名 2026-04-02 18:40:30
防XSS,聪明

登录 后参与回复。