There are three parts to a subroutine invocation in assembler if it is a function: 1. setup of parameters and local variables 2. actual CAL instruction 3. handling the return value Parameters are filled in with values from the caller. There are distinct places in memory for the temporary variables that the subroutine works on. These are labeled with MONUS_ in this code. For example, MONUS_PARM1. The C program uses more natural variable names like x and y but these can't be confused with an x or y in another block of code due to C's scoping rules. However, in assembler all variables are in the same block and all labels and names are visible everywhere. Some method of distinguishing them has to be used. Modern assemblers use the run-time stack to do this but in our simple example, we take another approach where we prefix the names with the name of the subroutine they belong to. This notation, MONUS_PARM1, is similar to the familiar dot notation used in most higher level languages such as Java: MONUS.PARM1. The CASM assembler language permits you to give more than one label to the same word of memory. Hence, you could use a generic label such as MONUS_PARM1 as well as the more naturalistic MONUS_X. Here's how that would look: MONUS_PARM1: MONUS_X: NUM 0 ; both labels refer to the same word But of course, you shouldn't just label it X: because there are likely to be several X's in a program and assembler has no way of disambiguating them. You must do all the work yourself by prefixing them with their subroutine names. If the subroutine is a function, then it has a return value. Many languages put the return value in a prominent place so that it can be part of an ongoing expression computation. In the above example we put the return value in an explicit variable: MONUS_RETVAL However, it can be placed directly in the A register so that the next step would be to compute with it. For example: x = min(a,b) + y;In this statement, min(a,b) is computed and the result left in the A register, so that the next step is to add y's value to it, before storing the final expression's value into x. |