5.1: Exit Subprogram
- Page ID
- 27120
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
The first subprogram to be introduced will be called Exit
, and will call syscall service 10 to exit the program. Because there will be no need to return to the calling program, this subprogram can be created by creating a label Exit
, and putting code after that label which sets up the call to syscall and executes it. This is shown in the following program that prompts a user for an integer, prints it out, and exits the program.
Program 5-1: Implementing and calling Exit subprogram # File: Program5-1.asm # Author: Charles Kann # Purpose: To illustrate implementing and calling a subprogram named Exit. .text main: # read an input value from the user li $v0, 4 la $a0, prompt syscall li $v0, 5 syscall move $s0, $v0 # print the value back to the user li $v0, 4 la $a0, result syscall li $v0, 1 move $a0, $s0 syscall # call the Exit subprogram to exit jal Exit .data prompt: .asciiz "Please enter an integer: " result: .asciiz "\nYou entered: " # subprogram: Exit # author: Charles Kann # purpose: to use syscall service 10 to exit a program # input: None # output: None # side effects: The program is exited .text Exit: li $v0, 10 syscall
5.1.1 Commentary on Exit subprogram
- This program has two
.text
sections. This is because theExit
subprogram is not a part of the main, and so exists in a different part of the file. Eventually the subprogram will be put into another file and included when needed. There can be as many.text
and.data
statements in assembly language as are needed, and they can be interspersed as they are here. They inform the assembler what type of information follows. When in a.text
segment, there should be only program text, in a .data segment, only data. Later these will also be associated with segments of memory used. - The
jal
instruction transfers control of the computer to the address at the labelExit
. For the present the reader can think of this as calling or executing a method or function in a HLL.However the wording used here of transferring control is much more accurate, and this will become clear later in the chapter.
- The
Exit
subprogram halts the program so there is no return statement when it is complete. This is a special case, and all other subprograms in this text will execute a return using thejr $ra
instruction. This means that in this case a jump (j
) operator could have been used to transfer control to theExit
subprogram. To be consistent with all other subprogram, this text will always usejal
. - Programmers should always attempt to be consistent with naming conventions. This text will use the conventions of camel casing with the first word beginning with a lower case for variables and labels in a program (e.g. inputLabel), and camel casing with the first word beginning with a capital letter for subprogram (.e.g. InputInteger). There is no industry wide standard for naming conventions as with Java or C#, so the reader is free to choose their own standard. But once chosen, it should be followed.
- All subprograms should at a minimum contain the information provided in this example. The programmer should state the program name, author, purpose, inputs, outputs, and side effects of running the program. The input and output variables are especially important as they will be registers and not be named as in a HLL. It is very hard after the program is written to remember what these values represent, and not documenting them makes the subprogram very difficult to use and maintain.