ENSC 150 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

ENSC 150

Description:

... may need to use memory locations to store variables such as loop counters while it is executing. ... we do not need special locations to be reserved for the ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 17
Provided by: luc135
Category:
Tags: ensc

less

Transcript and Presenter's Notes

Title: ENSC 150


1
ENSC 150
28
  • Stack Frames

2
Bit Reversal Problem
Consider the problem of reversing the bits of an
8-bit pattern. For example, Given the 8-bit
pattern 01010001 we would reverse them to be
10001010 OR
10001010 would become
01010001.
3
Local Variables
A subroutine may need to use memory locations to
store variables such as loop counters while it is
executing. These variables are transient in that
their values do not have any meaning while the
instruction on the calling level are executing.
In order to save memory, we can design the
subroutine so that it stores all of its internal
variables on the stack. When the subroutine exits
the stack is restored to its original position.
The memory that was being used for the internal
variables is again available.
Consider a subroutine that receives an 8-bit
input and reverses the bits from the left side to
the right side.
4
The Flowchart
We begin by writing the subroutine with Global
Variables for the parameters and the Internal
Variables.
We first find the variables and parameters.
Count can be encoded in 3 bits Left and Right
choose a 8-bit masks that have 7 0s and a
single 1 for the arrow. Input and Output both
8-bits.
5
Here is some code for you to try.
The main level that is used simply to test the
subroutine. ORG 0800 Main lds 0a00 jsr R
everse Break bra
6
Now the Initialization section of the flowchart.
Reverse movb 0,Result movb 80,Right movb
1,Left movb 8,count
Of course we still need to tell the assembler
that we need memory locations for these 4
variables. ORG 0900 Result dc.b 0 Right dc.
b 0 Left dc.b 0 Count dc.b 0
7
Now we implement the first diamond that tests for
eight loops.
TestIfLastBit tst Count beq Finished
We do not know the exact position of the branch
position YET but we suspect that it will be near
the RTS so .we write Finished rts
8
After the NO of the first diamond we hit the next
diamond.
ldaa Input bita Left beq Over
We need the value of the input parameter. For
simplicity we begin by writing the code as
thought this were a fixed GLOBAL variable.
We need to set the appropriate bit of the Result
if this diamond test fails. ldaa Result oraa R
ight staa Result
9
Now comes the instructions that update the main
variables that need to change each time around
the loop.
Over lsl Left lsr Right dec Count Bra Test
IfLastBit
10
Passing Parameters
Now suppose that we decide upon a convention for
exchanging information between the calling level
and the subroutine. The Input shall be passed to
the subroutine as an 8-bit pattern on the
stack The result shall be returned back to the
calling level as a value in Register B.
Let us write a main level loop that will call the
subroutine 16 times with different patterns and
then store the results in a 32-byte output table.
The following slide gives you an example of some
possible code that can be used to test your
subroutine. You can write the declarations for
the variables used for testing purposes by
yourself.
11
Main lds 0a00 ldx 16 ldy OutputTable m
ovb F5, TestValue TestLoop ldaa TestValue p
sha staa 1,y jsr Reverse stab 1,y pula
inc TestValue dbne x,TestLoop
12
Now we need to modify the subroutine so that it
is consistent with our new convention for
exchanging information. To begin we draw a
picture of the stack when the subroutine hits its
first instruction.
Now we simply find every instruction that used
the extended addressing-mode to access the Global
variable called Input and change it to an indexed
addressing mode that says 2,sp
To be consistent with the convention for
returning the Result we simply make sure that the
value inside the Global variable called Result is
loaded into Register B before executing the RTS.
13
Changing to Local Variables
Now we decide that we do not need special
locations to be reserved for the subroutine. We
begin by counting the bytes that are needed for
local variable storage. We have Result, Left,
Right and Count which is 4 bytes. Now we draw the
stack and CHOOSE where to put then.
The values in these 4 locations are considered
useful information while the subroutine is
executing. We must move the stack pointer upon
entry and restore it upon exit.
14
We simply find every instruction that makes a
reference to Global variables using the extended
addressing-mode and change them to indexed
mode. Input 6,sp Count 0,sp Right 1,sp Left
2,sp Result 3,sp
We also need to lower the value inside the stack
pointer by 4 at the beginning of the subroutine
and add the 4 back at the end of the
subroutine. We use the instruction
LEAS -4,sp at the beginning LEAS 4,SP at
the end
15
Using A Frame Pointer
We are nearly there. There is still one problem.
The subroutine may wish to execute an
instruction that change the value inside the
stack pointer. The subroutine will have to
execute another instruction later on that
restores the value back to its original position.
Notice that every instruction that executes in
between these two instructions will need to have
the offsets for the local variables and the input
parameters recalculated. This may not be possible.
The solution to this problem is to define another
convention. We choose an index Register and use
this register to store the position of the top of
stack upon entry. This register is called a frame
pointer. We use this register to reference all
the variables on the stack and do not allow its
value to change throughout the subroutine.
16
Reverse pshy tfr sp,y leas -4,sp tfr y
,sp puly rts
We now change all the references so that Input
4,y Result -1,y Left -2,y Right -3,y And
Count -4,y
Write a Comment
User Comments (0)
About PowerShow.com