Tölvuhögun
Assignment 4: Stack
Valdemar Örn Erlingsson
a)Pseudo-code
// Global variables
int[10] storage; // Reserve space, *storage points to bottom of stack
main()
{
int *A5_top = storage; // Points at top of stack, variable kept in register A5
int D4_size = 0;// Curent size of the stack
int D5_max = 10; // Maximum stack size, variable kept in register D5
while(1)
{
( clear screen )
printStack();
doMenu();
}
}
void printStack()
{
A3 = storage;// A3 points to lowest instance
while(A3 < A5_top)// Print all instances under A5 (A5 points to an empty instance)
{
print *(A3)+;
print(" ");
}
return
}
void doMenu()
{
print("Select an operation \n\n");
print("\t (1) Push new value \n");
print("\t (2) Pop value \n");
print("\t (3) Quit program \n\n");
input <- (input from user);
if(input == 1)
push();
elseif(input == 2)
pop();
elseif(input == 3)
quit();
else
return
// back to main
}
void push()
{
print("Enter value: ");
input = (input from user);
*(A5_top)+ = input;// Add the value and increment A5
D4_size++;// Increment size by one
// Stack full, empty it!
if(D4_size > D5_max)
{
while(A5_top > storage)// Loop and decrement A5 until A5 points to the start
{
print( *-(A5_top) + " ");
}
D4_size = 0;
}
}
void pop()
{
if(A5_top == storage)
{
print("Stack is empty!");
return;
}
output = *-(A5_top);// Remove a value and decrement A5
D4_size--;// Decrement size by one
// Oh well, I don't need to show the output...
return
}
void quit()
{
(invoke Trap task #9)
}
b)Assembly code
*------
* Program :Stack
* Written by :Valdemar Erlingsson
* Date :5/11/2008
* Description:
*------
STARTORG$1000
BRAMAIN
storageDS.w10* Reserve space for 10 numbers
SPACEDC.B' ',0
NOP
MAIN* ------
LEAstorage,A5
MOVEQ#0,D4* Current Size
MOVEQ#10,D5 * Max Stack size
LOOP* ------This is the main program loop ------
* Clear the screen
MOVEQ#0,D1
ADDI#65280,D1* Special Clear command
MOVE.B#11,D0
TRAP#15
BSRPRINTSTACK* Print the Stack
BSRDOMENU* Run the menu function
BRALOOP* Not needed, but just in case...
PRINTSTACK* ------
LEAstorage,A3
PRINTCHECK
MOVEA3,D3
CMPAD3,A5* is A3 < A5_max ?
BLEPRINTCOMPLETE
MOVE(A3)+,D1* Move data to D1
MOVE#3,D0* Print the number
TRAP#15
LEASPACE,A1* Print space
MOVE.B#14,D0
TRAP #15
BRAPRINTCHECK
PRINTCOMPLETE
LEASPACE,A1* Print blank line
MOVE.B#13,D0
TRAP #15
TRAP #15
RTS
DOMENU* ------
LEAMENTXT,A1* Print the menu
MOVE.B#14,D0
TRAP #15
MOVE.B#4,D0* Query for input
TRAP #15* input in D1
LEASPACE,A1* Print blank line
MOVE.B#13,D0
TRAP #15
CMPI#1,D1
BEQPUSH
CMPI#2,D1
BEQPOP
CMPI#3,D1
BEQQUIT
BRALOOP* Options did not fit, query again
MENTXTDC.B'Select an operation',$0D,$0A
MENTXT2DC.B$09,'(1) Push new value',$0D,$0A
MENTXT3DC.B$09,'(2) Pop value',$0D,$0A
MENTXT4DC.B$09,'(3) Quit the program',$0D,$0A
MENTXT5DC.B$09,'Selection: ',0
NOP
PUSH* ------
LEAINSERT,A1* input request
MOVE.B#14,D0
TRAP #15
MOVE.B#4,D0* Query for input
TRAP #15* input in D1
MOVED1,(A5)+* Insert new value and increment pointer
ADDI#1,D4
CMPD5,D4* Compare D5_max and D4_size
BGEFULL* The Stack is full, empty it and display values
BRALOOP* The Stack is not full, go to menu
FULL
LEAstorage,A3
CMPA3,A5* Is A5 at the lowest point?
BLEENDFULL
MOVE-(A5),D1* Move data to D1
MOVE#3,D0* Print the number
TRAP#15
LEASPACE,A1* Print space
MOVE.B#14,D0
TRAP #15
BRAFULL
ENDFULL
MOVEQ#0,D4* Set Size = 0
LEACLEANED,A1* Nothing to Pop
MOVE.B#14,D0
TRAP #15
MOVE.B#4,D0* Wait for keypress, then return
TRAP #15
BRALOOP
CLEANEDDC.B$0D,$0A,'The stack has been depleted.',$0D,$0A
CLEANE2DC.B'Press enter to continue. ',0
INSERTDC.B'Enter value: ',0
NOP
POP* ------
LEAstorage,A3
CMPA5,A3
BEQEMPTY
MOVED0,-(A5)
ADDI#-1,D4
BRALOOP
EMPTY
LEAISEMPTY,A1* Nothing to Pop
MOVE.B#14,D0
TRAP #15
MOVE.B#4,D0* Wait for keypress, then return
TRAP #15
BRALOOP
ISEMPTYDC.B'The stack is empty',$0D,$0A
ISEMPT2DC.B'Press enter to continue. ',0
NOP
QUIT* ------
MOVE.B#9,D0
TRAP#15Halt Simulator
ENDSTART
I want to draw your attention to one detail: When you overflow the stack (by adding 10 values), only nine are shown at the top of the screen. This is normal since the tenth value has just been input, which invokes the depletion-sequence. Thus, the program never has a chance to display the value at the top, but it has obviously still been added (and removed).