Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 1 Introduction Text Editor Program Ex. DOS, Notepad, Word saved as ASCII Source Code Assembler or Cross-Assembler Object Code Machine code + other information Listing file - Linker Hex File Linker combines one or more object file to generate hex file Loader Binary File Executable Form ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 2
Introduction Cross-assembler runs on a system using one type of computer and it assembles the source code for a different processor Motorola AS11 Cross-Assembler runs on an Intel 80x86 based operating system => produces object code for Motorola 68HC11 ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 3 Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 4
Format and Source Code The source code must follow very definite rules in order for an assembler to assemble it properly Each line of source code is organized into fields (or columns) Label Operation Operand Comments Identifier Must begin in the first column Other fields may start in any other column Opcode (mnemonic) or Pseudo-op Identifies the data or an address comment starting in column 1 must start with an * otherwise with ; ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 5 Format and Source Code * FILE list4a.asm * demo source file * ---------------------------------------------------------- * MAXIMUM UNSIGNED BYTE * Find the largest unsigned byte in a block of data * * DATA STRUCTURE * MAXRESULT Address where result is stored * COUNT Address that contains number of * bytes in data block * FIRST Address of first element in block * * ALGORITHM * 1. Get COUNT * 2. Set MAX = 0, the current maximum * 3. Compare each element to MAX * If element > MAX then MAX = element * Else MAX unchanged * 4. Repeat step 3 for all elements * 5. Put result, MAX into MAXRESULT * ======================================== ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 6
Format and Source Code * Define data and segment addresses PAGE0 EQU 0 STACK EQU $FF PROGSEG EQU $0100 * -------------------------------------------------- * start data segment * want page0 RAM to force direct addressing mode * whenever possible ORG PAGE0 RAM RMB 4 MAXRESULT RMB 1 COUNT RMB 1 * for convenience of testing, predefine some RAM data FIRST FCB $5C,$67,$00,$31,$B3,$A5,$20,$80 FCB $88,$3C,$91,$91,$43,$CE,$34,$01 * end data segment ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 7 Format and Source Code * ---------------------------------------------------------- * Program Segment * ---------------------------------------------------------- ORG PROGSEG LDS #STACK;initialize start of stack LDAB COUNT ;get count CLRA ;maximum=zero(min. possible) LDX #FIRST ;point to first entry in block MAXM CMPA 0,X ;is current maximum greater * ;than current entry? BCC NOCHG ;if yes, keep maximum LDAA 0,X ;else replace with current entry NOCHG INX ;point to next entry DECB ;all entries checked? BNE MAXM STAA MAXRESULT ;save maximum LDX #MAXRESULT-2 TAB ;set up calling registers JSR UNPACK ;unpack result HERE BRA HERE ;stop program ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 8
Format and Source Code * ---------------------------------------------- * Subroutine UNPACK (Same as Listing 3.5) * Converts a byte into two single-hex-digit bytes (MSD=0) * Stores the unpacked result in 2 bytes in RAM * Calling Registers * ACCB = data byte to be unpacked * IX = address to store unpacked result (MSD first) * Return Registers * All registers unaffected UNPACK PSHA ;store ACCA TPA ;store CCR PSHA TBA ;shift upper digit into lower nibble LSRA LSRA LSRA LSRA STAA 0,X ;and store it TBA ;next, ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 9 Format and Source Code INX ;work on lower digit STAA 0,X ;first store both digits BCLR 0,X $F0 ;then remove upper digit DEX ;restore IX PULA ;restore CCR TAP PULA ;restore ACCA RTS ;return to calling routine * end of program segment * -------------------------------------------------------- END ;(optional) end of source code, * ;assembler will not assemble * ;anything AFTER this point. * -------------------------------------------------------- ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 10
Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 11 Code and Data Segments The source code can be organized into blocks called segments Should have separate segments for code, data and special areas such as vectors, look-up tables, subroutine codes, and the stack AS11 has no segments the assembler does the linker s job Memory Type RAM EEPROM ROM Page 0 Calibration Table Application Program Utility Subroutine Interrupt and Reset Handler Routine Vectors Reserved Data Initial SP ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 12
Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 13 Pseudo-Operations Pseudo-operation tells the assembler what to do Examples of pseudo operations from AS11 RMB reserve memory bytes FCB form constant byte FDB form double byte FCC form constant characters Fill, BSZ, and ZMB these are directives to define a block of bytes to Other cross-assembler pseudo-ops ORG DB define byte DW define word EQU equal DS define space ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 14
* Listing 4.2 * Sample data segment to demonstrate data definition * pseudo-ops * * start data segment * ---------------------------------------------------------- DATASEG EQU $C000 ORG DATASEG * assigning values to symbols, can use decimal, hex or ASCII STACK EQU $FF NUM1 EQU 55 NUM2 EQU $55 NUM3 EQU 'U' * RESULT STACK=$FF, NUM1=$37, NUM2=$55, * NUM3=$55, ASCII code for 'U' * RMB reserves uninitialized bytes of space BEGINS RMB 4 ;assigned to address $C000 RESULTRMB 1 ;assigned to address $C004 COUNT RMB 1 ;assigned to address $C005 ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 15 * FCB defines 8-bit quantities * can use decimal, hex, or ASCII BEGIN FCB 50,$50,'5' FCB 76,$76 * RESULT BEGIN assigned to address $C006 * hex code C006 32 50 35 4C 76 ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 16
* FCC (Form Constant Characters) can define a string * a string is a sequence of bytes representing ASCII * characters STRING FCC "Hello my friend" CR EQU $0D LF EQU $0A FCB CR, LF * add carriage return (CR) and line feed (LF) to string * RESULT STRING assigned to address $C00B * hex code C00B 48 65 6C 6C * C00F 6F 20 6D 79 * C013 20 66 72 69 * C017 65 6E 64 0D * C01B 0A ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 17 * FDB defines 16-bit quantities FDB FDB FDB $6,6,$54C3,'x' 'y' 'z' * RESULT * hex code C01C 00 06 00 06 * C020 54 C3 00 78 * C024 00 79 00 7A * demonstrate prefix convention HEXP FCB $FB ; hex DECP FCB 251 ; decimal OCTP FCB @373 ; octal BINP FCB %11111011 ; binary * RESULT * hex code C028 FB FB FB FB ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 18
* demonstrate signed numbers and arithmetic FCB $FB UNSGN FCB 251 SGN FCB -5 ADD1 FCB 248+3 SUB1 FCB 254-3 * RESULT * hex code C02C FB FB FB FB FB * use labels or symbols as operands FCB NUM2 ;a symbol FDB STRING ;a label * RESULT note that label STRING was assigned address $C00B * hex code C031 55 C0 0B * end of data segment * ----------------------------------------------- ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 19 Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 20
The Assembly Two-Pass Process Pass 1: starts with the source code Assembler fills in all known address and hex code information line by li leaves spaces for any unknown information collects all possible information Pass 2: starts with the source code fills in the missing information left behind after the firs pass During the assembly process the assembler use a location counter to point to the line on which it is working ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 21 Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 22
Assembler Options and Preprocessor Directives Avocet assembler options and preprocessor directives Options must begin in column 1 with a $ sign must occur at the beginning of the source code Preprocessor directives follow the assembler options must begin with a % tell the assembler to do something before beginning the assembly process ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 23 ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 24
ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 25 ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 26
ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 27 Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 28
Hex and Binary Files Linkers produce a hex file Hex files have standard formats Motorola S-record Intel hex Applications are first developed and tested using evaluation boards Motorola hex file => EVBU board The EVBU board has a built-in program in ROM that converts the hex file to binary After the application is tested it can be written in the ROM by the semiconductor manufacturer ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 29 Hex and Binary Files Data records S1 Two-character data byte count (Number of data bytes plus three) Four-character load address Data byte characters Two-character checksum Carriage return Linefeed The end record S9 Count, 03 Four-character start address Two-character checksum Carriage return Linefeed ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 30
Hex and Binary Files Intel Hex Format Data records : Two-character data byte count Four-character load address Record type, 00 Data byte characters Two-character checksum Carriage return Linefeed The end record : Two character count Four-character start address Record type, 01 Two-character checksum Carriage return Linefeed ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 31 Hex and Binary Files Checksum technique used by the hex files can detect errors but cannot correct them Motorola hex format checksum one s complement of the 8-bit sum of the data bytes, byte count, and address bytes Intel hex format checksum two s complement of the 8-bit sum of all the data bytes, byte count, record type, and address bytes ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 32
Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 33 Documentation Files Listing file Cycle count Symbol listing ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 34
Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 35 Simulation Instead of running a program in the actual 68HC11 CPU you can run it using a computer simulator that simulates the CPU With simulation you can freely modify your program, modify registers, modify data and set breakpoints A simulator usually shows the information more conveniently and allows easier access to registers and memory for modification ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 36
Cross Assembly and ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 37 Evaluation Boards and Emulation ENGG4640/3640; Fall 2004; Prepared by Radu Muresan 38