advanced assembly language programming
DESCRIPTION
Outline More Indexing Bit and Byte Manipulation Arithmetic Operations Shift Operations Logical Operations Arithmetic Operations Multiplication Division BCD Operations The Stack 330_05TRANSCRIPT
Advanced Assembly Language Programming
ELEC 330Digital Systems Engineering
Dr. Ron Hayne
330_05 2
Outline
More Indexing Bit and Byte Manipulation
Shift Operations Logical Operations
Arithmetic Operations Multiplication Division BCD Operations
The Stack
330_05 3
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 4
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 5
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
Arithmetic or logical shift left memory byte or accumulator LSR, LSRA, LSRB, LSRD
Logical shift right memory byte or accumulator ASR, ASRA, ASRB
Arithmetic shift right memory byte or accumulator
330_05 6
Logical Operations
Bit Picking ANDA, ANDB
And memory byte to accumulator BITA, BITB
And memory byte to accumulator and discard result
Contents of memory called a mask Example
330_05 7
Testing Hardware Signals
Memory Register IN1 PB1 (bit 6) PB2 (bit 3)
Example program to test push buttons Execute one program if
both are pushed Execute another program
if both not pushed
MASK1 FCB %01000000MASK2 FCB %00001000 LDAA IN1 ANDA MASK1 BEQ NOT LDAA IN1 ANDA MASK2 BEQ NOTBOTH NOT
330_05 8
Logical Operations
Bit Packing ORAA, ORAB
Or memory byte to accumulator
Use ORA to pack 1s Use ANDA to pack 0s
Bit Reversing EORA, EORB
Exclusive or memory byte to accumulator
Use EOR with 1 to reverse a bit
330_05 9
Hardware Signals Continued
Memory Register OUT LT1 (bit 2)
Program continued Turn on LT1 if both
buttons are pushed Turn off LT1 otherwise
MASK3 FCB %00000100MASK4 FCB %11111011 BOTH LDAA OUT ORAA MASK3 STAA OUT BRA NEXTNOT LDAA OUT ANDA MASK4 STAA OUTNEXT
330_05 10
Logical Operations
Bit Set and Clear BSET
Set all bits in a memory byte that correspond to 1s in the mask
BCLR Clear all bits in a
memory byte that correspond to the 1s in the mask
Assembly Language BSET LIGHTS,$10
Bit Testing and Branching BRSET
Branch if all the bits in a memory byte that correspond to 1s in the mask are set
BRCLR Branch if all the bits in a
memory byte that correspond to 1s in the mask are clear
Assembly Language BRCLR 0,X,MASK1,NEXT
330_05 11
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 12
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 13
Stack Instructions
LDS Load SP Creates a stack
PSHA, PSHB Push accumulator on stack Decrement SP (after)
PULA, PULB Pull from stack to
accumulator Increment SP (before)
PSHX, PSHY Push index register on
stack (2 bytes) PULX, PULY
Pull from stack to index register (2 bytes)
TSX, TXS, TSY, TYS Transfer the SP to index
register Transfer index register to
SP
330_05 14
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC
B7FD A
B7FE
B7FF B
330_05 15
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC B7 FFB7FD A
B7FE
B7FF B
330_05 16
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC B7 FFB7FD A
B7FE 22B7FF B
33
330_05 17
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC B7 FEB7FD A
B7FE 22B7FF 22 B
33
330_05 18
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC B7 FDB7FD A
B7FE 33 22B7FF 22 B
33
330_05 19
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC B7 FEB7FD A
B7FE 33 33B7FF 22 B
33
330_05 20
Stack Example
ORG $E100* INITIALIZE STACKSTART LDS #$B7FF* CREATE SAMPLE DATA LDAA #$22 LDAB #$33* STORE DATA IN STACK PSHA PSHB* GET DATA FROM STACK PULA PULB SWI
SP
B7FC B7 FFB7FD A
B7FE 33 33B7FF 22 B
22
330_05 21
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 22
Summary
More Indexing Bit and Byte Manipulation
Shift Operations Logical Operations
Arithmetic Operations Multiplication Division BCD Operations
The Stack
330_05 23
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 24
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 25
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 26
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 27
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 28
Subroutine Example
* Data Section ORG $0010VALUE FCB $46 Demo Data* Program Section ORG $E100 * Create a StackSTART LDS #$B7FF* Load A With Demo Data LDAA VALUE* Call subroutine JSR SWAPDONE BRA DONE "STOP"
330_05 29
Swap Subroutine
* Swap Subroutine ORG $E150SWAP LSLA ADCA #0 LSLA ADCA #0 LSLA ADCA #0 LSLA ADCA #0 RTS Return from Subroutine END
330_05 30
Summary
Subroutines JSR, BSR RTS
Passing Data Call by Value Call by Reference