;----------------------------------------------------------------- ; EX73.ASM PROCEDURE CALL TO void DELAY(unsigned *PTR) ; Parameter Paasing via a Pointer to a Variable ; ; C.P. Diduch 2001 ; ; The main program toggles the state of an LED every T ms. The ; procedure, void DELAY(unsigned *PTR), is called to implement ; a software delay of T milliseconds. The parameter, PTR, is passed ; as a pointer to (or address of) T, in register R0. ; ; Author(s): ; Signature(s): Date: ;----------------------------------------------------------------- ; PROGRAM CONSTANTS ; .EQU TS 100 ; #define TS 100 ; ; ; STACK and VARIABLES ; ; ; .ORG 0x0000 ; "assign following to RAM" .DS 5 ; "reserve space for stack" TOS: .DS 1 ; ; T: .DS 1 ; unsigned T ; ;----------------------------------------------------------------- ; MAIN PROGRAM ; void main(void) { ; .ORG 0x8000 ; "assign the following to ROM" ; ADDL SP, NULL, TOS ; "initialize stack pointer" XOR R2, R2, R2 ; X = 0 ; // Reg. var. in R2. ; ; do { M0: STOREP 0x0002, R2 ; outport(0x0002, X) ; ADDL R0, NULL, 1000 ; STOREM T, R0 ; T = 1000 ; ADDL R0, NULL, T ; DELAY(&T) ; // &T denotes CALL DELAY, RA ; // the address of T. XORL R2, R2, 0x0001 ; X = X ^ 0x0001 ; JMP M0 ; } ; } ;----------------------------------------------------------------- ; PROCEDURE void DELAY(*PTR) ; ; Implements a software delay of N milliseconds by invoking a ; delay of 1 ms, N times. The 1 ms delay is implemented by ; decrementing register R1 from TS to 0. TS is a constant chosen ; such that it takes 1 ms to decrement R1 to 0. A pointer (or ; address) to N is passed as PTR in register R0. ; ; Registers affected : R0, R1, FL. ; Calls : no procedures. ; Called by : main(). ; Stack depth : 1 word. ; ; Call/linkage sequence : T: .DW 5 ; ; ADD R0, NULL, T ; R0 = &T ; ; CALL RA, DELAY ; DELAY(R0) ; // &T denotes the address of T. ; DELAY: ; void DELAY(unsigned T) { SUBL SP, SP, 1 ; DSTOREM (SP), RA ; "save status" : push RA DLOADM R0, (R0) ; N = *PTR ; // Reg. var. in R0. ; // *PTR denotes contents of ; // memory location PTR. D0: SUBL NULL, R0, 0 ; while (T != 0) { JMPZ D3 ; ADDL R1, NULL, TS ; R1 = TS ; ; D1: SUBL NULL, R1, 0 ; while (R1 != 0) { JMPZ D2 ; SUBL R1, R1, 1 ; R1 = R1 - 1 ; JMP D1 ; } ; D2: SUBL R0, R0, 1 ; N = N - 1 ; JMP D0 ; } ; D3: DLOADM RA, (SP) ; "restore status" : pop RA ADDL SP, SP, 1 ; DJMP (RA) ; }