assembly language for the freescale 9s12 and...

38
Assembly Language for the Freescale 9S12 and CodeWarrior Microprocessor Applications for Engineering University of Hartford, CETA Fall 2009 Prof. Jonathan Hill [email protected]

Upload: vuonglien

Post on 22-May-2018

228 views

Category:

Documents


1 download

TRANSCRIPT

Assembly Language for the Freescale 9S12 and CodeWarrior

Microprocessor Applications for EngineeringUniversity of Hartford, CETA

Fall 2009

Prof. Jonathan [email protected]

9S12 ArchitectureThe architecture of a microprocessor includes everything that the assembly language programmer is aware of.

The 9S12 has several CPU registers

● One 16 bit program counter – PC● Two 8 bit registers – A, B● A, B form one 16 bit register – D ● Two 16 bit index registers – X, Y● One 16 bit stack pointer – S or SP● One 8 bit condition flags register – C, CR, CC, or CCR

Condition Register● Each instruction executes completely and then is mostly

forgotten by the CPU● The CPU has a single entity which performs all arithmetic

and logic operations, the ALU● The CR uses flags to summarize some past events and

certain settingsH: Half carry flag for BCD additionN: Negative resultZ: Zero resultV: Two’s complement overflowC: Carry out or unsigned overflowS: Stop instruction disable flag

X, I: Flags used with interrupts, leave ‘1’ for now to disable● All these flags are the topics of future lectures

9S12 Architecture7 0Accumulator A 7 0Accumulator B

15 0Double Accumulator D

15 0Index Register IX

15 0Index Register IY

15 0Stack pointer

15 0Program Counter

A:B

D

IX

IY

PC

SP

S X H I N Z V C CCR

Carry

Overflow

Negative

Zero

I interrupt mask

Half-Carry (from bit 3)

X Interrupt Mask

Stop Disable

Figure 1.3 MC68HC11 Programmer's model

Memory MapsA processor access memory and peripherals, each by address. A ‘memory map’ summarizes the relationship between memory regions. In computer engineering, the unit 1K is 1024 decimal, $400 in hexadecimal, 4*16^2 or 2^10 in decimal.

Size = End – Start + 1

ROM_Size = 12K bytesRAM_Size = 8K bytesPeripherals_Size = 64 bytes

9S12 ArchitecturePrograms are made up of executable code called instructions, and data. For the 9S12 an instruction generally has two parts.

● opcode – one or more bytes, usually one byte● operand – optional, one or more bytes

The following instruction puts the value $5A into the A register

Address Contents Description$C000 $86 opcode$C001 $5A operand

9S12 ArchitectureConcepts

big endian – To store a multi-byte value, with increasing address values, the most significant byte (msb) is stored first, and on, till finally the least significant byte (lsb).

start address – The last two bytes in memory contain the address of the the first instruction that will be executed following system reset.

hidden registers – The assembly language programmer need not be aware of additional non-accessible registers such as the IR, IP, and others used by the processor.

pre-increment, post-decrement – The stack pointer decrements before pushing and increments after pulling. (more later)

9S12 ArchitectureEffective Address (EA) – The final or actual address value

used to access memory

Addressing Modes – Methods used to calculate an EA. The value NX is the address of the operand.● Immediate (IMM) – the operand is the data, so EA = NX● Extended (EXT) – the operand value is the EA, so EA = [NX]● Direct (DIR) – operand byte is lsb of EA, so EA = $00:[NX]

only the first 256 addresses can be accessed● Relative (REL) – operand byte is offset to the branch target

(more later)● Indexed (IDX?) – Several, we consider those that include a

constant offset, and push, or pull stack (more later)

9S12 Architecture

Each addressing mode is used for a specific purpose:● Variable data can change● A variable effective address can access array data

AddressingMode

EffectiveAddress

Actual Dataor Access

Inherent Implied ImpliedImmediate, Relative Constant ConstantDirect, Extended Constant VariableIndexed Variable Variable

9S12 Instruction EncodingSo called inherent instructions imply the addressing mode or may not access memory.

Quick Check

Addressing mode?Hint: $5A is the data and it ‘immediately’ follows the opcodeAnswer: Immediate addressing

Effective address?Hint: The effective address is where the data isAnswer: $C001

Address Contents Description$C000 $86 opcode$C001 $5A operand

9S12 Architecturemnemonic – a short group of letters that suggest an action, a

given mnemonic may refer to several possible instructions, based on the addressing mode used.

Consider the mnemonic, ‘ldaa’ which means, ‘load a byte value into the A register. The addressing mode indicates where the value will be found in memory.

encoding – the first part of an instruction is called the opcode. The way an opcode conveys an action and the addressing mode(s) is called the encoding. See appendix A in Huang to see a list of all the instructions.

Mnemonics and AddressingBased on the addressing mode, one mnemonic may correspond to several possible opcodes.● Having no operand implies INH addressing● The ‘#’ implies IMM addressing● The K,REG implies this type of indexed addressingThe given symbolic notation is meant to be intuitive

Mnemonic Address Mode Example Encoding Notation

CLRA INH (inherent) CLRA $87 $00 → ALDAA IMM (immediate) LDAA #$5A $86 $5A $5A → A

DIR (direct) LDAA $A7 $96 $A7 [$A7] → AEXT (extended) LDAA $15A3 $B6 $15 $A3 [$15A3] → AIDX (indexed 5 bit) LDAA 0,X $A6 $00 [$00 + X] → A

Index Addressing Postbyte CodeThe postbyte indicates the type of indexed addressing. We will be concerned with ‘5 bit constant offset’ indexed addressing. The ‘0’ bit in xb indicates this type of indexed addressing.

xb = rr0nnnnn

rr indicates the index register used00 = X, 01 = Y, 10 = S, 11 = PC

nnnnn is a five bit constant in two’s complement

Calculating the 8 Bit Branch OffsetBefore a branch instruction is executed, the opcode and operand are fetched, so the PC already refers to the following instruction in memory:

A = Address of branch instruction opcodeB = Number of bytes in branch instructionA + B = Address of next instructionO = Offset value in two’s complementT = Target address

To summarize: A + B + O = TPerform the arithmetic in a 16 bit field. In producing O, keep the lower 8 bits. In using O, properly sign extend.

$00 to $7F – Prepend $00$FF to $80 – Prepend $FF

9S12 AssemblerAn assembler converts assembly language source code files, either to an intermediate form called an object file, or an executable file.

Some processors such as the Intel 8086 have several assemblers to choose from, each with a variation in syntax.

Thankfully, nearly all assemblers for the 9S12 and predecessors follow a traditional format (with quirks and oddities) documented in Motorola/Freescale literature.

9S12 Assembler Syntaxsymbol – a name for a value

label – a name for an address value

Rules to keep in mind for symbols:● A symbol can include letters (A-Z, a-z), underscores, periods,

numbers● The first character must not be a number● Generated symbols start with an underscore. While ‘legal’

avoid defining labels that start with an underscore● Use of a following colon is optional in defining a symbol, the

following colon will be ignored.● Decimal numbers are assumed by default ● The ‘$’ symbol indicates hexadecimal, the ‘%’ symbol indicates

binary, the ‘@’ symbol indicates octal

9S12 Assembler SyntaxAssembly language programs contain lines of text which contain several optional fields● The left-most field contains a label, symbol, or a semicolon to start

a comment line● The second field contains either a mnemonic or an assembler

directive (command to the assembler)● The third field may contains either the instruction operand or data

associated with a directive● The fourth field is for comments and starts with a semicolon

; Here is a comment lineSymbol: Directive Data ; A commentLabel: Mnemonic Operand ; Another comment

Assembly Language SectionsA section is a portion of code, which cannot be split into smaller parts. Each section has:● An optional section name● A section type: relocatable or absolute● Attributes, one of which is the ‘content’

Absolute sections start with the ‘ORG’ directive, which is followed by the actual address in memory where the section begins in memory.

Relocatable sections start with the ‘SECTION’ directive. Sections with similar content are combined. The address where such combined sections actually start is given in the parameter file.

Absolute Assembler Source CodeA program written for absolute assembly is specially processed by the assembler, which directly produces executable code.

● The program must only have absolute sections● Only assembly language is allowed● The entire program must be in one file● External libraries cannot be used

The next few slides are of an absolute assembly example program.

9S12 Assembler Example;*************************************************************;* abs_example_01 - main.asm - Jonathan Hill - Aug.28, 2009;* A first example using CodeWarrior for the HCS12 processors;* demonstrating absolute assembly code. This code is written;* assuming the use of Background Debug Mode.;*************************************************************

XDEF Start ; export symbols ABSENTRY Start ; required for absolute assembly

ORG $0900 ; section for data in RAMSum: DS.B 1 ; put the sum value here

ORG $4000 ; section for code, constantsStart: LDS #$0900 ; initialize the stack pointer

LDAA #$1A ; get a value ADDA Val2 ; add to another STAA Sum ; store the sum Done: BRA Done ; all done for nowVal2: DC.B $23 ; a constant value

ORG $FFFE ; section for a constant DC.W Start ; Reset Vector

9S12 DirectivesA directive is a command to the assembler, that tells the assembler to do something on behalf of the programmer.

XDEF – makes symbols public or visible to other tools

ABSENTRY – programs that contain only absolute sections can be handled specially and require this directive

ORG – starts an absolute section at a given address

DS.B or RMB – sets aside a memory location for number of bytes. The DS.W and DS.L set aside memory for words (16 bits), and long words (32 bits)

DC.B or FCB – sets aside a memory location for each byte value in a list and assigns that actual value

DC.W or FDW – like DC.B but for words (16 bits). Likewise, DC.L is for long words (32 bits)

Hand Assembly So FarTo form a list file, insert new text to the left that lists the first address of a line and the corresponding contents in memory.Here we focus on the code following the opening directives.Addx Contents Original Source Code

ORG $0900 ; section for data in RAM0900 ?? Sum: DS.B 1 ; put the sum value here

ORG $4000 ; section for code, constants4000 .... Start: LDS #$0900 ; initialize stack pointer LDAA #$1A ; get a value ADDA Val2 ; add to another STAA Sum ; store the sum Done: BRA Done ; all done for now Val2: DC.B $23 ; a constant value

ORG $FFFE ; section for a constantFFFE 40 00 DC.W Start ; Reset Vector

9S12 Assembly SyntaxThe mnemonic and operand field convey a desired addressing mode.

● Direct addresses are from $0000 to $00FF● ‘< address or label’, ‘ > address or label’ forces direct or extended● Branch instructions use relative addressing● ‘offset’ is a constant value or refers to the A, B, or D register ● ‘reg’ refers to either the X, Y, S, or PC register● Several additional types of indexed addressing available

Category Code Operand Fieldinherent INH no operanddirect, extended DIR,EXT address or labelimmediate IMM #data or symbolindexed IDX offset,regindexed others ..... ....indexed indirect ..... ....

Hand Assembly First PassTo hand assemble, go instruction by instruction. Leave a blank for each byte value that you don’t know yet, here is what our first pass looks like.Addx Contents Original Source Code

ORG $0900 ; section for data in RAM0900 ?? Sum: DS.B 1 ; put the sum value here

ORG $4000 ; section for code, constants4000 CF 09 00 Start: LDS #$0900 ; initialize stack pointer 4003 86 1A LDAA #$1A ; get a value4005 BB ADDA Val2 ; add to another4008 7A 09 00 STAA Sum ; store the sum 400B 20 Done: BRA Done ; all done for now400D 23 Val2: DC.B $23 ; a constant value

ORG $FFFE ; section for a constantFFFE 40 00 DC.W Start ; Reset Vector

Hand Assembly First Pass

After the first pass we know that:

Sum = $0900Start = $4000Done = $400BVal2 = $400D

The BRA instruction returns to itself. Given that a branch offset is from the following instruction, an offset equal to minus two is needed, or $FE.

Hand Assembly Second PassDuring the final pass, fill in the remaining unknown values. This is a completed list file. Addx Contents Original Source Code

ORG $0900 ; section for data in RAM0900 ?? Sum: DS.B 1 ; put the sum value here

ORG $4000 ; section for code, constants4000 CF 09 00 Start: LDS #$0900 ; initialize stack pointer 4003 86 1A LDAA #$1A ; get a value4005 BB 40 0D ADDA Val2 ; add to another4008 7A 09 00 STAA Sum ; store the sum 400B 20 FE Done: BRA Done ; all done for now400D 23 Val2: DC.B $23 ; a constant value

ORG $FFFE ; section for a constantFFFE 40 00 DC.W Start ; Reset Vector

Quick CheckList the addressing mode and EA for each instruction:Addx Contents Original Source Code

ORG $0900 ;0900 ?? Sum: DS.B 1 ;

ORG $4000 ;4000 CF 09 00 Start: LDS #$0900 ; Immediate, EA = $4001 4003 86 1A LDAA #$1A ; Immediate, EA = $40044005 BB 40 0D ADDA Val2 ; Extended, EA = $400D4008 7A 09 00 STAA Sum ; Extended, EA = $0900 400B 20 FE Done: BRA Done ; Relative, EA = $400B400D 23 Val2: DC.B $23 ;

ORG $FFFE ; FFFE 40 00 DC.W Start ;

Hand Assembly Second PassA ‘memory dump’ lists values in memory. Each line starts with the address of the first byte in the line. The ‘??’ symbols indicates unknown values.

0900 - ?? ?? ?? ?? ?? ?? ?? ?? - ?? ?? ?? ?? ?? ?? ?? ??

4000 - CF 09 00 86 1A BB 40 0D - 7A 09 00 20 FE 23 ?? ??

FFF0 - ?? ?? ?? ?? ?? ?? ?? ?? - ?? ?? ?? ?? ?? ?? 40 00

An execution history is a listing showing by instruction how a program executes. Remember that the PC always refers to the ‘next’ instruction.

Execution History

The value in PC refers to the instruction that will be executed next. ‘??’ refers to an unknown value.

Write a value in for each assignment made to a register, whether or not a change is made in value.

A blank entry means "no assignment "

The brackets [ ] mean "the contents of " and are involved with accessing a given memory address.

PC A SP [Val2] Comment, to do next$4000 ?? ???? $23 $0900 → SP$4003 $0900 $1A → A$4005 $1A A + [Val2] → A$4008 $3D A → [Sum]$400B $3D BRA repeats

Building a Relocatable ProgramSource files are used to produce the object files. The parameter file explains how to combine similar sections from the object fies and libraries and how to produce the vector table. The linker then produces the executable program, called the absolute image file.

Linker and Parameter FileTo hand assemble relocatable source code we need to know how where the sections will be assigned to memory.

The linker reads each object file according to a list called the ‘link order’ and combines similar sections. The parameter file (.prm) instructs the linker how to assign the sections to memory.

For this example you can write after each section directive the address where the section starts and then assemble it as before.

Assembly Language SectionsFor relocatable sections there are three content types, based on simple rules:

Code Section – for executable code● Contains at least one instruction● Assigned to read-only memory (ROM)

Constant Section – for constant value data● Contains only constant data● Assigned to read-only memory (ROM)

Data Section – for variables in memory● Contains data that can change● Assigned to read-write memory (RAM)

Linker Parameter File/* This is a linker parameter file for the MC9S12C32 */NAMES END /* File names through a command line or here */

SEGMENTS /* The memory regions available */ RAM = READ_WRITE 0x0800 TO 0x0FFF; ROM_4000 = READ_ONLY 0x4000 TO 0x7FFF; ROM_C000 = READ_ONLY 0xC000 TO 0xFEFF;END

PLACEMENT /* Where in memory the sections actually go */ DEFAULT_ROM INTO ROM_4000; SSTACK, DEFAULT_RAM INTO RAM;END

ENTRIES /* keep the following unreferenced variables */END

STACKSIZE 0x100

VECTOR 0 Start /* the entry point */INIT Start /* Also needed for assembly language *//* end of parameter file */

Relocatable Example Program;*************************************************************;* rel_example_01 - main.asm - Jonathan Hill - Aug.28, 2009;* A first example using CodeWarrior for the HCS12 processors;* demonstrating relocatable assembly code. This code is;* written assuming the use of Background Debug Mode.;************************************************************* XDEF Start ; export symbols XREF __SEG_END_SSTACK ; symbol defined elsewhere

MyRAM: SECTION ; section for data in RAMSum: DS.B 1 ; put the sum value here

MyCode: SECTION ; section for codeStart: LDS #__SEG_END_SSTACK ; initialize SP

LDAA #$1A ; get a value ADDA Val2 ; add to another STAA Sum ; store the sum Done: BRA Done ; all done for now

MyCnst: SECTION ; section for constant dataVal2: DC.B $23 ; a constant value; end of example program

Relocatable Example Program

Addx Contents Original Source Code

XDEF Start ; export symbols XREF __SEG_END_SSTACK ; linker defined MyRAM: SECTION ; data section for RAM0900 ?? Sum: DS.B 1 ; put the sum value here

MyCode: SECTION ; code section4000 CF 09 00 Start: LDS #__SEG_END_SSTACK ; initialize SP

4003 86 1A LDAA #$1A ; get a value4005 BB 40 0D ADDA Val2 ; add to another4008 7A 09 00 STAA Sum ; store the sum 400B 20 FE Done: BRA Done ; all done for now

MyCnst: SECTION ; constant data section400D 23 Val2: Val2: DC.B $23 ; a constant value

A similar list file is produced. For this example the same program code is loaded into memory.

Relocatable Example Program

Observations:In hand assembling the file, we are taking on the job of the assembler and the linker, togetherThe assembler does not know:● Where any sections actually go into memory● Where the stack will be in memoryThe linker has some work to do:● Place each section into memory● Build the vector table which includes the reset address

but was not shown above

Significant Topics● CPU registers● Memory map● Instruction format● Addressing modes, effective address (EA)● Instruction encoding● Assembler syntax, directives, symbols, labels● Absolute assembler● Relocatable assembler, sections, parameter file● Hand assembly● Machine code in memory, dump file● Execution history