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
.textsections. This is because the
Exitsubprogram 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
.datastatements 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
.textsegment, there should be only program text, in a .data segment, only data. Later these will also be associated with segments of memory used.
jalinstruction transfers control of the computer to the address at the label
Exit. 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.
Exitsubprogram 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 the
jr $rainstruction. This means that in this case a jump (
j) operator could have been used to transfer control to the
Exitsubprogram. To be consistent with all other subprogram, this text will always use
- 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.