# You may have to edit this file to delete header lines produced by # mailers or news systems from this file (all lines before these shell # comments you are currently reading). # Shell archive made by dwjones on Thu Mar 28 04:02:09 PM CDT 2024 # To install this software on a UNIX system: # 1) create a directory (e.g. with the shell command mkdir stuff) # 2) change to that directory (e.g. with the command cd stuff), # 3) direct the remainder of this text to sh (e.g. sh < ../savedmail). # 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). Then read README # in the new directory for additional instructions. cat > README <<\xxxxxxxxxx README FOR DEMO STACK ABSTRACTION WITH MULTIPLE INSTANCES this project will demonstrate a stack to show how big programs can be managed TO BUILD THE PROJECT run make stackdemo see the makefile for the relationship between the project components xxxxxxxxxx cat > Makefile <<\xxxxxxxxxx # Makefile for the project demonstrating a stack with multiple instances # Make uses sh, not bash, so we need to install the hawk and hawklink commands; # these definitions are lifted from the aliases in .bashrc # (If you try to use this makefile on a different system, you will need # to change the file names to where hawk and hawklink are installed there.) smal=~dwjones/bin/smal32 -P 45 -U ~dwjones/lib/hawk hawklink=~dwjones/bin/hawklink hawk=~dwjones/bin/hawk ########## # primary make target stackdemo.o: main.o stack.o $(hawklink) -o stackdemo main.o stack.o ########## # other make targets main.o: main.a stack.h $(smal) main.a stack.o: stack.a stack.h $(smal) stack.a ########## # Make utilities # demonsrate the program demo: stackdemo.o $(hawk) stackdemo.o # remove object and listing files clean: rm -f *.o *.l # create a shell archive of the project stackdemo.shar README Makefile main.a stack.a stack.h: shar README Makefile main.a stack.a stack.h > stackdemo.shar xxxxxxxxxx cat > main.a <<\xxxxxxxxxx TITLE "main.a -- main program to demo stacks" USE "hawk.h" USE "stdio.h" USE "stack.h" INT MAIN S MAIN ; AR for main program ;RETAD = 0 ARSIZE = 4 MAIN: STORES R1,R2 ADDSI R2,ARSIZE ; create some stacks LIL R1,NEWSTACK JSRS R1,R1 MOVE R8,R3 ; stack1 = newstack() LIL R1,NEWSTACK JSRS R1,R1 MOVE R9,R3 ; stack2 = newstack() ; push some stuff MOVE R3,R8 ; -- parameter stack1 LEA R4,TEXTC ; -- parameter string LIL R1,PUSH JSRS R1,R1 ; stack1.push( textc ) MOVE R3,R9 ; -- parameter stack2 LEA R4,TEXTD ; -- parameter string LIL R1,PUSH JSRS R1,R1 ; stack2.push( textd ) MOVE R3,R8 ; -- parameter stack1 LEA R4,TEXTA ; -- parameter string LIL R1,PUSH JSRS R1,R1 ; stack1.push( texta ) MOVE R3,R9 ; -- parameter stack2 LEA R4,TEXTB ; -- parameter string LIL R1,PUSH JSRS R1,R1 ; stack2.push( textb ) ; pop and print stuff MOVE R3,R8 ; -- parameter stack1 LIL R1,POP JSRS R1,R1 ; -- parameter stack1.pop() LIL R1,PUTSTR JSRS R1,R1 ; putstr( pop() ) -- texta = hel MOVE R3,R9 ; -- parameter stack2 LIL R1,POP JSRS R1,R1 ; -- parameter stack2.pop() LIL R1,PUTSTR JSRS R1,R1 ; putstr( pop() ) -- textb = lo MOVE R3,R8 ; -- parameter stack1 LIL R1,POP JSRS R1,R1 ; -- parameter stack1.pop() LIL R1,PUTSTR JSRS R1,R1 ; putstr( pop() ) -- textc = hel MOVE R3,R9 ; -- parameter stack2 LIL R1,POP JSRS R1,R1 ; -- parameter stack2.pop() LIL R1,PUTSTR JSRS R1,R1 ; putstr( pop() ) -- textd = lo ADDSI R2,-ARSIZE LOADS PC,R2 TEXTA: ASCII "hel",0 TEXTB: ASCII "lo ",0 TEXTC: ASCII "wor",0 TEXTD: ASCII "ld",0 END xxxxxxxxxx cat > stack.a <<\xxxxxxxxxx TITLE "stack.a -- stack class implemented using one array per stack" USE "hawk.h" USE "stdlib.h" STACKSIZE= 10 ; stack size in words ; each stack object is a structure with 2 fields: SP = 0 ; the stack pointer, address of first free word STACK = 4 ; an array of stacksize words OBJSIZE = STACK + (STACKSIZE << 2) ;========== INT NEWSTACK; construct a new stack object ; given nothing ; returns R3 -- pointer to an initialized stack object ; wipes out R3-R7 ; AR for NEWSTACK ;RETAD = 0 ARSIZE = 4 NEWSTACK: STORES R1,R2 ADDSI R2,ARSIZE LIS R3,OBJSIZE ; -- parameter LIL R1,MALLOC JSRS R1,R1 ; retval = malloc( stacksize * 4 ) LEA R4,R3,STACK STORE R4,R3,SP ; retval->sp = &(retval->stack) ADDSI R2,-ARSIZE LOADS PC,R2 ; return retval ;========== INT PUSH ; push a word on the stack ; given R3 -- stack to push on ; R4 -- word to push ; returns nothing ; wipes out R3-R7 PUSH: LEA R5,R3,SP ; -- address of stack->mysp LOADS R6,R5 ; -- value of stack->mysp STORES R4,R6 ; *(stack->mysp) = word to push ADDSI R6,4 ; stack->mysp ++ STORES R6,R5 ; -- put stack->mysp back JUMPS R1 ; return ;========== INT POP ; pop a word from the stack ; given R3 -- stack to push on ; returns R3 -- a word popped from the stack ; wipes out R4-R7 POP: LEA R5,R3,SP ; -- address of stack->mysp LOADS R6,R5 ; -- value of stack->mysp ADDSI R6,-4 ; stack->mysp -- LOADS R3,R6 ; retval = *(stack->mysp) STORES R6,R5 ; -- put stack->mysp back JUMPS R1 ; return retval END xxxxxxxxxx cat > stack.h <<\xxxxxxxxxx ; stack.h ; definitions used by all users of the stack class EXT NEWSTACK; construct a new stack object ; given nothing ; returns R3 -- pointer to an initialized stack object ; wipes out R3-R7 EXT PUSH ; push a word on the stack ; given R3 -- stack to push on ; R4 -- word to push ; returns nothing ; wipes out R3-R7 EXT POP ; pop a word from the stack ; given R3 -- stack to push on ; returns R3 -- a word popped from the stack ; wipes out R4-R7 xxxxxxxxxx