我在asm编写程序,我知道一点,但没有专家。我正在使用火星模拟器。我不明白的部分是如何创建文件并使其具有特定的扩展名,如file.vig?我想要的是创建一个文件,给出我想要的名字,写入它。到目前为止,这些都没有奏效。我有其余的程序工作,但在火星运行时没有这个文件的创建和修改。
res指向一个字符串
FileName指向一个字符串,如:file.vig
li $v0, 13 # system call for open file
la $a0, FileName # output file name
li $a1, 577 # Open for writing (flags are 0: read, 1: write)
li $a2, 0x1ff # was recommended for file permissions
syscall # open a file (file descriptor returned in $v0)
move $s6, $v0 # save the file descriptor
#写入刚刚打开的文件
li $v0, 15 # system call for write to file
move $a0, $s6 # file descriptor
la $a1, res # address of buffer from which to write
li $a2, 15 # hardcoded buffer length
syscall
move $a0, $s6
li $v0, 16 # close
syscall
li $v0, 10
syscall
答案 0 :(得分:1)
您没有检查open
系统调用的返回值。它返回-1,因为你正在加载$a1
577而不 1 [它不像libc
并且对这个值很挑剔]。
这是你的程序,稍微调整为演示这个并使用正确的选项重试打开[请原谅无偿的样式清理]:
.data
FileName: .asciiz "file.vig"
bad_msg: .asciiz "open syscall failed\n"
ok_msg: .asciiz "open was okay\n"
res: .byte 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
.text
.globl main
main:
li $a1,577 # Open for writing (flags are 0: read, 1: write)
li $a2,0x1ff # was recommended for file permissions
main_retry:
li $v0,13 # system call for open file
la $a0,FileName # output file name
syscall # open a file (descriptor returned in $v0)
move $s6,$v0 # save the file descriptor
bltz $s6,main_fail # did open fail? fly if yes
la $a0,ok_msg
li $v0,4
syscall
# Write to file just opened
li $v0,15 # system call for write to file
move $a0,$s6 # file descriptor
la $a1,res # address of buffer from which to write
li $a2,15 # hardcoded buffer length
syscall
move $a0,$s6
li $v0,16 # close
syscall
main_exit:
li $v0,10
syscall
main_fail:
la $a0,bad_msg
li $v0,4
syscall
li $a1,1 # correct write mode (O_WRONLY)
li $a2,0 # file permissions are ignored by mars
j main_retry