i am trying to find a way to delay my program for a couple of seconds in MIPS. I have seen some answers regarding syscalls in MARS but nothing works in SPIM. Any way to delay in SPIM ? thanks in advance.
edit : im looking for a more convenient and smart approach, not like "write code that loops one million times"
Real OSes have sleep
system calls. e.g. POSIX nanosleep(2)
where user-space passes pointers to structs that say how long to sleep for, and that the kernel fills in with how long it actually slept if you wake up early. If you were writing code for Linux MIPS instead of SPIM, you could use that.
But SPIM is a "toy" system without any kind of sleep system call, or even a configurable timeout while waiting for something else. Your only option is a stupid delay loop, where the timing depends on how fast the host computer is.
MARS (the other major MIPS simulator) has an extended set of system calls beyond what SPIM provides that includes a millisecond-resolution sleep
: $v0 = 32
, $a0 = millis
.
MARS also has a time
system call, so you could write a spin-loop that keeps checking the time to see if you've passed your desired wake-up time. You could potentially use this (after a sleep
to get close) to get sub-millisecond delay precision. (SPIM lacks this as well, so a SPIM delay loop has to be purely dead-reckoning or externally "calibrated".)