# Shell archive made by dwjones on Wed Oct 25 03:07:04 PM CDT 2023 # To install this software on a UNIX system: # 0) save this text in a file (e.g. shar) # 1) create a directory (e.g. with the shell command mkdir stacka) # 2) change to that directory (e.g. with the command cd stacka), # 3) direct the remainder of this text to sh (e.g. sh < ../shar). # This will make sh create files in the new directory; it will do # nothing else (if you're paranoid, you should scan the following text # to verify this before you follow these directions). cat > main.a <<\xxxxxxxxxx TITLE "main.a -- test my stack of strings" USE "hawk.h" USE "stdio.h" ; needed for PUTSTR USE "ascii.h" ; needed for LF USE "stack.h" ; needed for PUSH, POP, EMPTY INT MAIN S MAIN ; activation record for main ;RETAD = 0 ARSIZE = 4 MAIN: ; expects nothing ; returns nothing ; tests the stack of strings STORES R1,R2 ADDSI R2,ARSIZE LEA R3,ZEBRA ; -- parameter LIL R1,PUSH JSRS R1,R1 ; push( zebra ) LEA R3,PET ; -- parameter LIL R1,PUSH JSRS R1,R1 ; push( pet ) LEA R3,MY ; -- parameter LIL R1,PUSH JSRS R1,R1 ; push( my ) LEA R3,LIKE ; -- parameter LIL R1,PUSH JSRS R1,R1 ; push( like ) LEA R3,I ; -- parameter LIL R1,PUSH JSRS R1,R1 ; push( I ) LOOP: ; for (;;) { -- pop and print strings LIL R1,EMPTY JSRS R1,R1 TESTR R3 BZR LOOPX ; if (empty()) break; LIL R1,POP JSRS R1,R1 ; -- parameter pop() LIL R1,PUTSTR JSRS R1,R1 ; putstr( pop() ) BR LOOP LOOPX: ; } -- end loop ADDSI R2,-ARSIZE LOADS PC,R2 ; return ; strings to push on the stack for testing I: ASCII "I",0 LIKE: ASCII " like",0 MY: ASCII " my",0 PET: ASCII " pet",0 ZEBRA: ASCII " zebra.",LF,0 END xxxxxxxxxx cat > stack.a <<\xxxxxxxxxx TITLE "stack.a -- implements one instance of a stack class" USE "hawk.h" USE "stdio.h" ; needed for NULL USE "stdlib.h" ; needed for MALLOC and FREE ; code here MUST conform to interfaces documented in stack.h ; PRIVATE DEFINITIONS FOR IMPLEMENTING THE STACK ; record structure for a linked list of nodes used to implement the stack STR = 0 ; pointer to this node's string NEXT = 4 ; pointer to the next node NODESIZE= 8 COMMON TOP,4 ; pointer to the top node in the stack LCSAVE = . . = TOP W NULL . = LCSAVE ; CODE FOR IMPLEMENTING THE STACK INT PUSH ; activation record for push ;RETAD = 0 S = 4 ARSIZE = 8 PUSH: ; expects R3 = s -- the string to push ; returns nothing ; uses R3 = new -- pointer to a new node once malloc is called ; R4 = temp ; R5 = &top -- pointer to global variable top ; R6-7 -- wiped by malloc STORES R1,R2 STORE R3,R2,S LIS R3,NODESIZE ; -- parameter ADDSI R2,ARSIZE LIL R1,MALLOC JSRS R1,R1 ; new = malloc( nodesize ) -- wipes R4-7 ADDSI R2,-ARSIZE LOAD R4,R2,S STORE R4,R3,STR ; new->str = s LIL R5,TOP ; -- r5 is &top LOADS R4,R5 ; -- r4 is top STORE R4,R3,NEXT ; new->next = top STORES R3,R5 ; top = new LOADS PC,R2 ; return INT POP ; activation record for pop ;RETAD = 0 RET = 4 ; the return value ARSIZE = 8 POP: ; expects nothing ; returns R3 = ret -- the return value ; uses R3 = old -- pointer to the old top before free ; R4 = temp ; R5 = &top -- pointer to global variable top STORES R1,R2 LIL R5,TOP LOADS R3,R5 ; old = top LOAD R4,R3,STR STORE R4,R2,RET ; ret = old->str; LOAD R4,R3,NEXT STORES R4,R5 ; top = old->next; ADDSI R2,ARSIZE LIL R1,FREE ; -- param old already in R3 JSRS R1,R1 ; free( old ) -- wipes R4 ADDSI R2,-ARSIZE LOAD R3,R2,RET LOADS PC,R2 ; return ret INT EMPTY ; no activation record needed for empty EMPTY: ; expects nothing ; returns R3 = ret -- 1 if stack empty, else 0 LIL R3,TOP LOADSCC R3,R3 ; -- test top LIS R3,0 ; ret = 0 -- does not touch condition codes BNE EMPEND ; if (top == 0) { LIS R3,1 ; ret = 1 EMPEND: ; } JUMPS R1 ; return ret END xxxxxxxxxx cat > stack.h <<\xxxxxxxxxx ; stack.h -- interface to one instance of a stack class ; These routine are all compatible with the standard calling sequence ; Actual register use is documented, some use fewer registers than allowed EXT PUSH ; expects R3 = s -- the string to push on the stack ; returns nothing ; uses R3-R7, following standard calling conventions EXT POP ; expects nothing ; returns R3 = s -- a string previously pushed ; uses R3-R5, following standard calling conventions (generously) EXT EMPTY ; expects nothing ; returns R3 = 1 if stack empty, else 0 ; uses R3, following standard calling conventions (generously) xxxxxxxxxx cat > Makefile <<\xxxxxxxxxx # Makefile for dumb stack demo # Defines needed for access to hawk toolchain smal = ~dwjones/bin/smal32 -P 45 -U ~dwjones/lib/hawk hawklink= ~dwjones/bin/hawklink hawk = ~dwjones/bin/hawk demo: main.o stack.o $(hawklink) -o demo main.o stack.o main.o: main.a stack.h $(smal) main.a stack.o: stack.a stack.h $(smal) stack.a # make test -- run the dumb stack demo test: demo.o $(hawk) demo.o # make clean -- removes all files created by make clean: rm -f *.o rm -f *.l rm -f demo rm -f shar # make shar -- create a version for export shar: shar *.a *.h Makefile > shar xxxxxxxxxx