汇编 MIPS - 创建文件,特定扩展名

Assembly MIPS - Create a file, specific extension

我正在用asm编写程序,我知道一点,但不是专家。我正在使用火星模拟器。我不明白的部分是如何创建文件并使其具有特定的扩展名,例如 file.vig?我想要的是创建一个文件,给它一个我想要的名字,写入它。到目前为止,None 有效。我让程序的其余部分正常工作,但在 运行 火星时间时无法创建和修改此文件。

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

您没有检查 open 系统调用中的 return 值。它是 returning -1 因为你用 577 加载 $a1not 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