Lab: stack operations

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

From the previous question, it is important to note how a return address is stored on stack on a "per invocation" basis as opposed to a "per subroutine/function" basis. In other words, no memory is used on stack until a function is called/invoked to store the return address to the caller. In the case of recursion, however, each invocation has its own return address stored. This is partially why recursion is even possible. Up to how many bytes are used on stack during the following program that calls a recursive subroutine? When a memory location is overwritten, it is "used". Note that you can find the answer by running the code with logging (you have to figure out which register and what else to log), or by analyzing the code. main: ldi c,3 ldi d,0 ldi a,retL1 dec d st (d),a jmpi sub1 retL1: halt sub1: and c,c jzi cIsZero dec c ldi a,retL2 dec d st (d),a jmpi sub1 retL2: cIsZero: ld a,(d) inc d jmp a

4

The content on stack can be anything. For example, when you run out of registers to use, you can temporarily "push" the value of a register on stack, use the register for some other purpose, and then "pop" the value back to restore the previous value of the register. This technique is quite useful in architectures that have a somewhat limited number of registers. The stack can also be used to store addresses that relate to the execution path of a program. This is an important concept because this is how all modern programming languages implement subroutine call and return. The following code is typical from the perspective of the caller (assuming the name of the subroutine is just "subroutine"): ldi a,retL1 dec d st (d),a jmpi subroutine retL1: The corresponding code of the callee is as follows: subroutine: // whatever the subroutine needs to do ld a,(d) inc d jmp a Note the use of the instruction mnemonic "jmp a". What does this instruction do?

continues execution at where register a points to

For reasons that will be become clear later, the stack grows from high address to low address. This means that when a stack is empty, the SP points the the location that is one byte past the allocated space of the stack. Whenever a new item is pushed on the stack, the SP decrements. Because the SP always points to the last item pushed, or the first item to be retrieved/removed, this means a push operation logically involves the following: allocate space on stack (decrement SP) copy value to push to where SP points to The opposite, retrieving and removing an item from the stack, involves the following: copy value from where SP points to deallocate space on stack (increment SP) This implementation convention is counter-intuitive when it comes to the naming of the last item pushed on the stack, also called the "top" of the stack. In other words, the "top" of the stack, which is where the SP points to, is actually a memory location that is the lowest of all the memory used by the stack. Which of the following instruction mnemonic is useful to allocate space on stack?

dec d

TTP has no actual stack pointer, it is a convention that register D is used as the stack pointer (SP). A "stack" is a data structure that follows the last-in-first-out (LIFO) store-retrieval order. There are many ways to implement a stack, but the use of an array (such as memory locations) and a SP is one of the most efficient way to do it. There are two operations related to the stack: to "push" is to store an item (a byte) on the stack to "pop" is to retrieve (and remove) an item (a byte) from the stack The convention of using register D as the SP also means that: register D points to the next item to retrieve (pop) the most recently item pushed is pointed to by register D Which of the following instruction mnemonic retrieves (but does not remove) an item from the stack into register A?

ld a,(d)


Ensembles d'études connexes

Property - Rule Against Perpetuities - Problem Set

View Set

SB ch. 5? Developmental psych 3200

View Set

OB Chapt. 22 Nursing Management of the Postpartum Woman at Risk

View Set

Geology Chapter 16 Running Water

View Set

Child Development 252 - Chapter 4

View Set