topic03d using assembly in the freescale mc9s12x
DESCRIPTION
This topic explains some of the key skills required to be a worthwhile embedded software engineer.TRANSCRIPT
![Page 1: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/1.jpg)
Assembly Language: Using it
Topic Video 03D3D
1Monday, 31 August 2009
![Page 2: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/2.jpg)
Structured Assembler Programming
• An assembler program consists of the following in this order:– Program Header
The program header should briefly explain what the program does. The authors name should be listed so blame or praise can be passed on to the appropriate person. The program header should also contain the date and revision history, and list any additional files that are required to assemble the program.
Example
; Program Header Example version 1.1;;This is an example of a program header, it contains examples of the sort of
information you would expect to find in a program header.;Source File: ALP2.ppt;Author: Brett Wildermoth;Created: 09/09/2002;Modifications: version 1.1 – fixed spelling errors; version 1.0 - None
2Monday, 31 August 2009
![Page 3: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/3.jpg)
Structured Assembler Programming
– Assembler EquatesAfter the program header the assembler equates should be listed. These include all
assembler related constants, that are substituted during the assembly process. Assembler equates can consist of three possible types:» System Equates: Equates such as equates to Monitor Routine or I/O ports.» Constant Equates: Equates to constants used in the program» Memory Map Equates: Equates defining the different sections of the memory map
;Monitor Equates out2hex: EQU $FE16 ;I/O Equates PORTA: EQU $0000 DDRA: EQU $0002 ;Constant Equates ONE: EQU 1 ;Memory Map Equates PRG EQU $0D00 SRAM EQU $0800
3Monday, 31 August 2009
![Page 4: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/4.jpg)
Structured Assembler Programming
– VariablesAny data that may change its value during the lifetime of the software. Generally
this section will be preceded by an ORG statement referring to RAM.– Program Location
The program location contains only one line with a single ORG pseudo-operator, that defines the starting location of the program.
– Program InitialisationThis section of the code contains instructions related to initialising system
resources such as the stack pointer.– Main Program Body
This is the major section of any assembler program and contains the program’s instructions.
– Constants– Vector Table
4Monday, 31 August 2009
![Page 5: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/5.jpg)
;*****************************************************************
; Example Assembly Program version 1.1
;
;This is an example of an assembly program, it contains examples of the sort of information you would expect to find in an assembly program.
;Source File: main.asm
;Author: Brett Wildermoth
;Created: 11/02/2008
;Modifications: version 1.1 – fixed some errors
;*****************************************************************
; export symbols
XDEF Entry ; export 'Entry' symbol
ABSENTRY Entry ; for absolute assembly: mark this as application entry point
; include derivative specific macros
INCLUDE 'mc9s12xdp512.inc'
ROMStart EQU $4000 ; absolute address to place my code/constant data
ProgramHeader
Structured Assembler ProgrammingExample
Assembler Equates
File full of Assembler
Equates
Using an assembler directive to tell it were the start of the
code is.
5Monday, 31 August 2009
![Page 6: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/6.jpg)
Structured Assembler ProgrammingExample
; variable/data section
ORG RAMStart
; Insert here your data definition.
; code section
ORG ROMStart
Entry:
LDS #RAMEND+1
CLI
MainLoop:
Spin: BRA Spin
;**************************************************************
;* Interrupt Vectors *
;**************************************************************
ORG $FFFE
DC.W Entry ; Reset Vector
Variables go here..
ProgramStart
ProgramInitialization
Main Program Body
Vector Table(more on this later)
6Monday, 31 August 2009
![Page 7: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/7.jpg)
Structured Assembler Programming
• The focus of structured assembly code is on readability.
Example
; Enable Port A with 0-3 for input and 4-7 for outputINIT: MOVB #%11110000,PortA
• With higher level languages such as C, the program flow is defined using various structures, such as:
• Functions• While / Repeat-until structures• For looping structures• If-Then-Else structures
7Monday, 31 August 2009
![Page 8: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/8.jpg)
Controlling the Program FlowFunctions
• A function in an assembler program is commonly referred to as a subroutine.
• A subroutine consists of a series of instructions, terminated by the RTS instruction.
• A subroutine is called using the JSR (jump-to-subroutine) instruction.• A subroutine is always preceded by subroutine or function header
containing the following information:• Title• Description, including relevant constants used, etc.• Inputs• Outputs• Author, Date, and Revision
8Monday, 31 August 2009
![Page 9: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/9.jpg)
Controlling the Program FlowFunctions
BEGIN
END
InitSCI(IN: OUT:)
InitSCI(IN: OUT:)
END
AMAP =1
SCIACR1 =0
SCIACR2 =1
.
.
. This would be the place for the “RTS”
command in the assembly code.
This would be replaced by “JSR InitSCI” in
the assembly code.
9Monday, 31 August 2009
![Page 10: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/10.jpg)
Controlling the Program FlowFunctions: Subroutine Example
…; Title: InitSCI; Description: This routine will initialise the SCI interface with the following parameters:; - 9600 Baud Rate; - No Parity; - 1 stop bit ; Inputs: None; Outputs: None; Author: Brett Wildermoth, 09/09/02, Rev 1.0InitSCI: BSET SCISR2,AMAP MOVB #0,SCIACR1 MOVB #0,SCIACR2
... RTS …
10Monday, 31 August 2009
![Page 11: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/11.jpg)
Controlling the Program FlowWhile-Do Structures
C syntax:while (A<10) { …}
A==10
BEGIN
END
Do something
F
Assembler syntax:
; while (A<10) BEGINLOOP: CMPA #10 BEQ wend;Do …;EndDo BRA LOOPwend:; while END
T
11Monday, 31 August 2009
![Page 12: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/12.jpg)
Controlling the Program FlowRepeat-Until Structures
Example: Using a repeat-until structure repeat a section of code
until the condition A>10 is no longer met.
…; Repeat BEGINLOOP: … CMPA #10 BNE LOOP; Until (i>10) Repeat END; END_WHILE
A>10
BEGIN
ENDF
T
Do something
12Monday, 31 August 2009
![Page 13: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/13.jpg)
Controlling the Program FlowFor-loop Structures
Example
; FOR (B=10;B>0;B--) LDAB #10 LOOP: … DBNE B,LOOP ;END_FOR
B=10
B!=0
BEGIN
END
B=B - 1
Do something
TDBNEInstruction
Label “LOOP” refers to this point
on the flowchart
F
13Monday, 31 August 2009
![Page 14: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/14.jpg)
Controlling the Program FlowIf-Then-Else Structures
• The If-Then-Else structure is the most commonly used method of changing the flow of a program.
• If statements are used to change the flow of the program when a condition is met.
CHK: CMPB #10 ; IF (B==10)
BEQ THEN ; Else do this
ELSE: …
BRA END ; Then do thisTHEN: …
;End of IF
END:
B==10
Do something Do
something else
ElsePathThen
Path
14Monday, 31 August 2009
![Page 15: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/15.jpg)
Controlling the Program FlowIf-Then-Else Structures
CMPB #10 ;IF B<10 BGE CHK2 … ;Then do this BRA END-IFCHK2: CMPB #10 ;IF B==10 BNE CHK3 … ;Then do this BRA END-IFCHK3: CMPB #20 ;If B>20 BLE ELSE … ;Then do thisBRA END-IF ;Else do thisELSE: …END-IF: ;END of IF
B<10 Do thisCHK2
B==10 Or do this
B>20 Or do this
Otherwise do this
END-IF
CHK3
ELSE
15Monday, 31 August 2009
![Page 16: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/16.jpg)
Defining Data and Data Types in Assembler
• Data in assembler programs falls into two possible categories, either it is a constant or a variable.
• Constants can not be altered during normal program execution and remain at a constant value throughout the programs life.
• Variables on the other hand do not remain constant, they can be change by the program at any given time.
• The way constants and variables are defined in an assembler program depends on both the assembler directives used and the memory location in which the value is stored.
• Constants are defined using DC assembler directive and reside in the ROM region of memory, directly following the program.
• Variables on the other hand are defined using DCB and DS assembler directives and reside only in the RAM region of memory.
ORG RAMSTARTData: DS 1
;Following ProgramNAME: DC.B ‘Timmy’
Single Byte Variable ExampleString Constant Example
16Monday, 31 August 2009
![Page 17: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/17.jpg)
Defining Data and Data Types in AssemblerConstants
– Strings: A string is made up of a series of characters, used mainly for display purposes.For example:
STR: DC.B ‘The temperature is’
– 8 bit constants: A constant value in the range 0 to 255 or –127 to 127.For example:Delay: DC.B 20Delay: DC.B @12Delay: DC.B $45
– 16 bit constants: A constant value in the range 0 to 65535 or –32767 to 32768.For example:Delay: DC.B 20 DC.B 01Delay: DC.B 20,10Delay: DC.W $2345
17Monday, 31 August 2009
![Page 18: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/18.jpg)
String ExampleMainLoop:
JSR InitSCI LDX #Str
Loop: LDAB 0,X CMPB #0 BEQ Spin JSR PutChar INX BRA Loop
Spin: BRA Spin;Base Address for SCI1Serial_Base EQU $00D0 ;Add serial routines INCLUDE 'Serial.inc' ; Constants go here.Str DC.B 'Hello World',$00
B==0
BEGIN
END
InitSCI(IN: OUT:)
X=Str
B=M[X]
X=X+1
PutChar(IN: B OUT:)
18Monday, 31 August 2009
![Page 19: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/19.jpg)
Defining Data and Data Types in AssemblerVariables / Arrays
Single Variable Example ORG $RAMSTARTSingle: DS 1 ;a single 8bit variable locationDouble: DS 2 ;a double 8bit or single 16bit variable location.
Array Example ORG $RAMSTART
Array: DS 20 ; an array consisting of 20 bytes.
Accessing the ArrayLDX #ArrayLDAA 0,X ;Load array[0].LDAA 1,X ;Load array[1].STAA 19,X ;Save to array[19]
19Monday, 31 August 2009
![Page 20: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/20.jpg)
Lookup TablesPreinitialised Arrays
Examples of lookup tables:
kchar: DC.B ‘123A456B789C 0.#’
kchar: DCB $41 DC.B $42 … DC.B $23
20Monday, 31 August 2009
![Page 21: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/21.jpg)
Defining Data and Data Types in AssemblerVariables: Static or Dynamic
; variable/data section ORG RAMStart ; Insert here your data definition.Buffer DS 200
...mainLoop:
JSR InitSCILDAA #0LDX #Buffer
LOOP: JSR GetCharSTAB 0,XINXINCACMPA #200BNE LOOP
Spin: BRA SpinSerial_Base EQU $00D0 ;Base Address for SCI1 INCLUDE 'Serial.inc' ;Add serial routines
21Monday, 31 August 2009
![Page 22: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/22.jpg)
Defining Data and Data Types in AssemblerVariables: Static or Dynamic
ExampleOur program grabs a character from the serial port and stores it in consecutive memory
locations starting at $2000 until a ‘CR’ character is detected.
MainLoop: JSR InitSCI LDX #$2000
Loop: JSR GetChar STAB 0,X
INX CMPB #$OD BNE LOOP
Spin: BRA SpinSerial_Base EQU $00D0 ;Base Address for SCI1 INCLUDE 'Serial.inc' ;Add serial routines
The string that is being stored at $2000 can be of any length and the size changes each time the program is executed.
22Monday, 31 August 2009
![Page 23: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/23.jpg)
Debugging• Debugging software running on a micro-
controller can be achieved using many different approaches including:• In Circuit Emulator (ICE)•ROM monitor systems•Breakpoints•Background Debugger• JTAG•Logic Analyser
23Monday, 31 August 2009
![Page 24: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/24.jpg)
In-Circuit Emulator• The in-circuit emulator (ICE) was one of the most popular methods
used in embedded system debugging.• An in-circuit emulator is a device consisting of two components, an
emulator running on a host system and an adapter connected to the host at one end and sitting in place of the actual MCU at the other, providing all the functionality of the real MCU.
• The emulator attempts to completely replace the MCU, allowing the user the ability to stop the ICE at anytime and view the internal registers and memory of the emulated MCU at each individual clock cycle.
• The user has access to all the data provided on the inputs of the ICE and can trace the flow of data.
• The benefits of an ICE include:• Able to insert the debugger into the actual circuit and debugged programs in actual
application situations.• Allow the programmer the ability to debug MCUs which do not contain a built-in
debugger.
24Monday, 31 August 2009
![Page 25: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/25.jpg)
In-Circuit Emulator
• The use of an ICE for debugging purposes is no longer worthwhile for the following reasons:– Modern day processors contained many pins at an extremely high pin density, making
in-circuit emulators difficult, if not impossible to manufacture.– The architecture of the modern processor is extremely difficult to emulate in real-time.– Many modern processors contain instruction pipelines and multiple buses, that operate
faster than any emulator ever could. – Delays caused by the emulator create more problems in debugging the system.– Connecting the emulator to the target via a cable consisting of many signal lines makes
the debugging process difficult.
• ICE debuggers are now a thing of the past since many prcoessors now contain on-chip debuggers.
25Monday, 31 August 2009
![Page 26: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/26.jpg)
ROM Monitor Systems
• Many modern day microcontrollers are shipped with a built-in debugger resident in the on-chip ROM area of the controller.
• The monitor communicates with the user through a terminal connected via an RS232 interface to the microcontroller.
• Monitor programs provide a prompt on the screen and many useful commands and built-in functions to aide in the development and testing of source code.
• The monitor allows a host computer to communicate with the processor.
• During development and testing of programs the debugger is used to load, execute and debug the source code.
• When debugging is complete, the monitor is overwritten by the developed source code.
26Monday, 31 August 2009
![Page 27: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/27.jpg)
Background DebuggerBackground Debug Module (BDM)
• The BDM module has replaced the need for an in-circuit emulator.• Using a BDM pod is it possible to read and write to the memory of the target
system, view and modify registers in the target system and also trace a single instruction at a time.
• The BDM setup consists of single wire connection between the BDM pod and the target system, the connection is made to the BGND pin on each of the MCUs.
27Monday, 31 August 2009
![Page 28: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/28.jpg)
JTAGJoint Test Action Group
• Common name for the IEEE 1149.1 standard titled Standard Test Access Port and Boundary-Scan Architecture.
• Allows for easy programming and debugging of Target platforms.
28Monday, 31 August 2009
![Page 29: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/29.jpg)
JTAGJoint Test Action Group
• Every JTAG enabled device contains
• Four dedicated JTAG pins (TMS TCK, TDO, TDI)
• Every pin on the device is connected to a single bit buffer.
• All single bit buffers are daisy-chained together from TDI to TDO.
• The values in these buffers can be serially read and modified.
Figure from Texas Instruments JTAG Primer.
3-4 Boundary-Scan Architecture and IEEE Std 1149.1
InputPins
OutputPins
TDI
TMS
TCK
TDO
TAP
User DataRegister
BypassResister
InstructionRegister
Note: The boundary-scan register is shifted TDI to TDO.
BS
CB
SC
BS
CB
SC
BS
C
BS
C
BS
C
BS
C
BS
C
OE
Core Logic
Boundary-Scan Register
Figure 3-2. Boundary-Scan Architecture
29Monday, 31 August 2009
![Page 30: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/30.jpg)
Logic Analyzers
• As time has progressed, electronic systems have become larger and more complex.
• The oscilloscope is a priceless tool for analyzing electronic circuits. However, the oscilloscope has it limitations:
• The wave form must be repetitive for it to be displayed properly.• Oscilloscopes are limited to only a small number of inputs, with four being the
largest number of inputs found on an oscilloscope.
• Due to these limitations the oscilloscope is not the best tool for analyzing digital or µP circuitry.
• To successfully analyze a digital circuit built around an 8 bit µP, a device would be required that would be capable of monitoring 8 data lines, 16 address lines and several control lines, such a device is known as a logic analyzer.
30Monday, 31 August 2009
![Page 31: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/31.jpg)
Logic Analyzers
• A basic logic analyzer system consists of many probes, which are connected to the electrical signals to be monitored on the µP, a pod which interfaces the probes to the logic analyzer, and of course the logic analyzer itself.
• Each digital signal (0 or 1) is connected to a separate channel on the logic analyzer. The logic analyzer continually samples each channel and writes the data to its internal memory until a trigger event causes it to stop.
31Monday, 31 August 2009
![Page 32: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/32.jpg)
Logic Analyzers• The logic analyzer can monitor the signals on many lines
simultaneously. The data can be viewed in one of two possible ways:– The data or addresses can be displayed as binary, octal, or hexadecimal numbers.– Or the data can be displayed as a timing diagram, where each signal forms a line
across the screen represented by a series of pulses.
32Monday, 31 August 2009
![Page 33: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/33.jpg)
Logic Analyzers• Both an oscilloscope and a logic analyzer rely on a trigger event, on an oscilloscope
the signal can only be viewed after a trigger has occurred. The logic analyzer however allows the signal to be viewed before and after a trigger has occurred.
33Monday, 31 August 2009
![Page 34: Topic03D Using Assembly in the Freescale MC9S12X](https://reader034.vdocuments.site/reader034/viewer/2022042509/552e02a84a7959065d8b4819/html5/thumbnails/34.jpg)
Need Further Assistance?
• Ask your Demonstrator,
• Post a question on the Forum,
• Email the Convener, or
• Make an appointment.
34Monday, 31 August 2009