我正在尝试用 MIPS 汇编语言编写简单的程序。我想要做的是从键盘读取多个字符并将其保存到文件中。我正在使用 13 个操作码创建文件并使用 15 个操作码保存字符。我不明白:如何为 15 个操作码(第 37 行,现在硬编码)动态分配要写入 $a2 的字符数。此外,我不知道如何打印写入文件的字符数($v0 在写入文件后包含此值,第 49 行)。
现在程序抛出错误:第 49 行:0x00400078 处的运行时异常:地址超出范围 0x0000002c
这是我的代码:
.data
handle_text:
.space 100 # buffor of 100 characters, bits, bytes?
out_file:
.asciiz "file_out.txt" # out file
asklabel:
.asciiz "\Please enter string to save\n"
countlabel:
.asciiz "\Characters typed:\n"
.text
main:
la $a0, asklabel # text to print
li $v0, 4 # opcode
syscall
la $a0, handle_text # Where to put text
la $a1, handle_text # Number of characters to write
li $v0, 8 # opcode
syscall
li $v0, 13 # system call for open file
la $a0, out_file # output file name
li $a1, 1 # Open for writing (flags are 0: read, 1: write)
li $a2, 0 # mode is ignored
syscall # open a file (file descriptor returned in $v0)
move $s6, $v0 # save the file descriptor
move $a0, $s6 # file handle
la $a1, handle_text # text to print
#line 37
li $a2, 44 # TEXT LENGTH
li $v0, 15 # opcode
syscall
move $t1, $v0 # move v0 to t1 so v0 won't be overwritten
la $a0, countlabel # show text
li $v0, 4 # op code
syscall
move $a0, $t1 # place characters amount in $a0
li $v0, 4 # opcode
syscall
# ERROR. Maybe it's becouse string should be null terminated?
li $v0, 16 # system call for close file
move $a0, $s6 # file descriptor to close
syscall # close file
li $v0, 10 # close app
syscall