TITLE "mystring.a -- a string demo program (good version)" USE "hawk.h" USE "stdio.h" ; myscpy -- homemade strcpy ; does not use activation record MYSCPY: ; param R3 address of string dst ; R4 address of string src ; returns nothing ; use R5 to load bytes from *src ; use R6 to store bytes to *dst ; ; int myscpy( char * dst, char * src ) { MYCLOOP: ; do { LOADS R5,R4 EXTB R5,R5,R4 ; -- get *src LOADS R6,R3 ; -- no change to CC from here to BZS STUFFB R6,R5,R3 ; STORES R6,R3 ; *dst = *src BZS MYCQUIT ; if (*src == '\0') break ADDSI R3,1 ; src++ ADDSI R4,1 ; dst++ BR MYCLOOP MYCQUIT: ; } while no break JUMPS R1 ; } ; myslen -- homemade strlen ; does not use activation record MYSLEN: ; param R3 address of string s ; returns R3 as length of s ; use R4 as start ; use R5 to load bytes from *s ; ; int myslen( char * s ) { MOVE R4,R3 ; start = s MYSLOOP: LOADS R5,R3 EXTB R5,R5,R3 ; -- get *s BZS MYSQUIT ; while (*s != '\0') { ADDSI R3,1 ; s++ BR MYSLOOP MYSQUIT: ; } SUB R3,R3,R4 ; return s - start JUMPS R1 ; } ; play -- subroutine to play with a string ; activation record for play RETAD = 0 R8SAVE = 4 BUF = 8 ; buffer to copy string into ARSIZE = 40 ; big enough buffer (barely, just 32 characters long) PLAY: ; param R3 address of string s STORES R1,R2 STORE R8,R2,R8SAVE ADDI R2,R2,ARSIZE ; void play( char * s ) { MOVE R8,R3 ; -- save s JSR R1,MYSLEN ; -- parameter myslen( s ) LIS R4,8 ; -- parameter LIL R1,PUTDEC JSRS R1,R1 ; putdec( myslen( s ), 8 ) LIS R3,' ' ; -- parameter LIL R1,PUTCHAR JSRS R1,R1 ; putchar( ' ' ) LEA R3,R2,BUF-ARSIZE; -- param buf (reach down into AR to get it) MOVE R4,R8 ; -- param s JSR R1,MYSCPY ; myscpy( buf, s ) LEA R3,R2,BUF-ARSIZE; -- param buf (reach down into AR to get it) LIL R1,PUTS JSRS R1,R1 ; puts( buf ) ADDI R2,R2,-ARSIZE LOAD R8,R2,R8SAVE LOADS R1,R2 JUMPS R1 ; } ; main program S MAIN INT MAIN ; activation record for main RETAD = 0 ARSIZE = 4 MAIN: STORES R1,R2 ADDSI R2,ARSIZE LEA R3,STRING1 ; param JSR R1,PLAY ; play( "Test string" ) LEA R3,STRING2 ; param JSR R1,PLAY ; play( "1234567" ) LEA R3,STRING3 ; param JSR R1,PLAY ; play( string3 ) ADDSI R2,-ARSIZE LOADS R1,R2 JUMPS R1 ; if any of these strings longer than the buffer size, risk of buffer overflow STRING1:ASCII "Test string",0 STRING2:ASCII "1234567",0 STRING3:ASCII "Some humongous string of junk",0