How Many Saved Registers Does Mips Have
To maintain an orderly assignment of registers, the MIPS annals conventions specify which registers are preserved when a procedure is called and which ones are not. General registers $16-$23 and floating registers $f20-$f30 are specified as saved registers when a procedure is called.
In guild to preserve a register, it must exist saved earlier and and then restored after a process call. Registers which are not saved are considered temporary and their contents may be changed during the execution of a procedure.
Similar the return accost and the parameters to a procedure, the saved registers are function of the environment of a procedure. The technique for saving the return address in $ra on the system stack may be used for whatever register which must be saved.
Registers may be saved either by the calling program or by the procedure which is called. In the commencement case, the registers to be saved are pushed on the stack before the process is chosen and popped off and restored afterwards the process returns.
Suppose that registers $s0-$s2 are in apply when procedure P is called. These registers are pushed on the stack after any parameters which are passed to the procedure. Prior to the jal P teaching the stack will contain:
The MAL code to save and restore these registers in the calling program is:
. . add together $sp, -12 # save $s0-$s2 sw $s0, 12($sp) sw $s1, viii($sp) sw $s2, iv($sp) jal P # call process P lw $s2, four($sp) # restore $s0-$s2 lw $s1, 8($sp) lw $s0, 12($sp) add $sp, 12 . . P: sw $ra, ($sp) # save return address add together $sp, -4 # body of process (may alter $s0-$s2) add $sp, four lw $ra, ($sp) # restore return address jr $ra # return
Ordinarily, the calling program only saves temporary registers $t0-$t9 which are in use at the time of the process call.
The return address must always exist saved inside the chosen procedure considering the return address is not stored in $ra until the jal is executed. In the 2nd method of saving registers, if the procedure modifies any saved registers, then information technology must save and restore these registers along with $ra.
Suppose that procedure P uses registers $s0-$s2. And then these registers must be saved and restored past P as follows:
. . jal P # phone call procedure P . . P: sw $ra, ($sp) # save return address add $sp, -4 add $sp, -12 # save $s0-$s2 sw $s0, 12($sp) sw $s1, viii($sp) sw $s2, 4($sp) # trunk of procedure (changes $s0-$s2) lw $s2, 4($sp) # restore $s0-$s2 lw $s1, 8($sp) lw $s0, 12($sp) add together $sp, 12 add $sp, 4 lw $ra, ($sp) # restore return address jr $ra # return
The second method is preferred because all register saving code occurs inside the procedure. However, either method may be used equally long as the calling program and called procedure are in agreement.
In both methods, saving and restoring the agile registers adds a significant number of instructions and memory accesses. To minimize this overhead, procedures should use the temporary registers $t0-$t9 whenever possible because they do not have to be saved.
Source: https://www.cs.uaf.edu/2004/fall/cs301/notes/notes/node82.html
Posted by: congeroppers.blogspot.com
0 Response to "How Many Saved Registers Does Mips Have"
Post a Comment