Topic03B Assembly Language on the Freescale MC9S12X Part 1

62
Assembly Language: Instructions 1 Topic Video 03B 3B 1 Thursday, 2 July 2009

description

This topic is the first in a two part series, that covers the most commonly used assembly instructions inside the Freescale MC9S12X. This topic also contains assembly coding examples

Transcript of Topic03B Assembly Language on the Freescale MC9S12X Part 1

Page 1: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

Assembly Language: Instructions 1

Topic Video 03B3B

1Thursday, 2 July 2009

Page 2: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

• The CPUS12 has over 200 different instructions, performing 188 different tasks.

• The instruction are split into categories, these are:

The HS12 Instruction Set

Conditional BranchesLoop PrimitivesJump and BranchCondition CodeInterrupt

Load and StoreTransfer and ExchangeMove MemoryDecrement and IncrementArithmeticLogicRotates and ShiftsData Test

2Thursday, 2 July 2009

Page 3: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetLoad and Store Instructions

• The load and store instructions allows us to move data in and out of the registers.

• Depending on the size of the register, this data can be byte size or words (16 bit) size.

• There are load and store in instructions that can operate on:

Load StoreAccumulator A LDAA STAAAccumulator B LDAB STABAccumulator D LDD STDX index Register LDX STXY index Register LDY STYStack Pointer LDS STS

3Thursday, 2 July 2009

Page 4: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetLoad and Store Instructions (Continued)‏

Examples

Immediate Direct/ Extended IndexedLDAA #$23 LDAA Val LDAA 0,X

LDAB #$FA LDAB Byte LDAB 0,Y

LDD #$4523 LDD $1234 LDD 0,SP

LDY #3465 LDX Data LDX 0,Y

LDX #65563 LDY Value LDY 0,PC

4Thursday, 2 July 2009

Page 5: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

Examples

Direct/ Extended IndexedSTAA Val STAA 0,X

STAB Byte STAB 0,Y

STD $1234 STD 0,SP

STX Data STX 0,Y

STY Value STY 0,PC

The HS12 Instruction SetLoad and Store Instructions (Continued)‏

5Thursday, 2 July 2009

Page 6: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

• There is another way that we can load or store values.

• We can use the stack.

• The act of storing a value onto the stack is called “PUSHING”.• The act of loading a value from the stack is called “PULLING”.

• The “PULL” and “PUSH” instructions that can operate on:

Pull PushAccumulator A PULA PSHAAccumulator B PULB PSHBAccumulator D PULD PSHDX index Register PULX PSHXY index Register PULY PSHY

The HS12 Instruction SetLoad and Store Instructions (Continued)‏

6Thursday, 2 July 2009

Page 7: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

Example

... LDAA #32

PSHA #SP=SP-1, M[SP]=A...

The HS12 Instruction SetLoad and Store Instructions (Continued)‏

7Thursday, 2 July 2009

Page 8: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetTransfer Instructions

• The Transfer Instructions allow us to move data within the CPU from one register to another.

Transfer A to B TABTransfer B to A TBATransfer Registers TFR Source Register, Destination RegisterTransfer SP to X TFR S,XTransfer SP to Y TFR S,YTransfer X to SP TFR X,STransfer Y to SP TFR Y,SExchange Registers EXG Register1, Register 2Exchange D and X EXG D,XExchange D and Y EXG D,YExchange B and A EXG B,A

8Thursday, 2 July 2009

Page 9: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

8 bit register 8 bit register

16 bit register 16 bit register

8 bit value

16 bit value

The HS12 Instruction SetTransfer Instructions

9Thursday, 2 July 2009

Page 10: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

8 bit register 8 bit register

16 bit register 16 bit register

8 bit value

16 bit value

The HS12 Instruction SetTransfer Instructions

9Thursday, 2 July 2009

Page 11: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

• But what happens if the registers are different sizes?

8 bit register

8 bit register

8 bit register16 bit register

16 bit register

16 bit registerLSBMSB LSB

8 bit value8 bit value

8 bit valueLSB

The HS12 Instruction SetTransfer Instructions

10Thursday, 2 July 2009

Page 12: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

• But what happens if the registers are different sizes?

8 bit register

8 bit register

8 bit register16 bit register

16 bit register

16 bit registerLSBMSB LSB

8 bit value8 bit value

8 bit valueLSB

The HS12 Instruction SetTransfer Instructions

10Thursday, 2 July 2009

Page 13: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

• But what happens if the registers are different sizes?

8 bit register

8 bit register

8 bit register16 bit register

16 bit register

16 bit registerLSBMSB LSB

8 bit value 8 bit valueMSB=0

8 bit valueLSB

The HS12 Instruction SetTransfer Instructions

10Thursday, 2 July 2009

Page 14: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

• But what happens if the registers are different sizes?

8 bit register

8 bit register

8 bit register16 bit register

16 bit register

16 bit registerLSBMSB LSB

8 bit value 8 bit valueMSB=0

MSB=0 8 bit value LSB

The HS12 Instruction SetTransfer Instructions

10Thursday, 2 July 2009

Page 15: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

• Using the MOVE instructions it is possible to transfer data from one memory location to another without the need for a register.

• The MOVE instructions have two possible forms; MOVB for moving bytes and MOVW, for moving words.

• The source can be a constant or a memory location, however the destination must always be a memory address.

Move (copy) a byte MOVB Source, DestinationMove (copy) a word MOVW Source, Destination

11Thursday, 2 July 2009

Page 16: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

8 bit value

M[PortA]

M[PortA] M[Data]

M[$1234]

M[Data]8 bit value23

$FF 23

MOVB #23, Data MOVB PortA, Data

MOVB #$FF, PortA MOVB #23, $1234

23$FF

23

12Thursday, 2 July 2009

Page 17: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

8 bit value

M[PortA]

M[PortA] M[Data]

M[$1234]

M[Data]8 bit value23

$FF 23

MOVB #23, Data MOVB PortA, Data

MOVB #$FF, PortA MOVB #23, $1234

23$FF

23

12Thursday, 2 July 2009

Page 18: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

8 bit value

M[PortA]

M[PortA] M[Data]

M[$1234]

M[Data]8 bit value23

$FF 23

MOVB #23, Data MOVB PortA, Data

MOVB #$FF, PortA MOVB #23, $1234

23$FF

23

12Thursday, 2 July 2009

Page 19: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

8 bit value

M[PortA]

M[PortA] M[Data]

M[$1234]

M[Data]8 bit value23

$FF 23

MOVB #23, Data MOVB PortA, Data

MOVB #$FF, PortA MOVB #23, $1234

23$FF

23

12Thursday, 2 July 2009

Page 20: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

8 bit value

M[PortA]

M[PortA] M[Data]

M[$1234]

M[Data]8 bit value23

$FF 23

MOVB #23, Data MOVB PortA, Data

MOVB #$FF, PortA MOVB #23, $1234

23$FF

23

12Thursday, 2 July 2009

Page 21: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

M[Data](MSB)‏M[Data+1](LSB) ‏

MOVW #$1423, Data

$14$23

MOVW PortA, Data

Port APort B M[Data+1](LSB)‏

M[Data](MSB)8‏ bit value8 bit value

8 bit value8 bit value

13Thursday, 2 July 2009

Page 22: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

M[Data](MSB)‏M[Data+1](LSB) ‏

MOVW #$1423, Data

$14$23

MOVW PortA, Data

Port APort B M[Data+1](LSB)‏

M[Data](MSB)‏

$14$23

8 bit value8 bit value

8 bit value8 bit value

13Thursday, 2 July 2009

Page 23: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetMove Instructions

M[Data](MSB)‏M[Data+1](LSB) ‏

MOVW #$1423, Data

$14$23

MOVW PortA, Data

Port APort B M[Data+1](LSB)‏

M[Data](MSB)‏

$14$23

8 bit value 8 bit value

8 bit value8 bit value

13Thursday, 2 July 2009

Page 24: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetDecrement and Increment Instructions

• Using the increment and decrement commands it is possible to increase or decrease the value in a register or memory location by 1.• We can use this commands on:

Increment DecrementAccumulator A INCA DECA Accumulator B INCB DECBX index Register INX DEXY index Register INY DEYStack Pointer INS DESMemory INC mem DEC mem

14Thursday, 2 July 2009

Page 25: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetDecrement and Increment Instructions

Examples

mainLoop: JSR InitSCI

LDAB #2 INCB JSR OUT2HEXSpin: BRA SpinSerial_Base EQU $00D0;Base Address for SCI.

INCLUDE 'Serial.inc';Add serial routines ...

15Thursday, 2 July 2009

Page 26: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetDecrement and Increment Instructions

mainLoop:

JSR InitSCI MOVB #2,Counter INC Counter LDAB Counter JSR OUT2HEX

Spin: BRA SpinSerial_Base EQU $00D0 ;Base Address for SCI INCLUDE 'Serial.inc' ;Add serial routinesCounter DS 1

...

16Thursday, 2 July 2009

Page 27: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

• The clear and set instructions allows us to clear accumulator A, B or even a memory location (variable).

• They also allows us to set or clear particular bits in a memory location without affecting those bit near it.

Clear Accumulator A CLRAClear Accumulator B CLRBClear Memory CLR memory location

17Thursday, 2 July 2009

Page 28: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Bit Clear BCLR Operand, MaskBit Set BSET Operand, Mask

The operand is a memory location or variable name.The Mask specifies the bits to be set or cleared.The Mask consists of a binary number, the bit locations were a ‘1’

appear the locations that are cleared (BCLR) or set (BSET).

18Thursday, 2 July 2009

Page 29: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA%10101010

19Thursday, 2 July 2009

Page 30: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%10101010MOVB #%10101010,PortA

19Thursday, 2 July 2009

Page 31: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%10101010

19Thursday, 2 July 2009

Page 32: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%10101010BSET PortA, %11110000

19Thursday, 2 July 2009

Page 33: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%11111010BSET PortA, %11110000

19Thursday, 2 July 2009

Page 34: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%11111010BSET PortA, %11110000

19Thursday, 2 July 2009

Page 35: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%11111010%11111010

BCLR PortA, %10101010

19Thursday, 2 July 2009

Page 36: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%01010000

BCLR PortA, %10101010

19Thursday, 2 July 2009

Page 37: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetClear and Set Instructions

Example

MOVB #%10101010,PortA

BSET PortA, %11110000

BCLR PortA, %10101010

PortA

%01010000

19Thursday, 2 July 2009

Page 38: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

• We can use the the shift and rotate instructions on accumulators A, B, D or even directly on memory.

Arithmetic Shifting Left Right

Accumulator A ASLA ASRA

Accumulator B ASLB ASRB

Accumulator D ASLD ASRD

Memory ASL mem ASR mem

Logical Shifting Left Right

Accumulator A LSLA LSRA

Accumulator B LSLB LSRB

Accumulator D LSLD LSRD

Memory LSL mem LSR mem

20Thursday, 2 July 2009

Page 39: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

100000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

21Thursday, 2 July 2009

Page 40: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1

0

0000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

22Thursday, 2 July 2009

Page 41: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1 00000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

22Thursday, 2 July 2009

Page 42: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1

0

0000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

23Thursday, 2 July 2009

Page 43: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1 00000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

23Thursday, 2 July 2009

Page 44: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1

0

0000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

24Thursday, 2 July 2009

Page 45: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1 00000000

Logical Shift Left / Arithmetic Shift Left

CCR[C]

24Thursday, 2 July 2009

Page 46: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1

0

00000 00

Logical Shift Left / Arithmetic Shift Left

CCR[C]

25Thursday, 2 July 2009

Page 47: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1 000000 00

Logical Shift Left / Arithmetic Shift Left

CCR[C]

25Thursday, 2 July 2009

Page 48: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1

0

00000 00

Logical Shift Left / Arithmetic Shift Left

CCR[C]

26Thursday, 2 July 2009

Page 49: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

1 000000 00

Logical Shift Left / Arithmetic Shift Left

CCR[C]

26Thursday, 2 July 2009

Page 50: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0

00000 0 0

Logical Shift RightCCR[C]

1 00000 01

Arithmetic Shift RightCCR[C]

1

0

27Thursday, 2 July 2009

Page 51: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0

00000 0 0

Logical Shift RightCCR[C]

1

00000 01

Arithmetic Shift RightCCR[C]

1

0

27Thursday, 2 July 2009

Page 52: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0

00001 0 0

Logical Shift RightCCR[C]

0

1

00001 0

Arithmetic Shift RightCCR[C]

0

0

28Thursday, 2 July 2009

Page 53: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 00001 0 0

Logical Shift RightCCR[C]

01 00001 0

Arithmetic Shift RightCCR[C]

0

0

28Thursday, 2 July 2009

Page 54: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0

00010 0 0

Logical Shift RightCCR[C]

0

1

00011 0

Arithmetic Shift RightCCR[C]

0

0

29Thursday, 2 July 2009

Page 55: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 00010 0 0

Logical Shift RightCCR[C]

01 00011 0

Arithmetic Shift RightCCR[C]

0

0

29Thursday, 2 July 2009

Page 56: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

• We can use the the shift and rotate instructions on accumulators A, B, D or even directly on memory.

Rotating Left Right

Accumulator A ROLA RORA

Accumulator B ROLB RORB

Accumulator D ROLD RORD

Memory ROL ROR

30Thursday, 2 July 2009

Page 57: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 0001 0 0 0

Rotate RightCCR[C]

00000 0

Rotate LeftCCR[C]

0 0X

X

31Thursday, 2 July 2009

Page 58: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 00010 0 0

Rotate RightCCR[C]

1 00000 0

Rotate LeftCCR[C]

0

0 0

32Thursday, 2 July 2009

Page 59: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 00010 0 0

Rotate RightCCR[C]

1 00000 0

Rotate LeftCCR[C]

0

00

32Thursday, 2 July 2009

Page 60: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 00100 0 0

Rotate RightCCR[C]

0 00000 0

Rotate LeftCCR[C]

0

1 0

33Thursday, 2 July 2009

Page 61: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

The HS12 Instruction SetShift and Rotate Instructions

0 00100 0 0

Rotate RightCCR[C]

0 00000 0

Rotate LeftCCR[C]

0

10

33Thursday, 2 July 2009

Page 62: Topic03B  Assembly Language on the Freescale MC9S12X Part 1

Need Further Assistance?

• Ask your Demonstrator,

• Post a question on the Forum,

• Email the Convener, or

• Make an appointment.

34Thursday, 2 July 2009