MIPS assembly programming Q&A This document was produced to answer some of the (repeated) questions about programming in MIPS assembly language and using the SPIM simulators. Q: How do I input and output numbers? A: The spim/xspim simulator provides a simple I/O facility through the syscall instruction. Section 1.5 in the documentation explains how to use syscall to do I/O. Q: My program works correctly. However at the end of a run it gives me an "Attempt to execute non-instruction at 0x00400090" . Because my loop ends the program, I assume that there must be some code that tells spim to end the run. How do I exit the program? A: There are two ways to exit a program: 1. use the syscall command to exit the program (see section 1.5) 2. Save the content of $ra register and use " jr $ra" to exit. When you enter your program $ra holds the return address for the operating system code that initiated your program. In effect running your program looks like a procedure call to the OS. Returning from that procedure is equivalent to exiting your program. Q: I get what looks like an endless loop with a whole bunch of statements like these: Exception 7 [Bad address in data/stack read] occurred and ignored Exception 4 [Unaligned address in inst/data fetch] occurred and ignored Exception 5 [Unaligned address in store] occurred and ignored What does it mean? A: > Exception 7 [Bad address in data/stack read] occurred and ignored It means that your program is has a load instruction with a bad address. That address is not inside the data or stack segment of the address space. Look at your loads and see which instruction does not use a good address. I suggest using breakpoints or single step execution, looking at all load/store instructions checking what addresses your programs is using. > Exception 4 [Unaligned address in inst/data fetch] occurred and ignored >Exception 5 [Unaligned address in store] occurred and ignored This means that your load/store instructions are loading a word but the byte address is not on a word boundary. Remember! When loading a word, the lower order two bits must be 0 (The address must be divisible by 4!) Look at your code, you must be incrementing the address by 1 instead of 4. Also use the ".align 2 " directive to the assembler. Q: What is wrong with : " lw $t2, 0x100000100($t1)" A: You are using a 32 bit number in the immediate field. This does not work since the immediate field is only 16-bit long!!! I am not sure what exactly happens here, but it can't be right! Load the address into a register first and then increment the register to address consecutive words that is, use: la $t1 0x100000100 to load the start address into register $t1. Increment register $t1 by 4(!) to address the next word. Q: I have finished and submitted my program, called Fibonachi, and it works. However, After I have loaded and run the program once in xspim then xspim doesn't seem to run the program correctly a second time. I do not know why this would occur and I checked over my program and found no errors, but I was wondering why this would occur. The program runs fine if xspim is quit and restarted and then the program is loaded once again. A: The load "file" command in the spim simulator documentation says: " If the file has already been read into SPIM, the system should be cleared befor loading (see reinitialize, below) or global symbols will be multiply defined." I suspect that you loaded the file twice and have multiply defined symbols.