The CAL and RET instructions implement the actual transfer of control to and from the subroutine. There is a fundamental asymmetry here because CAL specifies a target address but RET doesn't. This is because we have to know which subroutine we are calling, but the subroutine returns to its calling place based upon where it was called. The same subroutine may be called many times from many different parts of the program, so it would be impossible to encode these in the return statement. The CSC-1 saves the return address, which is the address of the instruction directly following the CAL instruction, in the S register. (Refer back to the RTL for CAL in Chapter 8.) When the RET instruction is executed, the computer merely copies the value in S into the PC register, thereby effecting a jump to the instruction that followed the CAL. This works correctly because in the fetch/decode/execute cycle, PC is incremented after fetching the CAL instruction, so by the time CAL gets around to copying PC into S, the value in PC is not the address of the CAL instruction, but rather the one following. |