Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.

Similar presentations


Presentation on theme: "1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines."— Presentation transcript:

1 1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines

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

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

4 4 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes Immediate Addressing 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: cmpa #10 – Compare A with 8-bit constant value 10 Machine Code: $81 0A adda #$10 – Add 8-bit constant value 16 to A Machine Code: $86 10 cpd #$0100 – Compare D with 16-bit constant value 256 Machine Code: $8C 01 00

5 5 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes Direct Addressing 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 $00FF Examples: ldaa 50 – Load A with Mem[50] Machine Code: $96 32 adaa $FF – Add to A the value stored at Mem[255] Machine Code: $9B FF staa 128 – Store A to Mem[128] Machine Code: $5A 80

6 6 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes Extended Addressing 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: ldaa $1000 – Load A with Mem[4096] Machine Code: $B6 10 00 adaa $0100 – Add to A the value stored at Mem[256] Machine Code: $BB 01 00 staa $3800 – Store A to first RAM location of MC9S12C32 (Mem[14336]) Machine Code: $7A 38 00

7 7 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes PC Relative Addressing 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 ready points to the next instruction PC + 2 for most short branch instructions PC + 4 for most long branch instructions Examples: beq $04 – Branch if equal to PC + 2 + 4 Machine Code: $27 04 beq $FC – Branch if equal to previous instruction at PC + 2 - 4 -4 = $FC (11111100) Machine Code: $27 FC

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

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

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

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

12 12 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes 5/9/16-bit Constant Offset Indexed Addressing 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 Offsets are assumed to be signed values Example 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)

13 13 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes Accumulator Offset Indexed Addressing 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 ldaa B,X – Load A with value stored at Mem[X+B] stab D,Y – Store B to Mem[Y+D]

14 14 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes 16-bit Constant Indirect Indexed Addressing Calculates indirect address based on index register, stack pointer, or PC and a 16-bit offset 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 ldaa [10,X] – Load A with value stored at Mem[Mem[X+10]] If X = $1000 and Mem[$100A:$100B] = $2000 Indirect address = X+10 = $1000 + $0A = $100A Direct Address = Mem[$100A] = $2000 A = Mem[$2000]

15 15 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes Accumulator D Indirect Indexed Addressing Calculates indirect address based on index register, stack pointer, or PC and 16-bit accumulator D 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 ldaa [D,X] – Load A with value stored at Mem[Mem[X+D]] If X = $1000, D = $0C, and Mem[$100C:$100D] = $3800 Indirect address = X+12 = $1000 + $0C = $100C Direct Address = Mem[$100C] = $3800 A = Mem[$3800]

16 16 ECE 372 – Microcontroller Design Assembly Programming – Addressing Modes Auto Pre/Post Decrement/Increment Indexed Addressing Calculates address based on index register or stack pointer Provides four methods for automatically adjusting the index register during the instruction Pre-decrement/Pre-Increment Decrements or Increments the index register before calculating address Post-decrement/Post-Increment Decrements or Increments the index register after calculating address Can be decremented/increment in ranges from -1 to -8 and 1 to 8 Example 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

17 17 ECE 372 – Microcontroller Design Assembly Programming 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.

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

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

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

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

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

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

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

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

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

27 27 ECE 372 – Microcontroller Design Assembly Programming – Stack Pointer Accessing items within stack (not at the top) 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 ; B = Mem[SP-1] = $2C ldd 2,SP ; D = Mem[SP-2] = $2000... SP: $3FFC … $3FF9 $3FFA $3FFB $3FFC $3FFD $3FFE $3FFF $4000 … 00 14 2C 01

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

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

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

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

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

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

34 34 ECE 372 – Microcontroller Design Assembly Programming – Subroutines for(j=0; j<10; ) { j = FuncA(j); } byte FuncA(byte a) { return a+2; } C Function Example: Assembly Code: ldaa #0 ; Initialize j Loop: cmpa #10 ; Compare j to 10 bge EndLoop ; Else !(j<10) psha ; Push parameter J onto Stack des ; Allocate space for return value bsr FuncA ; Call FuncA pula ; Pull return value from stack ins ; Deallocate space from stack bra Loop ; Repeat Loop EndLoop: FuncA: ldaa 3,SP ; Load parameter from stack adda #2 ; Add 2 to A staa 2,SP ; Store return value to stack rts ; Return from FuncA … $3FF9 $3FFA $3FFB $3FFC $3FFD $3FFE $3FFF $4000 … SP: RetVal: Param:


Download ppt "1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines."

Similar presentations


Ads by Google