1

I am trying to make programs using argc and argv in MIPS simulator SPIM. This is my code:

.data
nl: .asciiz "\n"

.globl main
.text
main:
  # print argv[0]
  lw $a0, 0($a1)
  li $v0, 4
  syscall
  # print "\n"
  li $v0, 4
  la $a0, nl
  syscall
  # exit
  li $v0, 10
  syscall

When I run

$ spim -f args.s
Loaded: /usr/share/spim/exceptions.s                                                                    
args.s

I get the correct result argv[0] (the name of the program). But if I try

$ spim -f args.s hello
Loaded: /usr/share/spim/exceptions.s
Cannot open file: `(null)'
Memory address out of bounds

I get that error. I searched everywhere but couldn't find anything. What's the correct way to pass arguments to argv[]? Changing the lw in the code to lw $a0, 4($a1) gets me the following output.

$ spim -f args.s hello
Loaded: /usr/share/spim/exceptions.s
Cannot open file: `(null)'
SHELL=/bin/zsh

I tried in the virtual console and it's the same. After printing all the environment variables changing the offset in the lw it justs prints "Memory address out of bounds".

I don't know what I am doing wrong.

  • 1
    I don't know how to do this running spim from the command line. I run QtSpim interactively, which offers a menu item "Simulator" -> "Run Parameters", where you can type the rest of the command line for the simulated program. They appear in `argc`/`argv` (following the program name in `argv[0]`) as you'd expect. – Erik Eidt May 04 '22 at 16:34
  • [Entering in Command Line Arguments MIPS](https://stackoverflow.com/a/35997741) is a *possible* duplicate. The question looks like an install error, but the answer is answering the title (not body) and suggests that the first arg goes as `argv[0]` (traditionally the program name), not `argv[1]` (the first actual arg). No idea if it's true. – Peter Cordes May 04 '22 at 17:34
  • I saw that question, but it's not the same as this one. Maybe I am doing it correctly but something is wrong with my installation. I'm using Arch Linux and installed SPIM from the AUR. – Santiago Trini May 04 '22 at 17:48

1 Answers1

2

Finally solved it. The right way to use program arguments is indeed

$ spim -f code.s arg1 arg2 etc

Turns out I was wrong when I said that I installed it from the AUR, I compiled it from source using the last snapshot on the official Sourceforge repo. There is a bug in spim.cpp. After line 278 it should say break; and it works flawlessly. I already created a ticket in Sourceforge so they can patch it.