Summing Odd Numbers in an Array using Assembly Language on HC12, Lecture notes of Microcontrollers

An assembly language program to sum odd numbers in a memory array from 0xE000 to 0xE01f using HC12. It includes the program flow, CPU register usage, and assembly code. It also discusses the concept of a stack and stack pointer in HC12.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

prouline
prouline 🇬🇧

4.6

(7)

221 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EE 308 Spring 2002
Another Example of an Assembly Language Program
Add the odd numbers in an array of data.
The numbers are 8-bit unsigned numbers.
The address of the first number is $E000 and the address of the final number
is $E01F.
Save the result in a variable called answer at address $0900.
Start by drawing a picture of the data structure in memory:
0xE000
0xE01F
5
1
8
6
11
4
SUM ODD NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f
Treat numbers as 8−bit unsigned numbers
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Summing Odd Numbers in an Array using Assembly Language on HC12 and more Lecture notes Microcontrollers in PDF only on Docsity!

Another Example of an Assembly Language Program

Add the odd numbers in an array of data.

The numbers are 8-bit unsigned numbers.

The address of the first number is $E000 and the address of the final number

is $E01F.

Save the result in a variable called answer at address $0900.

Start by drawing a picture of the data structure in memory:

5 1 8 6 11

4

SUM ODD NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f

Treat numbers as 8−bit unsigned numbers

Start with the big picture

0xE

SUM ODD 8−BIT NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f

0xE01F

START

Process Entries

Init

Save Answer

Done

5 1 8 6 11

4

Decide on how to use CPU registers for processing data

Init

Done

0 −> Sum

Addr −> Pointer

0xE

SUM ODD 8−BIT NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f

0xE01F

4 5 1 8 6 11

Pointer: X or Y −− use X

Sum: 16−bit register D or Y No way to add 8−bit number to D Can use ABY to add 8−bit number to Y

START

Process Entries

Init

Done

Save Answer

Add more details: Expand another block

Init

Done

0 −> Sum

Addr −> Pointer

SUM ODD 8−BIT NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f

X −> 0xE

0xE01F

4 5

11

8

1

6

Process Entries

Init

Get Num

Even?

Inc Pointer

Add Num to Sum

More to do?

No

No

Yes

Yes

even:

loop:

START

Process Entries

Init

Done

Save Answer

Convert blocks to assembly code

Init

Done

0 −> Sum

Addr −> Pointer

LDY #

LDAB 0,X

ABY

INX

START

Process Entries

Init

Init

Done

0 −> Sum

Addr −> Pointer

Done

Save Answer

SUM ODD 8−BIT NUMBERS IN ARRAY FROM 0xE000 TO 0xE01f

How to test if even? LSB = 0 − check LSB of memory

How to check if more to do? If X < 0xE020, more to do.

BLO loop

BRCLR 0,X,$01,even

BRCLR 0,X,$01,even

Process Entries

Init

Even?

Inc Pointer

Add Num to Sum

More to do?

No

Num

Get

Yes

even:

loop:

Yes

No BLO loop

X −> 4 5 1 8 6 11

LDX #ARRAY

CMPX #ARRAY_END

0xE000 ARRAY

0xE01F ARRAY_END

Write program

;Program to sum odd numbers in a memory array

prog: equ $ data: equ $

array: equ $E len: equ $

CODE: section .text org prog

ldx #array ; initialize pointer ldy #0 ; initialize sum to 0 loop: ldab 0,x ; get number brclr 0,x,$01,skip ; skip if even aby ; odd - add to sum skip: inx ; point to next entry cpx #(array+len) ; more to process? blo loop ; if so, process sty answer ; done -- save answer swi

DATA: section .data org data answer: ds.w 1 ; reserve 16-bit word for answer

Important: Comment program so it is easy to understand.

The map file for the above program

 Note that the map file shows you where your code and data will go, and how much room they will take.  The value of all names are shown.

 The addresses of all labels are shown.

Map of sumodds.h12 from link file sumodds.lkf - Thu Jan 31 21:19:43 2002

Segments:

start 00000800 end 00000800 length 0 segment .text start 00000900 end 00000900 length 0 segment .data start 00000900 end 00000902 length 2 segment DATA start 00000800 end 00000818 length 24 segment CODE start 00000000 end 0000011e length 286 segment .debug

Modules:

sumodds.o: start 00000000 end 0000011e length 286 section .debug start 00000800 end 00000818 length 24 section CODE start 00000900 end 00000902 length 2 section DATA

.11 00000000 .12 00000008 .13 00000013 answer 00000900 array 0000e data 00000900 len 00000020 loop 00000806 prog 00000800 skip 0000080e

Symbols:

THE



STACK AND THE STACK POINTER

Sometimes it is useful to have a region of memory for temporary storage,

which does not have to be allocated as named variables.

When we use subroutines and interrupts it will be essential to have such a

storage region.

Such a region is called a Stack.

The Stack Pointer (SP) register is used to indicate the location of the last item

put onto the stack.

When you put something onto the stack (push onto the stack), the SP is decre-

mented before the item is placed on the stack.

When you take something off of the stack (pull from the stack), the SP is

incremented after the item is pulled from the stack.

Before you can use a stack you have to initialize the Stack Pointer to point to

one value higher than the highest memory location in the stack.

For the HC12 use a block of memory from about $09C0 to $09FF for the

stack.

For this region of memory, initialize the stack pointer to $0A00.

Use the LDS (Load Stack Pointer) instruction to initialize the stack point.

The LDS instruction is usually the first instruction of a program which uses

the stack.

The stack pointer is initialized only one time in the program.

For microcontrollers such as the HC12, it is up to the programmer to know

how much stack his/her program will need, and to make sure enough space

is allocated for the stack. If not enough space is allocated the stack can over-

write data and/or code, which will cause the program to malfunction or crash.

An example of some code which uses the stack

Stack Pointer:

Initialize ONCE before first use

Decreases when you put something on stack

Increases when you take something off stack

(LDS #STACK)

0x09FC

0x09FF

0x09FE

0x09FD

0x09FB

0x09FA

0x09F

0x09F

0x09F

0x09F

0x09F

X

SP

A

lds #STACK

pshx

psha

clra

CODE THAT USES A & X

pulx pula

0x0A

org 0x

CODE: section .text

STACK: equ $0A

ldaa #$2e ldx #$

ldx #$ffff

Points to last used storage location