CodeWarrior Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 1 Assembler • An “assembler” is a program that translates “assembly language” into “machine code”. – “Machine code” are the numbers that the CPU recognizes as instructions. $B6 $10 $00 – “Assembly language” is a mnemonic (symbolic) representation of machine code, purely for human reading. Assembly program Assembler (on PC) Machine code “Assembly time” or “compile time” Microcomputer Architecture and Interfacing Colorado School of Mines LDAA $1000 Target Hardware (microcontroller) “Run time” Professor William Hoff 2 CodeWarrior • CodeWarrior is an integrated development environment (IDE) – Free version from Freescale, Inc • Get this version: “Special Edition: CodeWarrior for HCS12(X) Microcontrollers (Classic).” • http://www.freescale.com/webapp/sps/site/overview.jsp?code=CW_SPECI ALEDITIONS&fsrch=1&sr=1&pageNum=1. • Has editor, simulator; interfaces to target hardware, debugger – Assembler and C compiler • We will use in class and lab • Tutorial in section 3.8 in the book • Approach – You create a “project” – This organizes all your files Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 3 Example • Start Codewarrior • On startup screen, choose “Create New Project” • On next screen – Select HCS12->HCS12C Family->MC9S12C32 – Select “Full Chip Simulation” – Hit Next • On next screen – Uncheck C, check “Absolute assembly” – Enter (or browse to) the new directory location for the project – Enter project name (you can leave it as “Project.mcp”) – Hit Finish Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 4 Template Assembly file • CodeWarrior automatically creates a sample “main.asm” file – This is a simple program to generate Fibonacci numbers – You can run this program as is, or modify it – It has some useful initialization stuff in there • We’ll first run it as is, then change it Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 5 Header comments main.asm Declarations Variables in RAM Initialization Main body of program Interrupt vectors Architecture and Interfacing Microcomputer Colorado School of Mines Professor William Hoff 6 Assemble and Debug • Select Project->Compile – If there are errors you will get a popup screen • Select Project->Debug – This will bring up the debugger window Shows assembly code with addresses Source code CPU registers Contents of memory Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 7 Running the template program • Hit “Single Step” to step through the program – Notice the registers changing • Notice count of CPU cycles • Right click in the Memory window and select “Address…” • Type 800 and hit OK Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 8 Simple Fibonacci Program • We’ll create a simple program to generate Fibonacci numbers – (Actually, the template program does this, but our program is simpler) • Fibonacci numbers are a sequence defined by Fn = Fn-1 + Fn-2 • where F0 = 0 and F1 = 1 • The sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... • We will have three memory locations labeled N1,N2,N3 • These correspond to Fn-2, Fn-1, Fn Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 9 Simple Fibonacci Program • Replace the lines Counter FiboRes DS.W DS.W 1 1 • with N1 N2 N3 DS.B 1 DS.B 1 DS.B 1 • • These define labels N1,N2,N3 “DS.B 1” means “define storage (bytes)” and the number of bytes allocated is 1 • Replace everything between “mainloop” and “RTS” (including those lines) with: movb movb #0,N1 #1,N2 ldaa adda staa N1 N2 N3 movb movb N2,N1 N3,N2 Initialize the first two Fibonacci numbers to be 0, 1 loop Labels must start in column 1 bra loop Microcomputer Architecture and Interfacing Add N1+N2, result in N3 Save N2 and N3 as the last two values generated Loop forever Colorado School of Mines Professor William Hoff 10 Simple Fibonacci Program • Compile and run debugger • Step through program and verify that the Fibonacci numbers are produced in N3: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... • When does overflow occur? • Number of clock cycles in the loop? loop – Can look up the count for each instruction, in the instruction set table – Or you can look at the count of CPU cycles in the simulator as you step through Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff ldaa adda staa N1 N2 N3 movb movb N2,N1 N3,N2 bra loop 11 Other Assembler Details • Comments – Start with semicolon – Ignored by assembler – Example ldaa N ; this is a comment • Syntax for numeric constants – $ indicates hexadecimal (e.g., $10 has decimal value 16) – % indicates binary value (e.g., %100000 has decimal value 32) – A number with no preceding symbol is decimal Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 12 Assembler Directives: “org”, “equ” • These are instructions for the assembler only, not the target hardware • These are used at “assembly time” • They are not instructions that will be performed at “run time” • org expr – Tells the assembler to set the load counter to expr, the value of the operand expression – Example org $1000 ldab #$FF – The next item to be placed in memory (the opcode byte for “ldab”) will go into $1000 • label equ expr – The label symbol is assigned the value of expr rather than the current load counter value – Does not allocate any space – Example loop_cnt equ $40 – From now on, whenever the symbol loop_cnt is seen, the assembler replaces it with $40 Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 13 ; Include derivative-specific definitions INCLUDE 'derivative.inc' ROMStart EQU $4000 ; absolute address to place my code/constant data ; variable/data section • Assemble “simple Fibonacci” program • Look at output list file (choose Project->Disassemble) ORG RAMStart ; Insert here your data definition. N1 DS.B 1 N2 DS.B 1 N3 DS.B 1 ; code section ORG Entry: _Startup: LDS ROMStart #RAMEnd+1 CLI ; initialize the stack pointer ; enable interrupts movb movb #0,N1 #1,N2 ldaa adda staa N1 N2 N3 movb movb N2,N1 N3,N2 bra loop loop ;************************************************************** ;* Interrupt Vectors * ;************************************************************** ORG $FFFE DC.W Entry ; Reset Vector Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 14 Assembler list file Memory Contents address or value : 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 5492 40 0000 4000 ROMStart EQU $4000 ; absolute address to place my code/constant data ; variable/data section ORG RAMStart ; Insert here your data definition. N1 DS.B 1 N2 DS.B 1 N3 DS.B 1 a000800 a000801 a000802 ; code section ORG ROMStart Notes: • DS.B just defines storage space; it doesn’t put anything into memory • “movb” has a two byte opcode • “cli” actually is implemented as “andcc #$FE” Entry: _Startup: a004000 CF10 00 LDS a004003 10EF CLI a004005 004009 a00400A 00400E movb #0,N1 movb #1,N2 180B 0008 00 180B 0108 01 Microcomputer Architecture and Interfacing #RAMEnd+1 ; initialize the stack pointer ; enable interrupts Colorado School of Mines Professor William Hoff 15 Assembler list file (continued) 5493 5494 5495 5496 5497 5498 5499 41 42 43 44 45 46 47 5500 48 5501 49 loop a00400F B608 00 a004012 BB08 01 a004015 7A08 02 ldaa adda staa N1 N2 N3 a004018 00401C a00401E 004022 movb N2,N1 movb N3,N2 bra loop 180C 0801 0800 180C 0802 0801 Freescale HC12-Assembler (c) Copyright Freescale 1987-2010 Abs. Rel. ---- ---5502 50 5503 51 5504 52 5505 53 5506 54 5507 55 5508 56 5509 57 5510 58 Loc Obj. code ------ --------a004024 20E9 a00FFFE 4000 Source line ----------- Notes: • The value of “loop” is $400F • The offset for “BRA” is $E9 or -23 decimal • The value $4000 is loaded into memory at $FFFE, $FFFF ;************************************************************** ;* Interrupt Vectors * ;************************************************************** ORG $FFFE DC.W Entry ; Reset Vector Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 16 Summary • The assembler translates assembly language into machine code. • There are special “assembler directives” that are used at assembly time. – Examples: org, equ, ds.b, ... – They tell the assembler where to load the machine code, how much storage to allocate, etc. • Codewarrior – An integrated development environment (IDE) – Includes editor, assembler, C compiler, simulator Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff 17