23
Physics 413 Chapter 2: HCS 12 Assembly Programming

Physics 413 Chapter 2 : HCS 12 Assembly Programming

Embed Size (px)

DESCRIPTION

Physics 413 Chapter 2 : HCS 12 Assembly Programming. My First Assembly Program. Program adds two 16-bit numbers stored at $1000 - $1001 and $1002 - $1003 and stores the sum at $1010 - $1011 org $2000 ldd $1000 addd $1002 std $1010 end. Assembler Directives and more …. org end equ db - PowerPoint PPT Presentation

Citation preview

Physics 413Chapter 2: HCS 12 Assembly Programming

My First Assembly Program

Program adds two 16-bit numbers stored at $1000 - $1001 and $1002 - $1003 and stores the sum at $1010 - $1011

org $2000

ldd $1000

addd $1002

std $1010

end

Assembler Directives and more …

org

end

equ

db

fcc

Examples

alpha equ 1.7

string fcc “hello!”

array db $3, $8, $23, $11, $57, $12, $47

DAA

DAA Decimal Adjust Accumulator

Converts binary addition of BCD numbers into BCD

format

What will the contents of A be with & without DAA ?

LDAA # $ 23 86 23

ADDA # $ 48 8B 48

DAA 19

SWI 3F

DAA

LDAA # $ 23

ADDA # $ 48

DAA

SWI

Without DAA the contents of A = 6B

With DAA the contents of A = 71

Multiplication and Division

mul A * B stored in A:B

emul D * Y stored in Y:D

ediv 32-bit number Y:D divided by 16-bit X and quotient stored in Y and remainder in D

emacs

Multiply and Accumulate

Multiplies two 16-bit numbers and adds a 32-bit number.

16-bit numbers are pointed to by index registers X and Y.

Result is stored where the 32-bit constant was stored!

16-bit number takes up two memory locations

32-bit number takes up 4 memory locations.

Useful in signal processing and other computational problems

Rewrite ax2 +bx +c as x(ax+b) +c for emacs format

(Mx . Mx+1 )*(My . My+1 ) + (M . M +3) (M . M +3)

A Fork in the Road

LDAB # $ 13

here : ADDA # $ 24

DEC B

BNE here

SWI

BNE is the new instruction. Branch if not equal to zero.

Branches Galore

Mnemonic Description of Criteria

BCC Branch if Carry Clear

BCS Branch if Carry Set

BEQ Branch if Equal to Zero

BNE Branch if Not Equal to Zero

BRA Branch Always

Note: Familiarize yourself with other related instructions like DBNE, JMP, BSR, and JSR

Dare to Compare !

here: LDAA # $ E3

CMPA $ 50

BNE here

SWI

Temperature sensor reading is stored at memory location 0050 and compared with a danger limit of E3 .

Bit Condition Branch Instruction

again brclr $1000,$84,again

ldd $70

The mask is $84. The bits to be checked are bit 2 and bit 7. The contents of memory location $1000 will be checked and when bits 2 and 7 are both 0 the instruction ldd $70 will be executed . If both bits 2 and 7 are not 0 then the program will keep checking by going back to again.

Bit Condition Branch Instruction

again brset $1000,$84,again

ldd $70

The mask is $84. The bits to be checked are bit 2 and bit 7. The contents of memory location $1000 will be checked and when bits 2 and 7 are both 1 the instruction ldd $70 will be executed . If both bits 2 and 7 are not 1 then the program will keep checking by going back to again.

ASLA

ASLA Arithmetic Shift Left Accumulator A.

It shifts all the 8 bits of accumulator A one bit to the LEFT. The rightmost bit becomes 0 and the leftmost bit ends up as the carry bit of CCR

0C

Logical AND

What is the outcome of ANDA #$ 4C if accumulator A contains 3 A ?

Logical AND

3A 0011 1010

4C 0100 1100

_________________________

0000 1000

Hence A will contain 08 after the AND operation

Bit Flipping and Testing

bclr 0,y,$44 clears bits 2 and 6 of the contents of the memory location pointed to by the index register Y.

bset sets the bits designated by the mask.

bita $22 tests bits 1 and 5 of accumulator A and updates the Z and N flags of the CCR but does not change the contents of the accumulator.

Delay Loop

DELAY: LDX # $ FFFF

AGAIN : DEX

BNE AGAIN

SWI

Delay Loop Subroutine

here: JSR DELAY

LDAA # $ E3

CMPA $ 50

BNE here

SWI

.

.

.

DELAY: LDX # $ FFFF

AGAIN : DEX

BNE AGAIN

RTS

Stack

Stack is the area of RAM pointed to by the 16-bit Stack Pointer (SP)

LDS functions like LDX

LDS #$ 5C42 The number 5C42 is loaded into SP

LDS $ 5C42 Numbers from 5C43 and 5C42 loaded

LDS $ 5C Numbers from 005D and 005C loaded

Push and Pull

PSHA A MSP SP - 1 SP

PULA SP + 1 SP MSP A

Predict the Outcome!

PSHA

PSHB

PULA

PULB

Solution

Congratulations, if you said the contents of A and B will be swapped and , perhaps more importantly, the value of the stack pointer will be restored to its original value before this program segment was run.

00D6

00D5

00D4

00D3

Detailed Explanation

Suppose that SP was pointing at 00D6 (stack). PUSHA stores A into 00D6. Then PUSHB stores B into 00D5. At this point SP = 00D4. Then PULA pulls 00D5 (which contains B) and stores it into A. Finally, PULB pulls 00D6 (which contains A) and stores it into B. We end up swapping A and B. At this point SP = 00D6, its original value.

00D6

00D5

00D4

00D3