问题
The QtSPIM MIPS assembler already shows some lines of code even though there is no program loaded, like can be seen on https://ecs-network.serv.pacific.edu/ecpe-170/tutorials/qtspim-tutorial.
I assume this is required for loading programs, but I would be very interested in an exact explanation to understand all details.
回答1:
A QtSPIM program consists of two parts: an exception handler, and a user program.
The default exception handler includes both a short user-mode startup code sequence (loaded at 0x00400000
as well as the actual exception handler located in kernel memory .ktext
, which starts at 0x80000180
,
When you load your user program, it is appended to the user-mode code (from the exception handler) at about location 0x00400024
. You can also see that until you load your program with your main
, that the address of main
(in that startup code) is taken as 0, aka undefined.
You can replace the default exception handler with your own. Use Simulator
menu, Settings
submenu, MIPS
tab, Load Exception Handler
(or Use default exception handler
).
In your exception handler file, you would put your actual handler after a .ktext
directive, and, you would put your start sequence after a .text
directive — which is optional. If you don't supply one then main
has nowhere to return to, so to terminate the simulator normally, you would have to exit via syscall
instead of simply returning.
The other popular simulator, MARS, does not include this default user mode startup code sequence so MARS programs always have to exit from main
using a syscall.
The default exception handler prints the reason for the exception and doesn't do much else.
If you want to do interrupt driven console I/O, you would write your own exception handler, for example. QtSPIM also supports a timer, which also uses the interrupt mechanism. You can also implement trap handler (teq
, tne
, teqi
, tnei
instructions), but the syscall
instruction is always handled by QtSPIM outside of the MIPS exception handling mechanism.
来源:https://stackoverflow.com/questions/59823995/qtspim-explanation-for-code-shown-without-loading-program