Writing and Assembling M8C Code - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Writing and Assembling M8C Code

Description:

AREA tells the assembler where and how to locate the code or data it produces. ROM or RAM ... AREA bss(RAM) //defaults to ABS. AREA text(ROM,REL) // code space ... – PowerPoint PPT presentation

Number of Views:423
Avg rating:3.0/5.0
Slides: 13
Provided by: kevinb7
Category:
Tags: m8c | area | assembling | code | writing

less

Transcript and Presenter's Notes

Title: Writing and Assembling M8C Code


1
Writing and Assembling M8C Code
  • Assembly Language source files contain
  • Included files with common definitions and
    headers for system calls
  • Custom definitions for constants and macros
  • Declaration of variables in SRAM
  • Assembly language code
  • Assembler Directives
  • Separate out the different sections (above)
  • Define variables
  • Define constants and macros
  • Generally help the programmer

2
Comments and Labels
  • Comments follow either or //
  • ADD A,3 increment counter by 3
  • MOV 13,22 // copy total into return
    value
  • Labels define reference points
  • All labels end in
  • Case sensitive, may include _
  • Start _end JumpTable
  • Labels can be called only from the source file
    that defines them unless Exported
  • EXPORT _main // main can be called from any
    file
  • Reusable labels begin with a . and can be
    reused in a source file
  • .loop .end
  • The closest occurring instance is used

3
Defining Areas
  • AREA tells the assembler where and how to locate
    the code or data it produces
  • ROM or RAM
  • Instructions go in ROM, while variables go in RAM
  • ABS (Absolute) or REL (Relocatable)
  • The assembler may move around REL code
  • ABS code is fixed in place with an ORG directive
  • CON (Concatenate) or OVR (Overlay)
  • Used with overlays, which will be discussed later
  • Examples
  • AREA bss(RAM) // RAM for variables, default ABS
  • AREA text(ROM,REL) // code space
  • AREA lit(ROM,REL) // space for constants such as
    strings

4
Defining variable space
  • Variables reside in the bss Area of RAM
  • BLK Defines space (in bytes) for variables
  • AREA bss(RAM)ADCValue BLK 2 // 2 byte
    variable for ADCCounter BLK 1 // 1
    byte variable for counter
  • Variables can be referenced by label in code
    portion
  • If variable should be accessed by code in other
    source files, be sure to EXPORT it
  • BLKW Defines space (in 16-bit words) for
    variables
  • AREA bss(RAM)Result BLKW 1 // 1 word
    variable for result

5
Areas predefined in boot.asm
--------------------------------- Order
Critical RAM ROM AREAs --------------------
------------- 'TOP' is all that has been
defined so far... ROM AREAs for C CONST,
static global items AREA lit
(ROM, REL, CON) 'const' definitions
AREA idata (ROM, REL, CON)
Constants for initializing RAM __idata_start
AREA func_lit (ROM, REL, CON)
Function Pointers __func_lit_start AREA
psoc_config (ROM, REL, CON)
Configuration Load Unload AREA UserModules
(ROM, REL, CON) User Module APIs
CODE segment for general use AREA text
(ROM, REL, CON) __text_start RAM area
usage AREA data (RAM, REL,
CON) initialized RAM __data_start AREA
virtual_registers (RAM, REL, CON) Temp vars
of C compiler AREA InterruptRAM (RAM,
REL, CON) Interrupts, on Page 0 AREA bss
(RAM, REL, CON) general
use __bss_start
lit area for constants in ROM
text area for your code
bss area for RAM varables
6
Defining String Constants in ROM
  • ROM Constants are often used for strings for user
    I/O
  • Usually placed in lit area
  • ASCIZ Null-Terminated ASCII String
  • PromptStr ASCIZ Please enter a number
  • ResultStr ASCIZ Result
  • Most PSoC APIs that use strings require
    NULL-terminated strings
  • DS Define String (not NULL-terminated)
  • LabelStr DS Label

7
Defining Data Constants in ROM
  • Data can be defined in ROM
  • Example A table of values to output to a DAC to
    define a sine wave
  • DB Define Byte
  • TopGrade DB 100
  • FibTable DB 1, 1, 2, 3, 5, 8, 13, 21, 34
  • DW Define 16-bit Word
  • StockValue DW 11232
  • MAX DW 0xFFFF

8
Equating Constants
  • Equates make it easier to write code
  • Whenever the equated value is found, its numeric
    value is substituted
  • EQU - Equate
  • StartYear EQU 2002
  • IncValue EQU 5
  • Equated labels may be used anywhere a number is
    needed

9
Include Directive
  • When using system calls, header files provide
    important info
  • Equates and Macro definitions for using system
    resources
  • Header files are included with the INCLUDE
    directive
  • INCLUDE ADCINC12_1.inc
  • INCLUDE c\PSOC\Project1\project1hdr.inc

10
Origin Directive
  • Most code and data can be relocated
  • Definitions and references always use labels,
    rather than absolute values
  • The assembler/linker decides where it is best to
    put values in memory
  • Data or Code may be fixed in memory with the ORG
    directive
  • Only works in ABS Areas
  • AREA tables(ROM,ABS)ORG 0x9000 // set the
    current address to 9000DB 18, 20, 21, 13, 14,
    15, 18, 29// values at addresses 0x9000-9007

11
Code Example
This area must be named textYour code goes
here.
///////////////// Code area //////////////////////
// area text area for code export
_main _main Insert your main assembly code
here. mov counter,0 again mov A,0 loop
cmp A,32 jz exit inc A jmp
loop outerlp inc counter cmp
counter,10 jz exit jmp again exit jmp
exit //////////////// Constant area
/////////////////////// area lit area for
constants promptStr ASCIZ Hello World!
/////////// PSoC Project /////////////// part
specific constants and macros include "m8c.inc"
include "memory.inc" PSoC API
definitions for all User Modules include
"PSoCAPI.inc" /////////// Variable
storage area ////////////// area bss counter
BLK 1
_main Entry point
Always include these three files
Some codeNote how labels and variablesare used.
This area must be named bssAll variables in
RAM should belisted here.
This area usually named litYour constants go
here.
12
include ltm8c.hgt / part
specific constants and macros / include
"PSoCAPI.h" / PSoC API
definitions for all User Modules / int
iResult / ADC result
variable / void main(void)
UART_1_Start(UART_PARITY_NONE) / Enable
UART / UART_1_CPutString("Example
ADC_UART_LCD") // Example string /
UART_1_PutCRLF() PGA_1_Start(PGA_1_MEDP
OWER) / Turn on PGA power /
ADCINC12_1_Start(ADCINC12_1_MEDPOWER) / Turn on
ADC power / ADCINC12_1_GetSamples(0)
/ Sample forever / LCD_1_Start()
/ Init the LCD /
LCD_1_Position(0,0) LCD_1_PrCString("PSoC
LCD") M8C_EnableGInt
/ Enable Global interrupts / while
(1)// Main loop if
(ADCINC12_1_fIsDataAvailable() ! 0) / If ADC
sample is ready... / iResult
ADCINC12_1_iGetData() 2048 / Get result,
convert to unsigned and clear flag /
ADCINC12_1_ClearFlag()
UART_1_PutSHexInt(iResult) / Print result to
UART / UART_1_PutCRLF()
/ Tack on a CR and LF /
LCD_1_Position(1,0) / display result
on LCD in hex and as a bar graph /
LCD_1_PrHexInt(iResult)
include "m8c.inc" include m8c
specific declarations include "psocapi.inc"
include User Module API specific
declarations export _main area bss(RAM)
inform assembler that variables
follow iResult blk 2 ADC
result variable area text(ROM,REL)
inform assembler that program code
follows _main mov A, UART_PARITY_NONE
lcall UART_1_Start Enable UART
mov A, gtsRomString1 mov X, ltsRomString1
lcall UART_1_CPutString Display example
string lcall UART_1_PutCRLF mov
A, PGA_1_MEDPOWER lcall PGA_1_Start
Turn on PGA power mov A,
ADCINC12_1_MEDPOWER lcall ADCINC12_1_Start
Turn on ADC power mov A, 0 lcall
ADCINC12_1_GetSamples Sample forever
lcall LCD_1_Start Init the LCD
mov A, 0 row mov X,
0 column lcall
LCD_1_Position mov A, gtsRomString2 mov
X, ltsRomString2 lcall LCD_1_PrCString
Display string M8C_EnableGInt
Enable Global interrupts loop
lcall ADCINC12_1_fIsDataAvailable If conversion
complete.... jz loop lcall
ADCINC12_1_iGetData Get result, convert to
unsigned and clear flag mov iResult1, A
mov iResult0, X add iResult0,
0x08 add 0x0800 to result lcall
ADCINC12_1_ClearFlag mov A,
iResult1 mov X, iResult0 lcall
UART_1_PutSHexInt Print result to UART
lcall UART_1_PutCRLF Tack on a CR and
LF mov A, 1
row mov X, 0 column
lcall LCD_1_Position display result
in hex mov A, iResult1 mov X,
iResult0 lcall LCD_1_PrHexInt
jmp loop area lit sRomString1 DS
"Example ADC_UART_LCD" db 00h sRomString2 DS
"PSoC LCD" db 00h area text
(0078) (0079) include ltm8c.hgt
/ part specific constants and macros
/ (0080) include "PSoCAPI.h"
/ PSoC API definitions for all User Modules
/ (0081) (0082) define RESOLUTION 12
/ ADC resolution / (0083) define
SCALE_BG (( 1 ltlt RESOLUTION)/55) / BarGraph
scale factor / (0084) (0085) int iResult
/ ADC result variable
/ (0086) (0087) void main(void) _main__text_sta
rt_main bgPos --gt X0
0861 10 PUSH X 0862 4F MOV
X,SP 0863 38 01 ADD SP,0x1 (0088)
(0089) BYTE bgPos
/ BarGraph position / (0090) (0091)
UART_1_Start(UART_PARITY_NONE) / Enable
UART / 0865 10 PUSH X 0866 50
00 MOV A,0x0 0868 7C 03 96 LCALL
0x0396 086B 20 POP X (0092)
UART_1_CPutString("Example ADC_UART_LCD") //
Example string / 086C 10 PUSH X
086D 50 01 MOV A,0x1 086F 08
PUSH A 0870 50 59 MOV A,0x59 0872
5C MOV X,A 0873 18 POP A
0874 7C 04 8F LCALL 0x048F (0093)
UART_1_PutCRLF() 0877 7C 04 A1 LCALL
0x04A1 087A 20 POP X (0094)
(0095) PGA_1_Start(PGA_1_MEDPOWER)
/ Turn on PGA power / 087B 10 PUSH
X 087C 50 02 MOV A,0x2 087E 7C 04
AA LCALL 0x04AA 0881 20 POP X (0096)
ADCINC12_1_Start(ADCINC12_1_MEDPOWER) /
Turn on ADC power / 0882 10 PUSH X
0883 50 02 MOV A,0x2 0885 7C 08 0B
LCALL 0x080B 0888 20 POP X (0097)
ADCINC12_1_GetSamples(0) / Sample
forever / 0889 10 PUSH X 088A
50 00 MOV A,0x0 088C 7C 08 1F LCALL
0x081F (0098) (0099) LCD_1_Start()
/ Init the LCD / 088F 7C
05 DF LCALL 0x05DF 0892 20 POP
X (0100) // LCD_1_InitBG(LCD_1_SOLID_BG) (0101
) LCD_1_Position(0,0) 0893 10
PUSH X 0894 50 00 MOV A,0x0 0896
5C MOV X,A 0897 7C 06 4F LCALL
0x064F 089A 20 POP X (0102)
LCD_1_PrCString("PSoC LCD") 089B 10
PUSH X 089C 50 01 MOV A,0x1 089E
08 PUSH A 089F 50 50 MOV A,0x50
08A1 5C MOV X,A 08A2 18 POP
A 08A3 7C 04 DE LCALL 0x04DE 08A6 20
POP X (0103) (0104) M8C_EnableGInt
/ Enable Global interrupts
/ 08A7 71 01 OR F,0x1 08A9 80 3E
JMP 0x08E8 (0105) (0106) while
(1)// Main loop (0107) (0108) if
(ADCINC12_1_fIsDataAvailable() ! 0) / If ADC
sample is ready... / 08AB 10 PUSH X
08AC 7C 08 55 LCALL 0x0855 08AF 20
POP X 08B0 39 00 CMP A,0x0 08B2
A0 35 JZ 0x08E8 (0109) (0110)
iResult ADCINC12_1_iGetData() 2048 /
Get result, convert to unsigned and clear flag
/ 08B4 10 PUSH X 08B5 7C 08 58
LCALL 0x0858 08B8 5A 00 MOV __r0,X
08BA 20 POP X 08BB 01 00 ADD
A,0x0 08BD 53 0B MOV iResult1,A
08BF 51 00 MOV A,__r0 08C1 09 08
ADC A,0x8 08C3 53 0A MOV
iResult,A (0111) ADCINC12_1_ClearFlag(
) 08C5 10 PUSH X 08C6 7C 08 5D
LCALL 0x085D 08C9 20 POP X (0112)
(0113) UART_1_PutSHexInt(iResult
) / Print result to UART / 08CA 10
PUSH X 08CB 58 0A MOV X,iResult
08CD 51 0B MOV A,iResult1 08CF 7C
04 01 LCALL 0x0401 (0114)
UART_1_PutCRLF() / Tack on a CR
and LF / 08D2 7C 04 A1 LCALL 0x04A1
08D5 20 POP X (0115) (0116)
LCD_1_Position(1,0) / display
result on LCD in hex and as a bar graph /
08D6 10 PUSH X 08D7 57 00 MOV
X,0x0 08D9 50 01 MOV A,0x1 08DB 7C
06 4F LCALL 0x064F 08DE 20 POP
X (0117) LCD_1_PrHexInt(iResult)
08DF 10 PUSH X 08E0 58 0A MOV
X,iResult 08E2 51 0B MOV
A,iResult1 08E4 7C 05 13 LCALL 0x0513
08E7 20 POP X (0118) // bgPos
(BYTE)(iResult/SCALE_BG) (0119) //
LCD_1_DrawBG(1, 5, 11, bgPos) (0120)
(0121) 08E8 8F C2 JMP 0x08AB
08EA 38 FF ADD SP,0xFF 08EC 20
POP X (0122)
C Code 36 lines
ASM Code 70 lines
Code Comparison Code to sample ADC anddisplay on
LCD and UART
ASM generated by C compiler126 lines
Write a Comment
User Comments (0)
About PowerShow.com