TITLE "Strings package and test" USE "hawk.h" USE "monitor.h" SUBTITLE "strlen(s)returns the length of null terminated string s" STRLEN: ; expects R3 = s, a pointer to a string ; returns R3 the length of s ; uses R4 = s', working copy of s ; R5 = *s', successive values MOVE R4,R3 ; s' = s STRLLP: ; loop { LOADS R5,R4 ; -- get word holding *s' EXTB R5,R5,R4 ; -- get *s' BZS STRLQT ; if (*s' == 0) break ADDSI R4,1 ; s++ BR STRLLP STRLQT: ; } SUB R3,R4,R3 JUMPS R1 ; return s - s' SUBTITLE "strcpy(dst,src) copy null terminated string src to dst" STRCPY: ; expects R3 = dst, a pointer to a destination string ; expects R4 = src, a pointer to a source string ; returns R3 unchanged ; use R5 = dst' ; R6 = ch ; R7 temp MOVE R5,R3 ; dst' = dst STRCLP: ; loop { LOADS R6,R4 EXTB R6,R6,R4 ; ch = *src LOADS R7,R5 STUFFB R7,R6,R5 STORES R7,R5 ; *dst' = ch ; TESTR R6 -- cond codes survive from EXTB BZS STRCQT ; if (ch == NULL) break ADDSI R4,1 ; src++ ADDSI R5,1 ; dst++ BR STRCLP STRCQT: ; } JUMPS R1 ; return dst -- was always in R3 SUBTITLE "main() a main program" ; activation record structure ;RETAD = 0 BUFFER = 4 ARSIZE = BUFFER + BUFLEN S MAIN INT MAIN ; activation record structure MAIN: STORES R1,R2 ; -- save RA LEA R3,R2,BUFFER ; -- parameter dst LEA R4,ASTRING ; -- parameter src ADDI R2,R2,ARSIZE JSR R1,STRCPY ; strcpy( buffer, astring ) ADDI R2,R2,-ARSIZE ; -- parameter buffer already in R3 ADDI R2,R2,ARSIZE LIL R1,PUTS JSRS R1,R1 ; puts( buffer ) ADDI R2,R2,-ARSIZE LIS R3,' ' ; -- parameter ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( ' ' ) ADDI R2,R2,-ARSIZE LEA R3,R2,BUFFER ; -- parameter dst ADDI R2,R2,ARSIZE JSR R1,STRLEN ; temp = strlen( buffer ) ADDI R2,R2,-ARSIZE ; -- parameter temp already in R3 LIS R4,0 ; -- parameter ADDI R2,R2,ARSIZE LIL R1,PUTDEC JSRS R1,R1 ; putdec( temp, 0 ) ADDI R2,R2,-ARSIZE LOADS R1,R2 ; -- restore RA JUMPS R1 ; test data ASTRING:ASCII "this is a string",0 ITSLEN = . - ASTRING BUFLEN = ((ITSLEN + 3) >> 2) << 4 ; buflen rounded up