ch10_complete

ch10_complete - F.10 Chapter 10 Solutions 10.1 The defining...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: F.10 Chapter 10 Solutions 10.1 The defining characteristic of a stack is the unique specification of how it is to be accessed. Stack is a LIFO (Last in First Out) structure. This means that the last thing that is put in the stack will be the first one to get out from the stack. 10.2 The entries in the model in Figure 10.2 actually move when other entries are pushed and popped, while they do not in the model of Figure 10.3. If the stack is implemented in memory, it makes more sense to access the one entry alone, plus the stack pointer, rather than access all entries on the stack. If the stack is implemented as a piece of tailored logic, it is faster to physically move the actual entries provided that the power required to do so can handle it. But that is a subject for a later course. 10.3 (a) PUSH R1 (b) POP R0 (c) PUSH R3 (d) POP R7 10.4 This routine copies the value of the first element on stack into R0. If underflow occurs, R5 is set to 1 (failure) else R5 remains 0 (success). Overflow error checking is not necessary because we are not adding anything to the stack. PEEK AND R5, R5, #0 ; initialize R5 LEA R0, StackBase NOT R0, R0 ADD R0, R0, #-1 ;R0 = -(addr of ;stackbase + 1) ADD R0, R0, R6 ;R6 - stack pointer BRZ Underflow LDR R0, R6, #0 ;put the first ;element in R0 RET Underflow ADD R5, R5, #1 ;failure RET StackMax .BLKW 10, x0000 StackBase .FILL x0000 10.5 One way to check for overflow and underflow conditions is to keep track of a pointer that tracks the bottom of the stack. This pointer can be compared with the address of the first and last addresses of the space allocated for the stack. ; ; Subroutines for carrying out the PUSH and POP functions. This ; program works with a stack consisting of memory locations x3FFF ; (BASE) through x3FFB (MAX). R6 is the bottom of the stack. 1 2 ; POP ST R1, Save1 ; are needed by POP. ST R2, Save2 ST R3, Save3 LD R1, NBASE ; BASE contains -x3FFF. ADD R1, R1, #-1 ; R1 contains -x4000. ADD R2, R6, R1 ; Compare bottom of stack to x4000 BRz fail_exit ; Branch if stack is empty. LD R1, BASE ;Iterate from the top of ;the stack LDI R0, BASE ;Load the value from the NOT R3, R6 ;top of stack ADD R3, R3, #1 ;Generate the ;negative of the ;bottom-of-stack pointer ADD R6, R6, #1 ;Increment the ;bottom-of-stack ;pointer pop_loop ADD R2, R1, R3 ;Compare iterating ;pointer to ;bottom-of-stack pointer BRz success_exit;Branch if no more ;entries to shift LDR R2, R1, #-1 ;Load the entry to shift STR R2, R1, #0 ;Shift the entry ADD R1, R1, #-1 ;Increment the ;iterating pointer BRnzp pop_loop PUSH ST R1, Save1 ; Save registers that ST R2, Save2 ; are needed by PUSH. ST R3, Save3 LD R1, MAX ; MAX contains -x3FFB ADD R2, R6, R1 ; Compare stack pointer to -x3FFB BRz fail_exit ; Branch if stack is full....
View Full Document

Page1 / 21

ch10_complete - F.10 Chapter 10 Solutions 10.1 The defining...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online