Docsity

Understanding Stacks and Subroutines in Assembly Language: A Comprehensive Guide, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Stacks
Stacks
Subroutines
2
Complete Programmer's Model
Complete Programmer's Model
S
SX
XH
HI
IN
NZ
ZV
VC
CC
Condition C
Code R
Register
C
Carry/borrow (MSB)
oV
Verflow (2s C)
Z
Zero
N
Negative
H
Half carry (bit 3โ†’
โ†’โ†’
โ†’4),(BCD)
15 SP 0 Stack Pointer
S
STOP disable
X
X-interrupt mask
I
I-interrupt mask
7 A 0 7 B 0
15 D 0 16-bit Accumulator D
15 PC 0 Program Counter
Program Counter
15 IX 0 16-bit Index Register IX
15 IY 0 16-bit Index Register IY
8-bit Accumulators A
A & B
B
pf3
pf4
pf5

Partial preview of the text

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