lecture3-4pg - Reading Assignment EEL 4744C Microprocessor...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Reading Assignment EEL 4744C: Microprocessor Applications Lecture 3 Part 1 • Microcontrollers and Microcomputers: Chapter 5 Or • Software and Hardware Engineering (new version): Chapter 2.4, Chapter 5.3, Chapter 6 Plus • CPU 12 Reference Manual: Chapters 1, 2, and 3 Assembly Language Programming and Debugging Dr. Tao Li 1 Dr. Tao Li 2 Assembly Language • Gives you a better appreciation of how the hardware works than high-level languages • Resources are given by the programmer’s model • These include registers, hardware resources in CPU, and memory used for data storage • Look through the instructions in a category, find the mnemonic for the required operation, then find the correct addressing mode Dr. Tao Li 3 The Assembler • Converts (assembles) a source file into binary codes (machine codes) executed by the computer – 1-to-1 correspondence between assembly language statements and the machine code in memory • Assembly source code fields: 4 of these – Label Field – Operation Code Field – Operand Field – Comment Field Dr. Tao Li 4 1 Assembly Source Code Fields • Label field – Optional and provides a symbolic memory reference – Represents the address of the first byte of an instruction or a data element – Also used to define constants – Usually starts with an alphabetic character, may contain digits and other characters Assembly Source Code Fields • Operation code field (continued) – In the following code example, the ORG (for origination) directive specifies where the code is located in memory – When assembler sees the ORG directive, it sets the value of current location counter to $1000 Use label to specify target address (relative address) ORG directive helps determine actual address • Operation code field – Contains either a mnemonic for the operation or an assembler directive or pseudo-operation – Mnemonics are assembled into code to be placed into memory – Assembler directives or pseudo-operations are instructions to direct the assembler how to do its job Dr. Tao Li 5 Dr. Tao Li 6 Assembly Source Code Fields • Operand field – Can be: names of registers, numeric/symbolic constants, labels, algebraic expressions to be evaluated by assembler – The following 2 instructions load the A, B registers from memory location DATA1 and DATA1+1 Assembly Source Code Fields • Assembler directives or pseudo-operations field – We have seen the ORG example – There are others to define symbols, provide data in memory locations, reserve memory locations for data storage, define macros, etc. • Comment field Label – Ignored during assembly – May have source code lines that are only comments or blank – For some assemblers, beginning of comment fields are denoted by “;” or “* ” characters Dr. Tao Li 7 Dr. Tao Li 8 Numeric constant 2 Macro Assembler • What is it? – An assembler in which frequently used assembly instructions can be collected into a single statement – It makes the assembler more like a high-level language Macros and Subroutines • Each time a macro is invoked, the assembler expands the macro “in line”. A subroutine code is included only once. So the macro’s make your program larger • A subroutine requires a call or JMP, a macro does not. So a macro is faster than a subroutine to run • Both allows reuse of code segments. Both make the program easier to read, and allow changes to be made in one place. Both hide details of the program so you don’t need to know how it is doing something but just what it is doing 9 Dr. Tao Li 10 Assembler directives • 3 stages of using a macro: macro definition, macro invocation, macro expansion Dr. Tao Li Two-Pass Assemblers • Allow symbols to be used before they are defined. This is called a forward reference • The assembler evaluates these symbols by making 2 passes through the source code • On the first pass, any symbol definitions it finds are recorded in a symbol table (containing values defined for symbols using the EQU directive, and the memory locations for labels, e.g. TARGET • On the second pass, the assembler uses the symbol table to substitute the values for the symbols Dr. Tao Li 11 Cross and Native Assemblers • Cross assembler: for microcontroller applications, different computers are often used to edit and assemble the programs. (i.e. cross platforms) • Native assembler: one that runs on the target processor • Assembler output: – Executable file or object file – Assembler listing (source code + assembled code) – Symbol table, cross-reference listing… Dr. Tao Li 12 3 Code Location Problem • Tied to the hardware’s specific memory map • In a dedicated application system, code is located in ROM (read-only, non-volatile); data is in RAM Code Location Problem • Absolute assemblers – The code on slide #5, where the ORG directive specifies the code location, can be assembled with an absolute assembler • Major disadvantage – The source file MUST contain ALL the source code to be in the program, i.e. all code must be assembled with any change Dr. Tao Li 13 Dr. Tao Li 14 Code Location Problem • Relocatable assemblers – Source code file need not have the complete program, nor does it need to have location information, or ORG statements – The program can be split into multiple source files and assembled separately, producing multiple object files. – These object files are then combined using a linker program – Code and data location information are supplied to the linker program to produce the final e xecutable file Code Location Problem • The linker program – Takes modules that have been assembled by a relocatable a ssembler, links them together, and locates all addresses – May also receive object files from a library. Libraries are collections of object files that have been preassembled and bound together by a l ibrarian program – Linker pulls from the library only those object files needed Dr. Tao Li 15 Dr. Tao Li 16 4 Code Location Problem Code Location Problem Dr. Tao Li 17 Dr. Tao Li 18 Code Location Problem • Loader program – Puts an executable file into the memory of a computer – Can take on many forms, e.g. OS as MS-DOS programs, a modem/downloader program (cross platforms), a program or system that burns the programmable ROM Code Location Problem • Load time – When the executable file produced at link time (relocatable assembler) or at assembly time (absolute assembler) is loaded into the memory of the computer – Using assembler directives, it is possible to initialize the variables in RAM at load time • Assembly time – Refers to data values that are known at the time of assembly – Includes constants (e.g. loop counters, ASCII character codes), operand expressions, absolute addresses and relative addresses (with relocatable assemblers) • Run time – Refers to the time when the program is running – Data elements are initialized and manipulated at run time – Addresses can also be initialized and manipulated at run time 19 Dr. Tao Li 20 • Linking time – Evaluate address/constants left by the r elocatable assembler Dr. Tao Li 5 Program Debugging • Debugging: the process of finding the clues and interpreting these clues to find the problem • 1st approach: synthesis – Try to fix the problem by changing the code somewhere – Wrong! Program Debugging • 2nd approach: analysis – Try to find out what your program is doing before any fixes – Right! • Every program has 2 parts: data, logic – Most program bugs occur in the logic Dr. Tao Li 21 Dr. Tao Li 22 Program Debugging • Choose an input data set • Predict what the program will do with the input data set at each step of the program, and what the program will do next (the program model) • Now run the program, and look for data values and program steps that differ from the prediction • Once we find out where the program deviates from the prediction, we are on track to finding the bugs and fixes for the bugs Debugging Program Flow/Logic • Program trace: stepping through the program one statement at a time. Values of registers are shown during each step. Data elements in memory have to be checked manually • Breakpoints: much like a high-level language debugger, e.g. Gdb. These cause program flow to be interrupted, and control transferred to the debugger (the user) • Breakpoints can be set at statements, as well as when certain conditions are met, e.g. data elements become some specific values Dr. Tao Li 23 Dr. Tao Li 24 6 Debugging Data Elements • Registers: state of the registers must be known at each step. Debugger usually will display contents of all the registers, including the CCR. Watch for any deviating register content values • Memory: usually are displayed in hexadecimal • You will most likely need a source code listing generated by the assembler, not the original source code file. And you will need an up-to-date one! • Debugging plan: no universal solutions, but in general make your code “modularly” Dr. Tao Li 25 EEL 4744C: Microprocessor Applications Lecture 3 Part 2 68HC12 Assembly Program Dr. Tao Li 26 Reading Assignment • Software and Hardware Engineering (old version): Chapter 3 Or • Software and Hardware Engineering (new version): Chapter 5.1, 5.4, 5.5, 5.6, Plus • M68HC12B Family Data Sheet: Chapter 1 0000 0000 0000 0000 0000 0800 Hello World! (Assembler Listing) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ; Example program to print "Hello World" ; Source File: hello.asm ; Author: F. M. Cady ; Created: 4/97 ; Constant equates E OS: EQU 0 ; End of string ; Debug-12 Monitor equates p rintf: EQU $FE06 ; Print a string ; Memory map equates P ROG: EQU $0800 ; RAM in the EVB D ATA: EQU $0900 ; Middle of RAM S TACK: EQU $0a00 ; Stack pointer O RG PROG ; Locate the program ; Initialize stack pointer l ds #STACK ; Print Hello World! string l dd # HELLO ; Pass the adr of the string l dx p rintf ; The a dr o f the printf j sr 0 ,x ; Return to the monitor s wi ; Define the string to print H ELLO: DC.B 'Hello World!',EOS Dr. Tao Li 28 0 800 CF0A00 0803 CC080C 0806 FEFE06 routine 0809 1500 080B 3F 080C 48656C6C 6F20576F 726C6421 00 Dr. Tao Li 27 7 Assembler Source Code Field • Label: see textbook section 5.3 for how to define labels – A whitespace c haracter is needed for a source code line without label 68HC12 Assembler Program • Assembler pseudo-ops: – ORG: set assembler’s location counter (e.g. ORG $1000) – EQU: equate symbol to value (e.g. COUNT EQU $10) – DS (define storage): reserve storage (e.g. ARRAY DS 10) – DC.B (define byte constant), DC.W (define word constant) : define constants in memory storage (e.g. DATA1 DC.B $10,$20,$30) Pseudo-ops help the assembler generate code for the program • Opcode field: mnemonic, assembler directive or pseudooperation, macro name • Operand field: symbols, constants, expressions (see textbook section 5.3 for detail – Special symbols: * and $ represent the current value of program counter – Constants: Decimal (default), Hexadecimal /w ( $) prefix, Binary /w (%) prefix, ASCII /w ‘ ’ and “ ” – Expressions: evaluated by assembler, used for specifying constant only • Comment field: ; or * (in column 1) Dr. Tao Li 29 Dr. Tao Li 30 Example: ORG 0000 0000 0000 F000 F 000 CF0A00 F003 B60800 0800 0 800 20 1 2 3 4 5 6 7 8 9 10 11 12 13 R OM: R AM: S TACK: ; EQU EQU EQU O RG $F000 $0800 $0a00 ROM ; Location of ROM ; Location of RAM ; Location of stack 0000 0000 0000 0000 0000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Example: EQU C RLF: C OUNT: C OUNT1: L SMASK: l s_mask: EQU EQU EQU EQU EQU ; For each occurrence of ; CRLF, the assembler will ; substitute the value $0D0A 5 ; Loop counters often need to ; be initialized COUNT*5 ; The assembler can evaluate ; an expression to provide ; a value of 25 for COUNT1 $0F ; A mask that picks off the ; least significant nibble ; in a byte %00001111; A binary mask equate is ; more readable and ; informative than one ; given in hexadecimal $0D0A ; Set program counter to ROM ; for the program ; The following code is located at memory address ROM l ds #STACK ; Initialize SP l daa D ATA_1 ; Load from memory address RAM ; --O RG RAM ; Set program counter to RAM ; for the data D ATA_1: DC.B $20 ; Set aside $20 bytes Any constant value can be defined for the assembler using the EQU Dr. Tao Li 31 Dr. Tao Li 32 8 Example: DS Directive Show how to use the DS directive to reserve 10 bytes for data. Initialize each byte to zero in a small program segment. 0000 0000 0000 0800 0800 C60A 0802 CE0900 0805 6930 0807 0431FB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 N UMBER: P ROG: R AM: ; EQU EQU EQU O RG --l dab l dx l oop: c lr d bne ; B UF: --O RG DS 10 $0800 $0900 PROG ; Number of bytes allocated ; Program location ; Location of RAM Initialize B with a loop counter X points to the start of the buffer Clear each location and point to the next location Decrement the loop counter and branch if the loop counter is not zero 68HC12 Assembler Program • Assembler directives: (see textbook Table 5-5 for complete list) – Exact syntax varies by assembler program used; most interesting options for conditional assembly and macros – Directives are invoked by placing a /, # or $ as the first character of a line followed by the directive and any parameters – Conditional assembly directives permit different sections of source code to be assembled depending on setting of condition; Assembler directives control How the assembler creates it output files 33 Dr. Tao Li 34 # NUMBER ; ; # BUF ; ; 1 ,x+ ; ; b ,loop ; ; ; RAM NUMBER 0900 0900 ; Locate the data area ; Allocate the data area Dr. Tao Li Example: Conditional Assembly 0000 0000 0000 0000 0000 8676 0002 0002 1 2 3 4 5 6 7 8 9 10 11 12 13 ; Set the version number for this software P ARAM1: EQU $76 ; Parameter to use for v ers 1 P ARAM2: EQU $77 ; Parameter to use for v ers 2 $ SET Ver1 ;... ; The conditional assembly follows. $ IF Ver1 ; This code is assembled if Ver1 has been SET. l daa # PARAM1 $ ELSEIF ; This code will be assembled if Ver1 has been SETNOT. l daa # PARAM2 $ ENDIF 68HC12 Assembler Program • Assembler directives: Macros – Assembler directives MACRO and MACROEND encapsulate the code – Macro parameters: specified by %n (n is the nth parameters) in macro definition – Labels in Marcos: A label may not occur more than once in a program. The assembler expands macro labels to make each label unique Dr. Tao Li 35 Dr. Tao Li 36 9 Example: MACRO Parameters 0000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ; Macro definition for a variable arithmetic ; shift left of the A register $ MACRO a lsa_n n um ; Shift the A register left num bits ; where n is a parameter in the macro call. ; Save B to set up a loop counter p shb ; Save B on the stack l dab # %1 l oop: a sla ; Shift the A r eg d bne b ,loop ; Decr and branch if ; not zero p ulb ; restore the B reg $ MACROEND ; ; ; The macro call is with a parameter a lsa_n 3 P SHB L DAB #%1 L OOP: ASLA D BNE B,LOOP P ULB Dr. Tao Li 37 Assembler Output Files • Assembler listing (includes a symbol table) and executable (S-Record file) – Assembler listing Loc [Cycles] Obj Code Line Source Line – Symbol table: lists all symbols used in a program and their values – Cross-reference table: shows where the symbols are defined and referenced Dr. Tao Li 38 0 000 0000 0000 0001 0003 0004 0007 macro 37 C603 48 0431FC 33 Example: Symbol Table 0000 0000 0000 0000 0000 0800 0800 CF0A00 0803 CC080C 0806 FEFE06 0809 1500 080B 3F 080C 48656C6C 6F20576F 726C6421 00 ……… 5 ; Constant equates 6 E OS: EQU 0 ; End of string 7 ; Debug-12 Monitor equates 8 p rintf: EQU $FE06 ; Print a string 9 ; Memory map equates 10 P ROG: EQU $0800 ; RAM in the EVB 11 D ATA: EQU $0900 ; Middle of RAM 12 S TACK: EQU $0a00 ; Stack pointer 13 ORG PROG ; Locate the program 14 ; Initialize stack pointer 15 lds #STACK 16 ; Print Hello World! string 17 l dd # HELLO ; Pass the adr of the string 18 l dx p rintf ; The a dr o f the printf routine 19 j sr 0 ,x Symbol Table 20 ; Return to the monitor DATA 0900 21 s wi 2 2 ; Define the string to print EOS 0000 23 H ELLO: DB 'Hello World!',EOS Dr. Tao Li HELLO PRINTF PROG STACK 080C FE06 0800 0A00 39 10 ...
View Full Document

This note was uploaded on 11/13/2011 for the course EEL 4744c taught by Professor Staff during the Fall '09 term at University of Florida.

Ask a homework question - tutors are online