【问题标题】:Use scanf with MIPS将 scanf 与 MIPS 一起使用
【发布时间】:2016-10-15 19:17:18
【问题描述】:

这个问题与我发布的这个问题相关: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 内核系统调用,无法有效地使用它。非常感谢任何帮助。

【问题讨论】:

    标签: assembly mips mips32


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2014-12-24
      相关资源
      最近更新 更多