0
votes

This questions is tangentially related to this question I posted: Why isn't syscall working for MIPS.

I am attempting to use scanf in my MIPS assembly to read users input from the command line.

Here is my code:

    .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

When I run the a.out file I get a segfault. And, when I run gdb this is the result I get when stepping through:

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.

We are using a real MIPS processor, otherwise I would use syscall to print and scan. I also don't understand the linux kernel syscall enough to use it effectively. Any help is much appreciated.

1

1 Answers

1
votes

I wasn't able to figure out how to use this method but you can easily use space from the stack instead

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