TITLE "strings.a Strings package" ; Extracted from the second example distributed with the Jun. 28, 2018 notes. ; Here, this code is used as an example to demonstrate how a large file can ; be broken into multiple pieces for separate assembly and possible reuse. USE "hawk.h" USE "monitor.h" SUBTITLE "strlen(s)returns the length of null terminated string s" INT STRLEN 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" INT STRCPY 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 END