;----------------------------------------------------------------- ; EX77.ASM Copy a String from One Location to Another. ; Parameter Passing via Pointers. ; ; This program calls procedure void COPY(unsigned *DST, unsigned *SRC) ; to copy the source string at address SRC to the address DST. It ; then writes each of the elements of the destination string to ; output port 0x0002. ; ; Author(s): ; Signature(s): Date: ;----------------------------------------------------------------- ; PROGRAM CONSTANTS ; ; ; .EQU SENT 0 ; #define SENT 0 ;----------------------------------------------------------------- ; STACK and VARIABLES ; ; ; .ORG 0x0000 ; "varaiable assignments in RAM" .DS 5 ; "reserve RAM for the stack" TOS: .DS 1 ; SMSSG: .DS 50 ; unsigned SMSSG[50]; ; .ORG 0x8200 ; "assign the following to ROM" MSSG: .DW 'Hello ...', 0 ; unsigned MSSG = "Hello ..."; ; ;----------------------------------------------------------------- ; MAIN PROGRAM ; void main(void) { ; .ORG 0x8000 ; "assign the following to ROM" ; ADDL SP, NULL, TOS ; "initialize stack pointer" ; ADDL R1, NULL, SMSSG ; COPY(&SMSSG[0], &MSSG[0]); ADDL R0, NULL, MSSG ; CALL COPY, RA ; ADDL R0, NULL, 0 ; i = 0; // Reg. var. in R0. ; do { M0: DLOADM R1, SMSSG(R0) ; X = SMSSG[i]; // X = R1. STOREP 0x0002, R1 ; outport(0x0002, X); ADDL R0, R0, 1 ; i = i + 1; ; } SUBL NULL, R1, 0 ; while(X != 0); JMPNZ M0 ; ; } M1: JMP M1 ; // Program termination. ; ;----------------------------------------------------------------- ; PROCEDURE void COPY(unsigned *DST, unsigned *SRC) ; ; Copies the contents of the source string to the destination ; string. The source address SRC is passed in register R0 and the ; destination address DST is passed as R1. NOTE: a string is a ; "variable length" array terminate by the sentinel character, ; 0x0000. ; ; Typical CALL/LINKAGE sequence ; ; ADDL R0, NULL, SOURCE_STRING ; ADDL R1, NULL, DESTINATION_STRING ; CALL COPY, RA ; ; Calls : no procedures. ; Called by : main(). ; Registers affected : none. ; Stack level : 3 words. ;----------------------------------------------------------------- COPY: ; void COPY(unsigned *DST, ; unsigned *SRC) { SUBL# SP, SP, 1 ; DSTOREM (SP), FL ; "save status" : push FL SUBL SP, SP, 1 ; DSTOREM (SP), R2 ; "save status" : push R2 SUBL SP, SP, 1 ; DSTOREM (SP), R3 ; "save status" : push R3 ; XOR R2, R2, R2 ; i = 0; ; do { C0: ILOADM R3, (R0 + R2) ; TMP = SRC[i]; ISTOREM (R1 + R2), R3 ; DST[i] = TMP; ADDL R2, R2, 1 ; i = i + 1; SUBL NULL, R3, SENT ; while (SRC[i] != SENT); JMPNZ C0 ; ; DLOADM R3, (SP) ; "restore status" : pop R3 ADDL SP, SP, 1 ; DLOADM R2, (SP) ; "restore status" : pop R2 ADDL SP, SP, 1 ; DLOADM FL, (SP) ; "restore status" : pop FL ADDL# SP, SP, 1 ; DJMP (RA) ; }