Download Understanding Stacks and Subroutines in Assembly Language: A Comprehensive Guide and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
Stacks Stacks
Subroutines
Complete Programmer's Model Complete Programmer's Model
SS^ XX^ HH^ II^ NN^ ZZ^ VV^ CC^ CCondition^ CodeC^ RegisterR C Carry/borrow (MSB) oVVerflow (2s C) ZZero NNegative HHalf carry (bit 3 โโโโ 4),(BCD)
15 SP 0 Stack Pointer
S STOP disable XX-interrupt mask I I-interrupt mask
7 A 0 7 B 0
15 D 0 16-bit Accumulator D
15 PC 0 Program CounterProgram Counter
15 IX 0 16-bit Index Register IX 15 IY 0 16-bit Index Register IY
8-bit Accumulators (^) AA & (^) BB
Stacks Stacks
StacksStacks
Grow down, toward low memorytoward low memory
DataData
PuSHed on the stack (PSH)
PULled off the stack (PUL)
Always PULPUL as many times as PSHPSH
The Stack Pointer always points to the next empty cell on the stack The Stack Pointer always points to the next empty cell on the stack
Top of Stack cell
Stack AreaStack Area
15 SP 0
Stack Pointer empty
Pu PuSHingSHing Data on StacksData on Stacks
SP empty
Stack AreaStack Area
SP points to empty cell
SP points to empty cell
Data is PuSHed on the stack
Data is PuSHed on the stack
data
SP empty
SP decrements and points to empty cell
SP decrements and points to empty cell
More Stack Instructions More Stack Instructions
Increment / Decrement Stack Pointer Increment / Decrement Stack Pointer: INS SP+1 โโโโ SP DES SP-1 โโโโ SP
Load / Store Stack PointerLoad / Store Stack Pointer: LDS M:M+1 โโโโ SP IMM, DIR, EXT, indX, indY STS SP โโโโ M:M+1 DIR, EXT, indX, indY
Lots of addressing modes !!
Lots of addressing modes !!
Notice the increment SP and decrement IX, IY
Notice the increment SP and decrement IX, IY
Transfer RegistersTransfer Registers: TSX SP+1 โโโโ X TSY SP+1 โโโโ Y TXS IX-1 โโโโ SP TYS IY-1 โโโโ SP
Lec Lec. Exercise 1. Exercise 1
A. Write an assembly language program starting
at $6A00 that stores the contents of the
A,B,X,Y, and CCR registers on the stack.
(A) = $2B, (B) = %00001001, (X) = $6F00, (Y) =
$C000, and (CCR) = %01x0xxxx.
B. Write an assembly language program starting
at $D300 that loads the contents of the
A,B,X,Y, and CCR registers from the stack.
(SP) = $01, (SP+1) = $02, (SP+2) = $03, (SP+3)
= $04, (SP+4) = $05, (SP+5) = $06, (SP+6) =
$07, (SP+7) = $08.
Answer Answer
Exercise 1 Answer Exercise 1 Answer
A. Push A,B,X,Y,CCR on Stack (in that order) $6A00 BE LDS #$004A Buffalo User Stack area $6A01 00 $6A02 4A $6A03 36 PSHA $6A04 37 PSHB $6A05 3C PSHX $6A06 18 PSHY $6A07 3C $6A08 07 TPA use ACCA $6A09 36 PSHA $6A0A 3F HALT
B. Pull A,B,X,Y,CCR off Stack $D300 32 PULA $D301 33 PULB $D302 38 PULX $D303 18 PULY $D304 B7 STAA SAVEA $D305 D $D306 0D $D307 32 PULA get CCR $D308 06 TAP $D309 B6 LDAA SAVEA $D30A D $D30B 0D $D30C 3F HALT $D30D -- (SAVEA) holds ACCA
Subroutine Subroutine InstuctionsInstuctions
Subroutine call Subroutine call: JSR BSR SP
RTHL
Stack AreaStack Area
RTNH
SP SP
JSR, BSR push Return Address on Stack
JSRJSR, BSRBSR push Return Address on Stack
RTN is the address of the instruction following the subroutine call
RTN is the address of the instruction following the subroutine call
RTSRTS^ pulls Return Address off Stack
empty
empty
Subroutine return:Subroutine return RTS
empty
Exercise 2 Answer P Exercise 2 Answer P--CC
Pseudo Code for
* SUBROUTINE REVERSE
* REVERSES A LIST OF LENGTH 'LISTLEN'
* X POINTS TO THE LIST
Subroutine setup:
X points to front of list, LISTLEN contains
number of elements in the list
LISTLEN/2 โโโโ COUNT; truncates if LISTLEN is odd
X + LISTLEN โ 1 โโโโ Y; Y points to end of list
for 1 to COUNT do
M(X) โโโโ A; swap M(X) and M(Y)
M(Y) โโโโ B;
A โโโโ M(Y);
B โโโโ M(X);
X + 1 โโโโ X; move pointers toward middle
Y โ 1 โโโโ Y;
return;
LISTLEN/2 โโโโ COUNT; truncates if LISTLEN is odd
X + LISTLEN โ 1 โโโโ Y; Y points to end of list
for 1 to COUNT do
M(X) โโโโ A; swap M(X) and M(Y)
M(Y) โโโโ B;
A โโโโ M(Y);
B โโโโ M(X);
X + 1 โโโโ X; move pointers toward middle
Y โ 1 โโโโ Y;
return;
Exercise 2 Answer Exercise 2 Answer AsmAsm
* SUBROUTINE REVERSE
* REVERSES A LIST OF LENGTH
'LISTLEN'
* X POINTS TO THE LIST
ORG $
REVERSE EQU *
LDAB LISTLEN
ASRB
STAB COUNT
STX XSTORE
LDY XSTORE
LDAB LISTLEN
ABY
DEY
LOOP LDAA 0,X
LDAB 0.Y
STAB 0,X
STAA 0,Y
INX
DEY
DEC COUNT
BNE LOOP
RTS
COUNT RMB 1
XSTORE RMB 2
END