beautypg.com

Programming task examples, Table 2, Bubble listing – Zilog Z08470 User Manual

Page 41

background image

UM008007-0715

Programming Task Examples

Z80 CPU

User Manual

29

SBC

A, (HL)

;SUBTRACT (HL) FROM ACC

DAA

;ADJUST RESULT TO DECIMAL CODED VALUE

LD

(HL), A

;STORE RESULT

INC

HL

;ADVANCE MEMORY POINTERS

INC

DE

DJNZ

SUBDEC–$

;DECREMENT B AND GO TO SUBDEC

;IF B

;NOT ZERO, OTHERWISE FALL

;THROUGH

Seventeen bytes are required for this operation.

Programming Task Examples

As indicated in Table 2, this example program sorts an array of numbers to ascending
order, using a standard exchange sorting algorithm. These numbers range from 0 to 255.

Table 2. Bubble Listing

Location

Object
Code

Statement

Source Statement

1

;

standard exchange (bubble) sort routine

2

;

3

;

at entry:

hl contains address of data
c contains number of elements to be sorted
(1 < c < 256)

4

5

6

;

7

;

at exit

data sorted in ascending order

8

;

9

;

use of
registers

10

;

11

;

register

contents

12

;

13

;

a

temporary storage for calculations

14

;

b

counter for data array

15

;

c

length of data array

16

;

d

first element in comparison

17

;

e

second element in comparison

18

;

h

flag to indicate exchange

This manual is related to the following products: