Physics 413 Chapter 2 HCS 12 Assembly Programming
Physics 413 Chapter 2: HCS 12 Assembly Programming
My First Assembly Program adds two 16 -bit numbers stored at $1000 - $1001 and $1002 $1003 and stores the sum at $1010 - $1011 org $2000 ldd $1000 addd $1002 std $1010 end
Assembler Directives and more … org end equ db fcc Examples alpha equ 1. 7 string fcc “hello!” array db $3, $8, $23, $11, $57, $12, $47
DAA Decimal Adjust Accumulator Converts binary addition of BCD numbers into BCD format What will the contents of A be with & without DAA ? LDAA # $ 23 ADDA # $ 48 DAA SWI 86 23 8 B 48 19 3 F
DAA LDAA # $ 23 ADDA # $ 48 DAA SWI Without DAA the contents of A = 6 B With DAA the contents of A = 71
Multiplication and Division mul A * B stored in A: B emul D * Y stored in Y: D ediv 32 -bit number Y: D divided by 16 -bit X and quotient stored in Y and remainder in D
emacs Multiply and Accumulate Multiplies two 16 -bit numbers and adds a 32 -bit number. 16 -bit numbers are pointed to by index registers X and Y. Result is stored where the 32 -bit constant was stored! 16 -bit number takes up two memory locations 32 -bit number takes up 4 memory locations. Useful in signal processing and other computational problems Rewrite ax 2 +bx +c as x(ax+b) +c for emacs format (Mx. Mx+1 )*(My. My+1 ) + (M. M +3)
A Fork in the Road LDAB # $ 13 here : ADDA # $ 24 DEC B BNE here SWI BNE is the new instruction. Branch if not equal to zero.
Branches Galore Mnemonic Description of Criteria BCC Branch if Carry Clear BCS Branch if Carry Set BEQ Branch if Equal to Zero BNE Branch if Not Equal to Zero BRA Branch Always Note: Familiarize yourself with other related instructions like DBNE, JMP, BSR, and JSR
Dare to Compare ! here: LDAA # $ E 3 CMPA $ 50 BNE here SWI Temperature sensor reading is stored at memory location 0050 and compared with a danger limit of E 3.
Bit Condition Branch Instruction again brclr $1000, $84, again ldd $70 The mask is $84. The bits to be checked are bit 2 and bit 7. The contents of memory location $1000 will be checked and when bits 2 and 7 are both 0 the instruction ldd $70 will be executed. If both bits 2 and 7 are not 0 then the program will keep checking by going back to again.
Bit Condition Branch Instruction again brset $1000, $84, again ldd $70 The mask is $84. The bits to be checked are bit 2 and bit 7. The contents of memory location $1000 will be checked and when bits 2 and 7 are both 1 the instruction ldd $70 will be executed. If both bits 2 and 7 are not 1 then the program will keep checking by going back to again.
ASLA Arithmetic Shift Left Accumulator A. It shifts all the 8 bits of accumulator A one bit to the LEFT. The rightmost bit becomes 0 and the leftmost bit ends up as the carry bit of CCR C 0
Logical AND § What is the outcome of ANDA #$ 4 C if accumulator A contains 3 A?
Logical AND 3 A 4 C 0011 1010 0100 1100 _____________ 0000 1000 Hence A will contain 08 after the AND operation
Bit Flipping and Testing § bclr 0, y, $44 clears bits 2 and 6 of the contents of the memory location pointed to by the index register Y. § bset sets the bits designated by the mask. § bita $22 tests bits 1 and 5 of accumulator A and updates the Z and N flags of the CCR but does not change the contents of the accumulator.
Delay Loop DELAY: LDX # $ FFFF AGAIN : DEX BNE AGAIN SWI
Delay Loop Subroutine here: JSR DELAY LDAA # $ E 3 CMPA $ 50 BNE here SWI. . . DELAY: LDX # $ FFFF AGAIN : DEX BNE AGAIN RTS
Stack § Stack is the area of RAM pointed to by the 16 -bit Stack Pointer (SP) § LDS functions like LDX § LDS #$ 5 C 42 The number 5 C 42 is loaded into SP § LDS $ 5 C 42 Numbers from 5 C 43 and 5 C 42 loaded § LDS $ 5 C Numbers from 005 D and 005 C loaded
Push and Pull PSHA A MSP SP - 1 SP PULA SP + 1 SP MSP A
Predict the Outcome! PSHA PSHB PULA PULB
Solution Congratulations, if you said the contents of A and B will be swapped and , perhaps more importantly, the value of the stack pointer will be restored to its original value before this program segment was run. 00 D 3 00 D 4 00 D 5 00 D 6
Detailed Explanation Suppose that SP was pointing at 00 D 6 (stack). PUSHA stores A into 00 D 6. Then PUSHB stores B into 00 D 5. At this point SP = 00 D 4. Then PULA pulls 00 D 5 (which contains B) and stores it into A. Finally, PULB pulls 00 D 6 (which contains A) and stores it into B. We end up swapping A and B. At this point SP = 00 D 6, its original value. 00 D 3 00 D 4 00 D 5 00 D 6
- Slides: 23