top decor
Instruction set Subroutine call (GO)


To provide subroutine call, processor additionally to ordinary jump must remember the address to return (on purpose to continue the program after subroutine execution). This return address is the address of the instruction, that follows subroutine call command. For MMIX, where instructions are 4-byte long, this address can be calculated as A0 + 4, where A0 is subroutine call instruction's address.

The way processor stores the return address may be different. Even in MMIX there are two methods to do it. We'll consider the traditional (Knuth called it "old-fashioned") way of subroutine call here.

Two modification of instruction GO is used for this aim: GO with code 9E and GOI (9F). They have the same formats as instructions of data exchange with memory (see formats 1 and 2). Accordingly to this, GO instruction provides jump to subroutine with address equal to $Y + $Z (sum of two registers) and GOI - to $Y + Z (register + constant). The return address in both cases is saved into $X.

You must necessarily note, that GO is absolute jump instruction. It means that the address to go is not added to current program counter (as it was in the case of relative branches) but is used directly.

To return from subroutine you may use the same insrtuction GO. For example: you have called the subroutine by means of GOI $R, $A, 0 instruction (so the return address have being saved into register $R); then it's enough to write GOI $R, $R, 0 command in the end of the subroutine.

To applet
Examples:

  • instruction GO $1, $2, $14 will store return address to $1 and transfer control to address $2 + $14
  • GOI $1, $2, 14 - all the same as previous, but the subroutine address is $2 + 14; note that because MMIX is a machine with alignment, summary address to go must be evenly even (multiply to 4), so the two least significant bits of the result address are ignored
  • if you have ready address of subroutine in $3 and you needn't any summation, just use GOI $1, $3, 0 command
To see how subroutine calls are used for practical purpose, see this link.


Related topics:

"MMIX basics" page
 

  (C) 2003, Evgeny Eremin. rEd-MMI project documentation