ece 372 – microcontroller design assembly programming

24
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Basic Assembly Programming Top Assembly Instructions (Instruction You Should Know!) Assembly Programming Concepts Data Organization

Upload: hina

Post on 11-Jan-2016

59 views

Category:

Documents


0 download

DESCRIPTION

ECE 372 – Microcontroller Design Assembly Programming. HCS12 Assembly Programming Basic Assembly Programming Top Assembly Instructions (Instruction You Should Know!) Assembly Programming Concepts Data Organization. ECE 372 – Microcontroller Design Assembly Programming. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ECE 372 – Microcontroller Design Assembly Programming

1

ECE 372 – Microcontroller Design Assembly Programming

HCS12 Assembly Programming Basic Assembly Programming Top Assembly Instructions (Instruction You Should Know!) Assembly Programming Concepts Data Organization

Page 2: ECE 372 – Microcontroller Design Assembly Programming

2

ECE 372 – Microcontroller Design Assembly Programming

HCS12 Assembly Instructions Load and Store Instructions Arithmetic Operations Logic Operations Branch Instructions Shift and Rotation Operation Bit tests and Bit manipulations Stack and Index Pointer Operations Condition Code Register Instructions Subroutine Calls Interrupt Handling Operations Miscellaneous Operations

Page 3: ECE 372 – Microcontroller Design Assembly Programming

3

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Load and Store Instructions ldaa/ldab/ldd - Load A/B/D from memory (or immediate value) ldx/ldy - Load Index Register X/Y from memory (or immediate

value) lds - Load Stack Pointer (SP) from memory (or immediate value)

staa/stab/std - Store A/B/D to memory stx/sty – Store Index Register X/Y to memory sts – Store Stack Pointer (SP) to memory

Page 4: ECE 372 – Microcontroller Design Assembly Programming

4

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Arithmetic Operations adda/addb/addd – Add memory (or immediate value) to A/B/D suba/subb/subd – Subtract memory (or immediate value) from

A/B/D aba – Add B to A sba – Subtract B from A inca/incb/inx/iny – Increment A/B/X/Y

e.g. A = A + 1 deca/decb/dex/dey – Decrement A/B/X/Y

e.g. A = A - 1

Page 5: ECE 372 – Microcontroller Design Assembly Programming

5

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Arithmetic Operations cmpa/cmpb/cpd/cpx/cpy – Compare A/B/D/X/Y with memory (or

immediate value) Branch follows (e.g. greater, less, equal)

mul – Performs 8-bit unsigned multiplication of A and B, storing 16-bit result in D

e.g. D = A * B

idiv – Performa 16-bit unsigned division of D and Y, storing quotient in X and remainder in D

e.g. X = D / X and D = D % X

Page 6: ECE 372 – Microcontroller Design Assembly Programming

6

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Logic Operations anda/andb – Perform 8-bit logical AND of A/B with memory (or

immediate value) oraa/orab – Perform 8-bit logical OR of A/B with memory (or

immediate value) eora/eorb – Perform 8-bit logical XOR of A/B with memory (or

immediate value) bset/bclr – Set/Clear bit in memory using provided mask bita/bitb – Performs 8-bit logical AND of A/B with memory (or

immediate value) and modifies CCR Contents of A/B or the memory location are not affected

Page 7: ECE 372 – Microcontroller Design Assembly Programming

7

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Branch Instructions (Unsigned/Signed) beq – Branch if equal (Relative PC Addressing) bne – Branch if not equal (Relative PC Addressing) bra – Branch always (Relative PC Addressing) jmp – Branch always (Absolute Addressing)

Page 8: ECE 372 – Microcontroller Design Assembly Programming

8

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Branch Instructions (Unsigned) bhi – Branch if higher (Relative PC Addressing)

i.e. unsigned greater than bhs – Branch if higher or same (Relative PC Addressing)

i.e. unsigned greater than or equal blo – Branch if lower (Relative PC Addressing)

i.e. unsigned less than bls – Branch if lower or same (Relative PC Addressing)

i.e. unsigned less than or equal

Page 9: ECE 372 – Microcontroller Design Assembly Programming

9

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Branch Instructions (Signed) bgt – Branch if greater than (Relative PC Addressing) bge – Branch if greater than of equal (Relative PC Addressing) blt – Branch if less than (Relative PC Addressing) ble – Branch if less than or equal (Relative PC Addressing) bmi – Branch if minus (Relative PC Addressing)

i.e. negative result bpl – Branch if plus (Relative PC Addressing)

i.e. positive result

Page 10: ECE 372 – Microcontroller Design Assembly Programming

10

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Long Branch Instructions Similar to other branch instructions

PC relative addressing But with 16-bit relative address

Few Examples: lbra – Branch always (Long Relative PC Addressing) lbeq – Branch if equals (Long Relative PC Addressing) lbgt – Branch if greater than (Long Relative PC Addressing)

Page 11: ECE 372 – Microcontroller Design Assembly Programming

11

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Shift and Rotate Operations rola/rolb – Rotate left A/B through carry (C)

rora/rorb – Rotate right A/B through carry (C)

C 7 6 5 4 3 2 1 0Rotate Left:

Page 12: ECE 372 – Microcontroller Design Assembly Programming

12

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Shift and Rotate Operations lsla/lslb/lsld – Logical shift left A/B/D into carry (C)

Same as asla/aslb/asld instructions Least significant bit is loaded with 0 Also performs multiply by 2

lsra/lsrb/lsrd – Logical shift right A/B/D into carry (C) NOT the same asra/asrb/asrd Most significant bit is loaded with 0 Also performs unsigned divide by 2

C 7 6 5 4 3 2 1 0 0Logical Shift Left:

C7 6 5 4 3 2 1 0Logical Shift Right: 0

Page 13: ECE 372 – Microcontroller Design Assembly Programming

13

ECE 372 – Microcontroller Design Assembly Programming – Top Assembly Instructions

Shift and Rotate Operations asla/aslb/asld – Arithmetic shift left A/B/D into carry (C)

Same as lsla/lslb/lsld instructions Least significant bit is loaded with 0 Also performs multiply by 2

asra/asrb/asrd – Arithmetic shift right A/B/D into carry (C) NOT the same lsra/lsrb/lsrd Most significant bit is loaded with previous most significant bit value Also performs unsigned/signed divide by 2

C7 6 5 4 3 2 1 0Arithmetic Shift Right:

Page 14: ECE 372 – Microcontroller Design Assembly Programming

14

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

if(cond){ // if body}

If-Then C Code:

If-Then Assembly Template: cmp?? ; If Cond. b?? EndIf ; Branch !cond

; If BodyEndIf:

If Cond.

!cond If Body

cond

Page 15: ECE 372 – Microcontroller Design Assembly Programming

15

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

if(a>=20){ // if body}

If-Then C Code:

If-Then Assembly Code: cmpa, #20 ; If Cond. (A>=20) blo EndIf ; Branch !(A>=20)=>(A<20)

; If BodyEndIf:

If Cond.

!(A>=20) If Body

A>=20

Page 16: ECE 372 – Microcontroller Design Assembly Programming

16

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

if(cond) { // if body}else { // else body}

If-Then-Else C Code:

If-Then-Else Assembly Template: cmp?? ; If-Else Cond. b?? Else ; Branch !cond

; If Body bra EndIf ; Branch EndIfElse: ; Else Body ;EndIf:

If–ElseCond.

cond

Else Body

!cond

If Body

Page 17: ECE 372 – Microcontroller Design Assembly Programming

17

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

if(a>=20) { // if body}else { // else body}

If-Then-Else C Code:

If-Then-Else Assembly Code: cmpa, #20 ; If-Else Cond. (A>=20) blo Else ; Branch !(A>=20)=>(A<20)

; If Body bra EndIf ; Branch EndIfElse: ; Else Body ;EndIf:

If–ElseCond.

A>=20

Else Body

!(A>=20)

If Body

Page 18: ECE 372 – Microcontroller Design Assembly Programming

18

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

for(init; cond; iter){ // loop body}

For Loop C Code: Loop Init.

Loop Cond.

!cond

Loop Body

Loop Iter.

cond

For Loop Assembly Template: ?? ; Loop Init. Loop: cmp?? ; Loop Cond. b?? EndLoop ; Branch !cond

; Loop Body ?? ; Loop Iter. bra Loop ; Repeat LoopEndLoop:

Page 19: ECE 372 – Microcontroller Design Assembly Programming

19

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

for(J=0; J<10; J++){ // loop body}

For Loop C Code: J=0

Loop Cond.

!(J<10)

Loop Body

J++

J<10

For Loop Assembly Code: ldaa #0 ; Loop Init. (J=0)Loop: cmpa #10 ; Loop Cond. (J<10) bge EndLoop ; Branch !(J<10)=>(J>=10)

; Loop Body adda #1 ; Loop Iter. (J++) bra Loop ; Repeat LoopEndLoop:

Page 20: ECE 372 – Microcontroller Design Assembly Programming

20

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

while(cond){ // loop body}

While Loop C Code:

Loop Cond.

!cond

Loop Body

cond

While Loop Assembly Template: Loop: cmp?? ; Loop Cond. b?? EndLoop ; Branch !cond

; Loop Body bra Loop ; Repeat LoopEndLoop:

Page 21: ECE 372 – Microcontroller Design Assembly Programming

21

ECE 372 – Microcontroller Design Assembly Programming – Programming Concepts

while(i!=0){ // loop body}

While Loop C Code:

Loop Cond.

!(i!=0)

Loop Body

i!=0

While Loop Assembly Code: Loop: cmpa, #0 ; Loop Cond. (i!=0) beq EndLoop ; Branch !(i!=0)=>(i==0)

; Loop Body bra Loop ; Repeat LoopEndLoop:

Page 22: ECE 372 – Microcontroller Design Assembly Programming

22

ECE 372 – Microcontroller Design Assembly Programming – Data Organization

Data Organization Global Variables Constants Program Code Local Variables Return Addresses Temporary Data

Page 23: ECE 372 – Microcontroller Design Assembly Programming

23

ECE 372 – Microcontroller Design Assembly Programming – Data Organization

Data Organization Heap

Block of memory for allocating storage whose lifetime is not related to the execution of the current routine

Often used for dynamically allocated data (malloc)

Typically located at lowest memory address after global varaibles

Page 24: ECE 372 – Microcontroller Design Assembly Programming

24

ECE 372 – Microcontroller Design Assembly Programming – Data Organization

Data Organization Stack

Generally – A data structure supporting two basic operations, push and pop

Push – Adds an item to the top of the stack

Pop – Removes an item from the top of the stack

LIFO – Last In First Out data structure (opposite of queue)

Stack Pointer (SP) Points to memory location

corresponding to the top of the stack

Must be initialized within your assembly program