I’m learning to write assembly code at the moment using SPIM to simulate a MIPS32 environment. Reading some other posts here and elsewhere, it appears that some MIPS environments support an .include
directive to allow you to refer to another source code file, but it appears that SPIM does not support this directive.
I’ve managed to hack together a rough solution using Unix pipes, seen below, but it breaks down anytime I need to read any user input from stdin.
Assume I have these two files, main.asm & other.asm, where main refers to a label in other:
main.asm
.data
othmsg: .asciiz "hello from other"
hremsg: .asciiz "hello from main"
.text
.globl main
main: # save ra on s0 for later
addu $s0, $ra, $zero
la $a0, othmsg # provide message address to other in $a0
jal other # => stdout: hello from other
la $a0, hremsg
li $v0, 4
syscall # => stdout: hello from main
# return to caller
jr $s0
other.asm
.text
.globl other
other: # print string at address given in $a0
li $v0, 4
syscall
# then return to caller
jr $ra
Attempting to invoke some combination of both files in one call to spim using the -file
flag failed for the following attempts:
andrew@LAZARUS: ~/college/cs_350
$ spim -f "other.asm" "main.asm"
Loaded: /usr/share/spim/exceptions.s
The following symbols are undefined:
main
Instruction references undefined symbol at 0x00400014 [0x00400014] 0x0c000000 jal 0x00000000 [main] ; 188: jal main
andrew@LAZARUS: ~/college/cs_350
$ spim -f "main.asm" "other.asm"
Loaded: /usr/share/spim/exceptions.s
The following symbols are undefined:
other
Instruction references undefined symbol at 0x0040002c
[0x0040002c] 0x0c000000 jal 0x00000000 [other] ; 11: jal other # => stdout: hello from other
andrew@LAZARUS: ~/college/cs_350
$ spim -f "main.asm" -f "other.asm"
Loaded: /usr/share/spim/exceptions.s
The following symbols are undefined:
other
Instruction references undefined symbol at 0x0040002c
[0x0040002c] 0x0c000000 jal 0x00000000 [other] ; 11: jal other # => stdout: hello from other
andrew@LAZARUS: ~/college/cs_350
$ spim -f "other.asm" -f "main.asm"
Loaded: /usr/share/spim/exceptions.s
The following symbols are undefined:
main
Instruction references undefined symbol at 0x00400014
[0x00400014] 0x0c000000 jal 0x00000000 [main] ; 188: jal main
However, I did get the following invocation with Unix pipes to work for this trivial case:
$ ( echo ‘load “other.asm”’
echo ‘load “main.asm”’
echo ‘run’ ) | spim
hello from other
hello from main
Firstly, this seems pretty hacky and I really want to believe there’s got to be a better way. Secondly, it breaks down anytime I want to read user input from stdin using a syscall.
echo.asm
.data
given: .space 200
.text
.globl main
main: # save ra on s0 for later
addu $s0, $ra, $zero
la $a0, given
li $v0, 8
syscall # => read stdin to given
la $a0, given
li $v0, 4
syscall # => print given to stdout
# return to caller
jr $s0
Say I try to pass in the input as another echo 'some input'
command piped into spim, it errors:
andrew@LAZARUS: ~/college/cs_350/multiple-files
$ ( echo 'load "echo.asm"'
echo 'run'
echo 'this should be echoed back' ) | spim
Loaded: /usr/share/spim/exceptions.s
(spim) (spim) (spim) Unknown spim command
(spim)
I believe SPIM captures stdout/stdin in some fashion and that’s probably why I’m having this issue. Can anyone help me figure out a way around it? Alternatively, if there’s a more sane way to handle working with multiple source files in SPIM, I’d really love to hear more about that.