Transcript 330_05

Advanced Assembly Language
Programming
ELEC 330
Digital Systems Engineering
Dr. Ron Hayne
Outline
 More Indexing
 Bit and Byte Manipulation


Shift Operations
Logical Operations
 Arithmetic Operations



Multiplication
Division
BCD Operations
 The Stack
330_05
2
More Indexing
 Prebytes


When Y index register is included, the required number
of instruction op codes exceeds an 8-bit number
Double-byte op code


prebyte + op code byte
Op Code Maps


Pages 515, 516
Prebytes 18, 1A, CD
330_05
3
Additional Instructions
 Index Register Exchange Instructions

XGDX, XGDY

Exchange the D accumulator with the X or Y index
register
 Compare Accumulator D

CPD

Compare accumulator D to Memory
330_05
4
Shift Operations
 Rotate Instructions

ROL, ROLA, ROLB


Rotate left memory byte or accumulator
ROR, RORA, RORB

Rotate right memory byte or accumulator
 Shift Instructions

ASL, ASLA, ASLB, ASLD, LSL, LSLA, LSLB, LSLD


LSR, LSRA, LSRB, LSRD


Arithmetic or logical shift left memory byte or accumulator
Logical shift right memory byte or accumulator
ASR, ASRA, ASRB

Arithmetic shift right memory byte or accumulator
330_05
5
Logical Operations
 Bit Picking

ANDA, ANDB


BITA, BITB


And memory byte to accumulator
And memory byte to accumulator and discard result
Contents of memory called a mask

Example
330_05
6
Testing Hardware Signals
 Memory Register IN1


PB1 (bit 6)
PB2 (bit 3)
MASK1
MASK2
 Example program to
test push buttons


Execute one program if
both are pushed
Execute another
program if both not
pushed
330_05
BOTH
NOT
FCB
FCB

LDAA
ANDA
BEQ
LDAA
ANDA
BEQ




%01000000
%00001000
IN1
MASK1
NOT
IN1
MASK2
NOT
7
Logical Operations
 Bit Packing

ORAA, ORAB



 Bit Reversing

Or memory byte to
accumulator
EORA, EORB

Use ORA to pack 1s
Use ANDA to pack 0s
330_05

Exclusive or memory
byte to accumulator
Use EOR with 1 to
reverse a bit
8
Hardware Signals Continued
 Memory Register OUT

LT1 (bit 2)
 Program continued


MASK3
MASK4
BOTH
Turn on LT1 if both
buttons are pushed
Turn off LT1 otherwise
NOT
NEXT
330_05
FCB
FCB


LDAA
ORAA
STAA
BRA
LDAA
ANDA
STAA


%00000100
%11111011
OUT
MASK3
OUT
NEXT
OUT
MASK4
OUT
9
Logical Operations
 Bit Set and Clear

BSET



Set all bits in a memory
byte that correspond to 1s
in the mask
BCLR


 Bit Testing and Branching



Clear all bits in a memory
byte that correspond to
the 1s in the mask
Assembly Language
Branch if all the bits in a
memory byte that correspond
to 1s in the mask are clear
Assembly Language

330_05
Branch if all the bits in a
memory byte that correspond
to 1s in the mask are set
BRCLR


BSET LIGHTS,$10
BRSET
BRCLR 0,X,MASK1,NEXT
10
Arithmetic Operations
 Multiplication

MUL (unsigned)

Multiply accumulator A by accumulator B and put the product
into accumulator D
 Division

IDIV, FDIV

Divide two 16-bit numbers giving a 16-bit quotient and a 16-bit
remainder
 BCD Operations

DAA


Decimal adjust accumulator A
Use after ADDA, ADDB, ADCA, ADCB, ABA
330_05
11
The Stack
 Stack

Last-in-first-out
 Stack Pointer (SP)

Memory address of next available stack location
 Stack Operations

Push



Put data on stack
Decrement SP (after)
Pull


Remove data from stack
Increment SP (before)
330_05
12
Stack Instructions
 LDS


 PSHX, PSHY
Load SP
Creates a stack

 PULX, PULY
 PSHA, PSHB



Push accumulator on
stack
Decrement SP (after)

Pull from stack to index
register (2 bytes)
 TSX, TXS, TSY, TYS
 PULA, PULB

Push index register on
stack (2 bytes)

Pull from stack to
accumulator
Increment SP (before)
330_05

Transfer the SP to index
register
Transfer index register
to SP
13
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC


B7FD

A
B7FE


B7FF

B



14
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC

B7
B7FD

A
B7FE


B7FF

B


FF
15
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC

B7
B7FD

A
B7FE

22
B7FF

B

33
FF
16
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC

B7
B7FD

A
B7FE

22
B7FF
22
B

33
FE
17
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC

B7
B7FD

A
B7FE
33
22
B7FF
22
B

33
FD
18
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC

B7
B7FD

A
B7FE
33
33
B7FF
22
B

33
FE
19
Stack Example
ORG
$E100
* INITIALIZE STACK
START LDS
#$B7FF
* CREATE SAMPLE DATA
LDAA #$22
LDAB #$33
* STORE DATA IN STACK
PSHA
PSHB
* GET DATA FROM STACK
PULA
PULB
SWI
330_05

SP
B7FC

B7
B7FD

A
B7FE
33
33
B7FF
22
B

22
FF
20
Proper Rules of Stacking
 The stack is a temporary storage place
 ALWAYS PULL and PUSH the same numbers of
bytes
 Ensure that enough memory was allocated for your
stack
330_05
21
Summary
 More Indexing
 Bit and Byte Manipulation


Shift Operations
Logical Operations
 Arithmetic Operations



Multiplication
Division
BCD Operations
 The Stack
330_05
22
Subroutines
 A program module used multiple times during the
execution of a program
 It can be imbedded repeatedly in the main program
listing BUT is more efficiently CALLED by the
main program.
 Subroutines increase our programming productivity
330_05
23
Writing Subroutines
 You can get to your subroutine two ways, by
jumping (JSR) or branching (BSR)
 Both methods save the PC in the stack

This provides a “return address” to the main program
upon completion of the subroutine
 The final command of a subroutine is RTS, which
retrieves the return address from the stack

HINT: “Calling” subroutines requires a stack
330_05
24
Stack Care and Feeding
 Subroutines must leave the stack unchanged from
its state prior to the subroutine call
 Otherwise, RTS will not yield a proper return

Subroutine use of the stack must observe the
PUSH = PULL rule
 Stacks are used to:


Save temporary data
Save addresses
 Know what your use is at all times
330_05
25
Good Subroutines
 Are independent of the main program

If you move the data, does it break the subroutine?
 Are correctly structured



Single entry point and a single exit points
First instruction is always the first instruction executed
Ends with RTS
 Take extra care when using microprocessor
resources within the subroutine

Very easy for the main program and subroutine to
conflict when both use a particular accumulator or
register
330_05
26
Passing Data
 Call by Value


Copies of data are passed to the subroutine
Simple method - the main puts the data to be passed to
the subroutine in the microprocessor registers.


Can only pass 6 bytes and must take care to not to incorrectly
overwrite data
Subroutine cannot change the main programs data value
 Call by Reference

Memory addresses containing the data are passed to the
subroutine
330_05
27
Subroutine Example
* Data Section
ORG
$0010
VALUE
FCB
$46
Demo Data
* Program Section
ORG
$E100
* Create a Stack
START
LDS
#$B7FF
* Load A With Demo Data
LDAA VALUE
* Call subroutine
JSR
SWAP
DONE
BRA
DONE
"STOP"
330_05
28
Swap Subroutine
* Swap Subroutine
ORG
$E150
SWAP
LSLA
ADCA #0
LSLA
ADCA #0
LSLA
ADCA #0
LSLA
ADCA #0
RTS
Return from Subroutine
END
330_05
29
Summary
 Subroutines


JSR, BSR
RTS
 Passing Data


Call by Value
Call by Reference
330_05
30