Introduction to Computer Systems Chapter 10

Introduction to Computer Systems Chapter 10 - Appendix F...

Info iconThis preview shows pages 1–4. 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

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: Appendix F Selected Solutions 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.3 (a) PUSH R1 (b) POP R0 (c) PUSH R3 (d) POP R7 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. ; 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. 1 2 APPENDIX F. SELECTED SOLUTIONS 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. ADD R1, R6, #0 ;Iterate from the bottom ;of stack LD R3, NBASE ;NBASE contains ;-x3FFF ADD R3, R3, #-1 ; R3 = -x4000 push_loop ADD R2, R1, R3 ;Compare iterating ;pointer to ;bottom-of-stack pointer BRz push_entry ;Branch if no more ;entries to shift LDR R2, R1, #0 ;Load the entry to shift STR R2, R1, #-1 ;Shift the entry ADD R1, R1, #1 ;Decrement the ;iterating pointer BRnzp push_loop F.10. CHAPTER 10 SOLUTIONS 3 push_entry ADD R6, R6, #-1 ;Increment the ;bottom-of-stack pointer STI R0, BASE ;Push a value onto stack BRnzp success_exit success_exit LD R1, Save1 ;Restore original LD R2, Save2 ;register values LD R3, Save3 AND R5, R5, #0 ;R5 <--- success RET fail_exit LD R1, Save1 ;Restore original LD R2, Save2 ;register values LD R3, Save3 AND R5, R5, #0 ADD R5, R5, #1 ;R5 <--- failure RET BASE .FILL x3FFF NBASE .FILL xC001 ; NBASE contains -x3FFF....
View Full Document

This note was uploaded on 03/30/2008 for the course ECE 252 taught by Professor Wood during the Spring '08 term at Wisconsin.

Page1 / 13

Introduction to Computer Systems Chapter 10 - Appendix F...

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

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