ECE 372 Microcontroller Design Assembly Programming n HCS


































- Slides: 34

ECE 372 – Microcontroller Design Assembly Programming n HCS 12 Assembly Programming n n n Addressing Modes Stack Operations Subroutines 1

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n n n n Inherent Immediate Direct Addressing Extended Addressing Indexed Addressing PC Relative Addressing 2

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Inherent Addressing n n n No operand field (operands are implicit) Instructions have no operand or all operands are internal CPU registers Examples: n n mul – D = A * B idiv – X = D / X and D = D % X inca – A = A + 1 nop – No operation 3

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Immediate Addressing n n n Operands are constant values Operand is included in the instruction stream (ROM) No additional memory is needed (other than fetching instruction itself) Can be 8 -bit or 16 -bit value (usually depends on instruction) Examples: n cmpa #10 – Compare A with 8 -bit constant value 10 n n adda #$10 – Add 8 -bit constant value 16 to A n n Machine Code: $81 0 A Machine Code: $86 10 cpd #$0100 – Compare D with 16 -bit constant value 256 n Machine Code: $8 C 01 00 4

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Direct Addressing n n 8 -bit address provided along with instruction Address directly points to memory location of operand 8 -bit addresses can access memory locations from $0000 to $00 FF Examples: n ldaa 50 – Load A with Mem[50] n n adaa $FF – Add to A the value stored at Mem[255] n n Machine Code: $96 32 Machine Code: $9 B FF staa 128 – Store A to Mem[128] n Machine Code: $5 A 80 5

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Extended Addressing n n Similar to direct addressing but with 16 -bit address Address directly points to memory location of operand 16 -bit addresses can access memory locations from $0000 to $FFFF Examples: n ldaa $1000 – Load A with Mem[4096] n n adaa $0100 – Add to A the value stored at Mem[256] n n Machine Code: $B 6 10 00 Machine Code: $BB 01 00 staa $3800 – Store A to first RAM location of MC 9 S 12 C 32 (Mem[14336]) n Machine Code: $7 A 38 00 6

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n PC Relative Addressing n n Used for branch instructions 8 -bit signed offset used for short branch instructions 16 -bit signed offset used for long branch instructions When instruction is fetched, the PC already points to the next instruction n PC + 2 for most short branch instructions PC + 4 for most long branch instructions Examples: n beq $04 – Branch if equal to PC + 2 + 4 n n Machine Code: $27 04 beq $FC – Branch if equal to previous instruction at PC + 2 - 4 n n -4 = $FC (11111100) Machine Code: $27 FC 7

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n PC Relative Addressing Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge End. Loop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop End. Loop: ; do something else $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $86 $00 $81 $0 A $2 C ? ? $8 B $01 LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater than or equal) PC=PC+2+Rel ADDA immediate addressing Value to add to A $4008 $4009 … $20 ? ? Branch Always PC=PC+2+Rel How do we calculate these values? 8

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n PC Relative Addressing Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge End. Loop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop End. Loop: ; do something else PC = PC + 2 + Rel $400 A = $4004 + 2 + Rel = $400 A - $4004 -2 Rel = 4 ($04) $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $86 $00 $81 $0 A $2 C ? ? $8 B $01 LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BGE (Branch if greater than or equal) PC=PC+2+Rel ADDA immediate addressing Value to add to A $4008 $4009 … $20 ? ? Branch Always PC=PC+2+Rel PC = PC + 2 + Rel $4002 = $4008 + 2 + Rel = $4002 - $4008 - 2 Rel = -8 ($F 8) 9

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n PC Relative Addressing Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 lbge End. Loop ; Else !(j<10) ; do something adda #1 ; Increment j bra Loop ; Repeat Loop End. Loop: ; do something else PC = PC + 4 + Rel $400 C = $4004 + Rel = $400 C - $4004 - 4 Rel = 4 ($04) $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $86 $00 $81 $0 A $18 $2 C ? ? LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 LBGE PC=PC+4+Rel Relative Value $4008 $4009 $400 A $400 B … $8 B $01 $20 ? ? ADDA immediate addressing Value to add to A Branch Always PC=PC+2+Rel PC = PC + 2 + Rel $4002 = $400 A + 2 + Rel = $4002 - $400 A - 2 Rel = -10 ($F 6) 10

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Indexed Addressing n Address is based of o value stored within register (S) n n Typically based on X or Y index registers (not required) Many index addressing modes exist n n n 5/9 -bit Constant Offset Auto Pre/Post Decrement/Increment 16 -bit Constant Offset 16 -bit Indirect Indexed Accumulator Offset Accumulator D Indirect 11

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n 5/9/16 -bit Constant Offset Indexed Addressing n n Calculates addressed based on index register, stack pointer, or PC Address is 5/9/16 -bit offset added to address within X/Y/SP/PC n n Offsets are assumed to be signed values Example n n n ldaa 0, X – Load A with value stored at Mem[X+0] (5 -bit) stab -8, Y – Store B to Mem[Y-8] (5 -bit) stab -20, Y – Store B to Mem[Y-20] (9 -bit) 12

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Accumulator Offset Indexed Addressing n n n Calculates addressed based on index register, stack pointer, or PC and accumulators A, B, or D Address is the unsigned A/B/D offset added to address within X/Y/SP/PC Example n n ldaa B, X – Load A with value stored at Mem[X+B] stab D, Y – Store B to Mem[Y+D] 13

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n 16 -bit Constant Indirect Indexed Addressing n Calculates indirect address based on index register, stack pointer, or PC and a 16 -bit offset n n n Indirect address points to memory location of pointer providing memory address of operand Direct address is value stored in memory location provided by indirect address Example n ldaa [10, X] – Load A with value stored at Mem[X+10]] n n If X = $1000 and Mem[$100 A: $100 B] = $2000 Indirect address = X+10 = $1000 + $0 A = $100 A Direct Address = Mem[$100 A] = $2000 A = Mem[$2000] 14

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Accumulator D Indirect Indexed Addressing n Calculates indirect address based on index register, stack pointer, or PC and 16 -bit accumulator D n n n Indirect address points to memory location of pointer providing memory address of operand Direct address is value stored in memory location provided by indirect address Example n ldaa [D, X] – Load A with value stored at Mem[X+D]] n n If X = $1000, D = $0 C, and Mem[$100 C: $100 D] = $3800 Indirect address = X+12 = $1000 + $0 C = $100 C Direct Address = Mem[$100 C] = $3800 A = Mem[$3800] 15

ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes n Auto Pre/Post Decrement/Increment Indexed Addressing n n Calculates address based on index register or stack pointer Provides four methods for automatically adjusting the index register during the instruction n Pre-decrement/Pre-Increment n n Post-decrement/Post-Increment n n n Decrements or Increments the index register before calculating address Decrements or Increments the index register after calculating address Can be decremented/increment in ranges from -1 to -8 and 1 to 8 Example n n ldaa 1, -X – Load A with value stored at Mem[X-1], X = X - 1 staa 2, X+ – Store A to Mem[X], X = X + 2 16

ECE 372 – Microcontroller Design Assembly Programming n Create an assembly program to add together two 32 -bit unsigned numbers stored in memory at location $3800 and $3804 storing the result in location $3808. 17

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Stack Pointer (SP) n n Points to memory location corresponding to the top of the stack Must be initialized within your assembly program Initialize to location $4000 for MC 9 S 12 C 32 Basic Stack Pointer Instructions n n n des – Decrement stack pointer (SP) ins – Increment stack pointer (SP) psha/pshb/pshd/pshx/pshy – Push A/B/D/X/Y to stack n n pula/pulb/pulx/puly – Pull (Pop) A/B/D/X/Y from stack n n n Automatically adjusts stack pointer lds – Load stack pointer (SP) with from memory (or immediate value) sts – Store stack pointer (SP) to memory 18

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Storing and Retrieving from top of Stack n n Utilized stack to store temporary data Push data onto stack Pop data from stack Example: ldaa #0 psha ldaa #20 psha ldd #300 pshd ; ; ; A = 0 Push A(0) onto stack A = 20 Push A(20) onto stack D = 300 Push D(300) onto stack . . . puld pula pulb ; D = 300 ; A = 20 ; B = 0 SP: $4000 $3 FFF … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 00 19

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Storing and Retrieving from top of Stack n n Utilized stack to store temporary data Push data onto stack Pop data from stack Example: ldaa #0 psha ldaa #20 psha ldd #300 pshd ; ; ; A = 0 Push A(0) onto stack A = 20 Push A(20) onto stack D = 300 Push D(300) onto stack . . . puld pula pulb ; D = 300 ; A = 20 ; B = 0 SP: $3 FFE $3 FFF … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 14 00 20

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Storing and Retrieving from top of Stack n n Utilized stack to store temporary data Push data onto stack Pop data from stack Example: ldaa #0 psha ldaa #20 psha ldd #300 pshd ; ; ; A = 0 Push A(0) onto stack A = 20 Push A(20) onto stack D = 300 Push D(300) onto stack . . . puld pula pulb ; D = 300 ; A = 20 ; B = 0 SP: $3 FFC $3 FFE … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 01 2 C 14 00 21

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Storing and Retrieving from top of Stack n n Utilized stack to store temporary data Push data onto stack Pop data from stack Example: ldaa #0 psha ldaa #20 psha ldd #300 pshd ; ; ; A = 0 Push A(0) onto stack A = 20 Push A(20) onto stack D = 300 Push D(300) onto stack . . . puld pula pulb ; D = 300 ; A = 20 ; B = 0 SP: $3 FFE $3 FFC … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 01 2 C 14 00 22

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Storing and Retrieving from top of Stack n n Utilized stack to store temporary data Push data onto stack Pop data from stack Example: ldaa #0 psha ldaa #20 psha ldd #300 pshd ; ; ; A = 0 Push A(0) onto stack A = 20 Push A(20) onto stack D = 300 Push D(300) onto stack . . . puld pula pulb ; D = 300 ; A = 20 ; B = 0 SP: $3 FFF $3 FFE … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 14 00 23

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Storing and Retrieving from top of Stack n n Utilized stack to store temporary data Push data onto stack Pop data from stack Example: ldaa #0 psha ldaa #20 psha ldd #300 pshd ; ; ; A = 0 Push A(0) onto stack A = 20 Push A(20) onto stack D = 300 Push D(300) onto stack . . . puld pula pulb ; D = 300 ; A = 20 ; B = 0 SP: $4000 $3 FFF … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 00 24

ECE 372 – Microcontroller Design Assembly Programming – Arrays Array For Loop Example: Assembly Code: unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0 x 04; else PORTT=0 x 00; } ldaa #0 ldx #$3800 Loop: cmpa #10 bge End. Loop staa $3814 ldd 0, X cpd #1 bne Else ldab #4 bra End. If Else: ldab #0 End. If: stab PTT ldaa $3814 adda #1 inx bra Loop End. Loop: Programming Steps: 1. 2. 3. 4. Initialize J Compare J to 10 If Not Less than 10, 1. End Loop Else 1. 2. 3. 4. 5. Load a[j] If a[j] == 1 1. PORT T = 4 Else 1. PORT T = 0 Increment J Repeat Loop (Step 2) ; ; ; ; ; Initialize J Initialize index to A[0] Compare J to 10 Else !(J<10) Store J to RAM load A[J] Compare J to 1 Else !(A[J]==1) Value to write to PORT T ; ; ; ; Value to write to PORT T Write value to PORT T Read J from RAM Increment J Increment A[J] Need to increment by 2 Repeat Loop do something else We could store these values on the stack instead (temporary value). 25

ECE 372 – Microcontroller Design Assembly Programming – Arrays Array For Loop Example: Assembly Code: unsigned short a[10]; for(j=0; j<10; j++) { if(a[j]==1) PORTT=0 x 04; else PORTT=0 x 00; } lds #$4000 ldaa #0 ldx #$3800 Loop: cmpa #10 bge End. Loop psha ldd 0, X cpd #1 bne Else ldab #4 bra End. If Else: ldab #0 End. If: stab PTT pula adda #1 inx bra Loop End. Loop: Programming Steps: 1. 2. 3. 4. Initialize J Compare J to 10 If Not Less than 10, 1. End Loop Else 1. 2. 3. 4. 5. Load a[j] If a[j] == 1 1. PORT T = 4 Else 1. PORT T = 0 Increment J Repeat Loop (Step 2) Initialize stack pointer ; ; ; ; ; Initialize SP Initialize J Initialize index to A[0] Compare J to 10 Else !(J<10) Push J to stack load A[J] Compare J to 1 Else !(A[J]==1) Value to write to PORT T ; ; ; ; Value to write to PORT T Write value to PORT T Pull(Pop) J from Stack Increment J Increment A[J] Need to increment by 2 Repeat Loop do something else Replace ldaa and staa with psha and pula. 26

ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer n Accessing items within stack (not at the top) n n Push/Pull only provide access to top of stack Can use index addressing to access stack items not at top of stack . . . ldab 1, SP ldd 2, SP ; B = Mem[SP-1] = $2 C ; D = Mem[SP-2] = $2000 . . . SP: $3 FFC … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 01 2 C 14 00 27

ECE 372 – Microcontroller Design Assembly Programming – Subroutines n Similar to functions in C/C++ n n n Allows program to call routine Parameter passing must be done explicitly Basic Subroutine Instructions n bsr – Brach to subroutine (PC Relative Addressing) n n n jsr – Jump to subroutine n n n Return address will be pushed onto stack Uses 16 -bit address of following instruction (PC + 2) Return address will be pushed onto stack Uses 16 -bit address of following instruction (varies depending on addressing mode) rts – Return from subroutine n PC will pulled (popped) from top of stack 28

ECE 372 – Microcontroller Design Assembly Programming – Subroutines n Simple Subroutine Example C Function Example: Assembly Code: for(j=0; j<10; j++) { Func. A(); } ldaa #0 Loop: cmpa #10 bge End. Loop bsr Func. A adda #1 bra Loop End. Loop: ; ; ; Func. A: ; do something ; Return from Func. A void Func. A() { // do something } rts Initialize j Compare j to 10 Else !(j<10) Call Func. A Increment j Repeat Loop 29

ECE 372 – Microcontroller Design Assembly Programming – Subroutines Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop bsr Func. A adda #1 bra Loop End. Loop: ; ; ; Func. A: ; do something ; Return from Func. A rts Initialize j Compare j to 10 Else !(j<10) Call Func. A Increment j Repeat Loop $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $86 $00 $81 $0 A $2 C ? ? $8 B $01 LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BSR (Branch to Func. A) PC=PC+Rel ADDA immediate addressing Value to add to A $4008 $4009 … $4014 $20 $04 Branch Always PC=PC+2+Rel $3 D RTS (Return from Func. A) How do we calculate offset? PC = PC + Rel $4014 = $4004 + Rel = $4014 - $4004 Rel = 16 ($10) 30

ECE 372 – Microcontroller Design Assembly Programming – Subroutines Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop bsr Func. A adda #1 bra Loop End. Loop: ; ; ; Func. A: ; do something ; Return from Func. A rts Initialize j Compare j to 10 Else !(j<10) Call Func. A Increment j Repeat Loop $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $86 $00 $81 $0 A $2 C $10 $8 B $01 LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BSR (Branch to Func. A) PC=PC+Rel ADDA immediate addressing Value to add to A $4008 $4009 … $4014 $20 $04 Branch Always PC=PC+2+Rel $3 D RTS (Return from Func. A) $3 FFC SP: $3 FFA $4004 PC: $4014 … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 40 06 01 2 C 14 00 31

ECE 372 – Microcontroller Design Assembly Programming – Subroutines Assembly Code: ldaa #0 Loop: cmpa #10 bge End. Loop bsr Func. A adda #1 bra Loop End. Loop: ; ; ; Func. A: ; do something ; Return from Func. A rts Initialize j Compare j to 10 Else !(j<10) Call Func. A Increment j Repeat Loop $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 $86 $00 $81 $0 A $2 C $10 $8 B $01 LDAA immediate addressing Value to be stored in A CMPA immediate addressing Compare Value 10 BSR (Branch to Func. A) PC=PC+Rel ADDA immediate addressing Value to add to A $4008 $4009 … $4014 $20 $04 Branch Always PC=PC+2+Rel $3 D RTS (Return from Func. A) $3 FFC SP: $3 FFA $4006 PC: $4014 … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 40 06 01 2 C 14 00 32

ECE 372 – Microcontroller Design Assembly Programming – Subroutines n Subroutines with Parameters n n Utilize stack to pass parameters (in addition to return address) Caller Initialize Steps: n n Caller Steps: n n Push function parameters onto stack Allocate space on stack for return value Call subroutine Access parameters on using index addressing Caller Finishing Steps: n n Pull (Pop) return value from stack Deallocate space on stack previously used by parameters C Function Example: for(j=0; j<10; ) { j = Func. A(j); } byte Func. A(byte a) { return a+2; } Return Address: Return Value: Parameters: Initial SP: … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … 33

ECE 372 – Microcontroller Design Assembly Programming – Subroutines C Function Example: Assembly Code: for(j=0; j<10; ) { j = Func. A(j); } ldaa #0 Loop: cmpa #10 bge End. Loop psha des bsr Func. A pula ins bra Loop End. Loop: byte Func. A(byte a) { return a+2; } SP: Ret. Val: Param: … $3 FF 9 $3 FFA $3 FFB $3 FFC $3 FFD $3 FFE $3 FFF $4000 … ; ; ; ; ; Initialize j Compare j to 10 Else !(j<10) Push parameter J onto Stack Allocate space for return value Call Func. A Pull return value from stack Deallocate space from stack Repeat Loop ; ; Load parameter from stack Add 2 to A Store return value to stack Return from Func. A: ldaa 3, SP adda #2 staa 2, SP rts 34
Microcontroller assembly language programming
Db directive in 8051
Microcontroller
Ece 372
Clemson ece lab manual
Hcs clustering algorithm
Hcs classlink
Cisco hosted unified communications services
For offical use only
Classworks.manager.hcs
Inb 372 nsu
Resolução consema 372/2018
Uwace.uwaterloo.ca
Round 372 to the nearest 10
Microprocessor based system design
Microcontroller pcb design
Microcontroller lecture
Advanced assembler directives in system programming
Pic language
Language
Language level
Elements of assembly language programming
Example of assembly language
Assembly language programming examples
Loop 8086
Interrupt vector table of 8051
Design of assembler in system programming
Assembly language program development tools in 8086
Motorola 68k assembly
Arm assembly language programming examples
Perbedaan linear programming dan integer programming
Greedy algorithm vs dynamic programming
Components of system programming
Linear vs integer programming
Programing adalah