USE "hawk.h" USE "stdio.h" USE "stdlib.h" USE "string.h" INT MAIN S MAIN ;RETAD = 0 LEN1 = 4 BUFFER = 4 ; shares space with LEN1 ARSIZE = 8 MAIN: ; void main() { STORES R1,R2 ADDSI R2,ARSIZE LIL R3,HELLO LIL R1,MYSTRLEN JSRS R1,R1 ; len1 = strlen( hello ); STORE R3,R2,LEN1 LIL R3,WORLD LIL R1,MYSTRLEN JSRS R1,R1 ; len2 = strlen( world ); LOAD R4,R2,LEN1 ; -- last use of len1 ADD R3,R4,R3 ADDSI R3,1 ; temp = len1 + len2 + 1; ; -- parameter temp LIL R1,MALLOC JSRS R1,R1 ; buffer = malloc( temp ); STORE R3,R2,BUFFER ; -- first use of buffer ; -- parameter R3 = buffer LEA R4,HELLO ; -- parameter R4 = HELLO LIL R1,STRCPY JSRS R1,R1 ; strcpy(buffer,"hello, ") LOAD R3,R2,BUFFER ; -- parameter R3 = buffer LEA R4,WORLD ; -- parameter R4 = WORLD LIL R1,STRCAT JSRS R1,R1 ; strcat(buffer,"world!") LOAD R3,R2,BUFFER LIL R1,PUTS JSRS R1,R1 ; puts(buffer) -- output the text ADDSI R2,-ARSIZE LOADS PC,R2 ; } HELLO: ASCII "hello, ",0 WORLD: ASCII "world!",0 MYSTRLEN: ; expects: R3 = s, pointer to a string ; returns: R3 = length of s ; uses: R4 = length accumulator ; R5 = ch, the character ; R6 = address of s[length] LIS R4,0 ; length = 0 STLOOP: ; for (;;) { ADD R6,R3,R4 LOADS R5,R6 EXTB R5,R5,R6 ; ch = s[length] BEQ STLPQT ; if (s[length] != 0) break ADDSI R4,1 ; length++ BR STLOOP STLPQT: ; } MOVE R3,R4 JUMPS R1 ; return length END