在 MIPS 中使用 scanf

Use scanf with MIPS

这个问题与我发布的这个问题无关:Why isn't syscall working for MIPS

我正在尝试在我的 MIPS 程序集中使用 scanf 从命令行读取用户输入。

这是我的代码:

    .option pic0

    .rdata # read-only data

    .align  2

fromatInt:
    .asciz "%d"
    .asciz "%c"

    .align 2

resultFormat:
    .asciz  "(%d/%u)\n"

    .text
    .align  2

scanFormat:
    .word 1

    .text
    .align 2


        .global print

print:

    addiu $sp, $sp, -4  
    sw    $ra, ($sp)    

    move $a1, $a0
    la   $a0, resultFormat
    jal  printf
    move $a0, $a1

    lw    $ra, ($sp)    
    addiu $sp, $sp, +4  

    jr    $ra

    .global main

main:

    la    $a0, fromatInt
    la    $a1, scanFormat
    jal   scanf
    lw    $s0, scanFormat

    move  $a0, $s0
    jal   print

    jal exit

当我 运行 a.out 文件时出现段错误。而且,当我 运行 gdb 这是我单步执行时得到的结果:

Program received signal SIGSEGV, Segmentation fault.
0x77e821b0 in _IO_vfscanf_internal (s=0x77fa5bc0 <_IO_2_1_stdin_>, 
    format=<optimized out>, argptr=0x7fff6614, errp=0x0) at vfscanf.c:1826
1826    vfscanf.c: No such file or directory.

我们正在使用真正的 MIPS 处理器,否则我会使用系统调用来打印和扫描。我也不了解 linux 内核系统调用,无法有效地使用它。非常感谢任何帮助。

我无法弄清楚如何使用此方法,但您可以轻松地从堆栈中使用 space

addiu $sp, $sp, -4

add $s0, [=10=], $sp

la $a0, fromatInt
add $a1, [=10=], $s0
jal scanf
lw $a0, 0($s0)
#----------------------
#add function calls here
#----------------------
addiu $sp, $sp, +4 #deallocate