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

Slides:



Advertisements
Similar presentations
Floyd, Digital Fundamentals, 10 th ed EET 2261 Unit 7 Indexed Addressing Mode Read Mazidi, Chapter 6. Homework #7 and Lab #7 due next week. Quiz next week.
Advertisements

Ch. 7 Local Variables and Parameter Passing From the text by Valvano: Introduction to Embedded Systems: Interfacing to the Freescale 9S12.
EET 2261 Unit 6 The Stack; Subroutines
There are two types of addressing schemes:
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions
Addressing Modes & Instruction Set By: Prof. Mahendra B. Salunke Asst. Prof., Department of Computer Engg., SITS, Pune-41 URL:
Assembler Programming Chapter 6. EEL-4746 Best Practices.
H. Huang Transparency No.1-1 The 68HC11 Microcontroller Chapter 1: Introduction to 68HC11 The 68HC11 Microcontroller.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
1 ECE 372 – Microcontroller Design Basic Assembly Programming for(j=0; j
ECE 265 – LECTURE 4 The M68HC11 Address Modes 8/14/ ECE265.
© 2010 Kettering University, All rights reserved..
Module 10 Adapted By and Prepared James Tan © 2001.
© 2010 Kettering University, All rights reserved..
ME4447/6405 The George W. Woodruff School of Mechanical Engineering ME4447/6405 Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics.
EECC250 - Shaaban #1 Lec # 20 Winter Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Computer Architecture and the Fetch-Execute Cycle
Objectives Implement pointers using indexed addressing modes Use pointers to access arrays, strings, structures, tables, and matrices Present finite-state.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Physics 413 Chapter 4 A Fork in the Road LDAB # $ 13 here :ADDA # $ 24 DEC B BNE here WAI BNE is the new instruction. Branch if not equal to zero.
© 2010 Kettering University, All rights reserved..
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
ECE 447: Lecture 12 Logic, Arithmetic, Data Test and Control Instructions of MC68HC11.
6-1 EE 319K Introduction to Microcontrollers Lecture 6: Indexed Addressing Mode and Variants, Functional Debugging, Arrays, Strings.
Today’s Lecture Unconditional branch instruction
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ME4447/6405 The George W. Woodruff School of Mechanical Engineering ME4447/6405 Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
1 Stacks, Subroutines, I/O Routines Today: First Hour: Stacks, Subroutines –Section 3.9,3.10 of Huang’s Textbook –In-class Activity #1 Second Hour: I/O.
Addressing Modes MTT CPU08 Core Motorola CPU08 ADDRESSING MODES.
EE 345 Class Notes EE345 Midterm Review Dr. Jane Dong.
8-1 EE 319K Introduction to Microcontrollers Lecture 8:Fixed Point Numbers, Local Variables, Binding, Allocation, Access, Deallocation.
Ch.2 Intro. To Assembly Language Programming
ECE Lecture 21 Typical Assembly Language Program Bugs.
Ch.2 Intro. To Assembly Language Programming From Introduction to Embedded Systems: Interfacing to the Freescale 9s12 by Valvano, published by CENGAGE.
1 Microcontroller Fundamentals & Programming Addressing Modes.
Instruction Sets: Addressing modes and Formats Group #4  Eloy Reyes  Rafael Arevalo  Julio Hernandez  Humood Aljassar Computer Design EEL 4709c Prof:
Advanced Assembly Language Programming
Embedded Systems Lecture 5 January 25 th, 2016.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Physics 413 Chapter 4: Advanced Assembly Programming.
1 ECE 372 – Microcontroller Design Assembly Programming – Arrays unsigned short a[10]; for(j=0; j
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
1 ECE 372 – Microcontroller Design Basic Assembly Programming for(j=0; j
Revised: Aug 1, EE4390 Microprocessors Lessons 11, 12 Advanced Assembly Programming.
EE345 Chapter 2 Lecture 3 April Instruction and addressing modes 1.Extended Addressing 2.Direct Addressing 3.Inherent Addressing 4.Immediate Addressing.
5-1 EE 319K Introduction to Microcontrollers Lecture 5: Conditionals, Loops, Modular Programming, Sub- routines, Parameter passing.
EET 2261 Unit 6 The Stack; Subroutines
Displacement (Indexed) Stack
ECE 3430 – Intro to Microcomputer Systems
Addressing Modes in Microprocessors
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Microcomputer Programming
ME4447/6405 Microprocessor Control of Manufacturing Systems and
Branching and Loops.
MIPS Instructions.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
EET 2261 Unit 6 The Stack; Subroutines
ECE511: Digital System & Microprocessor
Presentation transcript:

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

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

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 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 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 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: $B adaa $0100 – Add to A the value stored at Mem[256] Machine Code: $BB staa $3800 – Store A to first RAM location of MC9S12C32 (Mem[14336]) Machine Code: $7A 38 00

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 Machine Code: $27 04 beq $FC – Branch if equal to previous instruction at PC = $FC ( ) Machine Code: $27 FC

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 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 Rel $400A = $ Rel Rel = $400A - $ Rel = 4 ($04) PC = PC Rel $4002 = $ Rel Rel = $ $ Rel = -8 ($F8)

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 Rel $400C = $ Rel Rel = $400C - $ Rel = 4 ($04) PC = PC Rel $4002 = $400A Rel Rel = $ $400A - 2 Rel = -10 ($F6)

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 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 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 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 = $ $0A = $100A Direct Address = Mem[$100A] = $2000 A = Mem[$2000]

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 = $ $0C = $100C Direct Address = Mem[$100C] = $3800 A = Mem[$3800]

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 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 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 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: $ $3FFF

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 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 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 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 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 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 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 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] = $ SP: $3FFC … $3FF9 $3FFA $3FFB $3FFC $3FFD $3FFE $3FFF $4000 … C 01

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 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 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 = $ Rel Rel = $ $4004 Rel = 16 ($10)

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 … C 01 $3FFA PC: $4014

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 … C 01 $3FFC PC: $4006

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 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: