这个问题与我贴出的这个问题有关: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处理器,否则我会使用syscall打印和扫描。我也不理解linux内核syscall,不足以有效地使用它。任何帮助都是非常感谢的。
发布于 2016-10-16 20:56:54
我无法理解如何使用此方法,但您可以轻松地使用堆栈中的空间。
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
https://stackoverflow.com/questions/40063226
复制相似问题