(C) VA Sirotkin, g.Krasnokamensk.
Software calculate the control
amount.
In some programs, direct
need is to control the accuracy and integrity of the data block
that is loaded or saved to disk. Also
may be necessary to control the dynamic part of the program.
This problem can be solved
checksum.
There are different algorithms and different approaches, but
the most effective of all is the method of cyclic redundancy
(CRC).
CRC algorithm used in
IBM-programs and can detect errors up to a byte. Below is a
listing of procedure calculates a checksum algorithm for CRC.
(This example is taken from the magazine 'Radio' for 1992 and
given without comment).
148.
; --- Input parameters:
; HL-starting block address
; DE-end block address
; --- Imprint:
; BC-checksum block
LEN DEFB 0
START PUSH HL
INC DE
XOR A
LD (LEN), A
LD BC, 0
MET1 PUSH DE
LD A, C
XOR (HL)
LD E, A
PUSH BC
PUSH HL
LD BC, 00
LD D, 8
MET2 PUSH BC
LD A, C
RRA
LD A, B
RRA
LD B, A
LD A, C
RRA
LD C, A
POP HL
LD A, E
XOR L
AND 1
JR Z, MET3
LD A, B
XOR # A0
LD B, A
LD A, C
XOR 1
LD C, A
MET3 LD A, E
RRCA
AND # 7F
LD E, A
DEC D
JR NZ, MET2
POP HL
POP DE
LD A, D
XOR C
LD C, A
LD A, E
XOR B
LD B, A
LD A, (LEN)
INC A
LD (LEN), A
INC HL
POP DE
LD A, H
CP D
JR NZ, MET1
LD A, L
CP E
JR NZ, MET1
DEC DE
POP HL
RET
2
This subprogramme is moveable, ie, can operate in any email
address unless you, of course, specify the address Tagged 'LEN'
somewhere outside of the subprogramme.
Quite accurately tracks changes in counted byte field. Seen
even lying next permutation of bytes. With regard to
performance, the block length of 48 kilobytes counted for 5
sec.
Arithmetic calculations.
As you know, the ROM SOS
built a powerful program calculator that allows both simple
arithmetic operations and actions floating point and
trigonometric calculations.
But to work CALCULATOR
requires a special area - STACK
CALCULATOR, and work with
calculator is quite complicated and
tedious, especially in the programs will be overwritten by the
entire area of RAM.
Sometimes, however, requires short
but effective routines that perform arithmetic operations on
bytes.
Here are some
routines that allow
do without the built-in calculator. All routines can
work anywhere in RAM, ie,
relotsiruemy.
Addition of N-byte positive
real numbers.
In order to add two
number, length of N bytes, it is necessary to register "B"
Record number of bytes in the terms. Terms are entered
originally in region with tags FIRST and SECND.
The result fits into the region with
Tagged FIRST.
148.
; Sub-adding multibyte unsigned
; Numbers
ADDN LD B, N; in the "B" - how many bytes must be
; Fold
LD DE, FIRST; address of the first term
LD HL, SECND; address of the second term
XOR A; carry flag will drop
SUM LD A, (DE); download the first (lowest)
, The number of bytes
ADC A, (HL); add a second number
LD (DE), A; save result
DEC B; all bytes are composed of?
RET Z; if all, the end
INC HL; not, continue adding
INC DE
JR SUM
- -
N EQU? , How many bytes of stack?
FIRST DEFB? ; Range in length (?) Bytes
; For the first day
SECND DEFB? ; Area for the second number
, Length (?)
MULTIPLICATION single-byte
positive integers.
; Multiplicand is entered in the register of 'D'
; Factor in the register of 'C'
; Result in register 'BC'
START LD B, 0, reset the high byte
; Result
LD E, 8; the number of bits in a byte
NXBIT LD A, C; factor
RRA; the next bit in the flag 'C'
LD C, A; return factor
DEC E; counter diminish
RET M; all the bits? DA-emerge
LD A, B; byte result
JR NC, NOADR; flags C'mnozhitelya = 0
ADD A, D; summarize factor
NOADR RRA; move the partial sum
LD B, A; return byte
JR NXBIT; bit multiplication of the following items
PROGRAM DIVISION single-byte
Positive integers.
; Dividend is placed in register 'E'
; Divider is placed in register 'D'
; Private obtained in case 'H'
And in case 'C' produced a positive balance of
; Division
START LD HL, 08; 8 bits in a byte
LD C, 0, reset the register balances
NEXT LD A, E; dividend shift
RLA; left
LD E, A; 1 bit
LD A, C; shift in the balance
RLA; left by 1 bit
SUB A, D; subtract divisor
JR NC, NOADR; balance positive?
ADD A, D; recovery of the negative
; Balance
NOADR LD C, A; tire remember
CCF; form the bits of the private
LD A, H; memorization
RLA; regular
LD H, A; numbers of private
DEC L; decrement bit counter
JR NZ, NEXT; cycle
RET; output
3 routine runs in 660 cycles.
MULTIPLICATION PROGRAM FULL
Single-byte NUMBER OF
Double-byte INTEGER NUMBER
(Positive number).
2-byte multiplicand to put in the 'DE'
, 1-byte factor in the 'A'
And the result we get the registers:
, In 'A' work byte
; In 'HL' lower 16 bits of the product
START LD HL, 0, reset the register works
LD C, 8; counter bits
NXBIT ADD HL, HL; partial sum
RLA; shift factor
JR NC, NOADR; bit multiplier analysis
ADD HL, DE; summarize the multiplicand
NOADR ADC 0; account transfer
DEC C; all 8 bits of the multiplier?
JR NZ, NXBIT; not continue
RET; output
;----------------------------- 2
Binary
Numbers in BCD.
To display the results
number of calculations necessary
result in easy-to-output
form, ie, convert a number to
decimal code. For example, the binary number 00001111 (# 0F) is
conveniently represented as 0001 0101
(# 15), ie convert it into
binary-decimal form. This
deals with the following routine.
148.
; BCD2B-routine transfer of 2-byte numbers.
; Binary number must be in register 'HL'.
; Result:
, In 'A' - tens of thousands;
And in 'B' - thousands and hundreds;
And in 'C' - tens and ones.
; During the procedure is called from CONV
; Sub BCD1B - translate single-byte numbers
, In which;
And in 'H' - the number to be translated, 'L' = 0
; Result: in the 'A' - level of hundreds;
And in 'B' - tens and ones
; Entrance to the procedure for converting 2-byte numbers:
BCD2B LD E, 17, counter 1 st cycle
CALL CONV; calculate Jr. BCD byte
LD C, A; save in 'C'
LD E, 17; counter the 2 nd cycle
JR PRODOL; the transition to the calculation of
; Procedure to convert 1-byte numbers
BCD1B LD E, 9; cycle for the 1-byte numbers
PRODOL CALL CONV; compute the two leading bytes
LD B, A; save an average of bytes
LD A, L; byte
RET; withdraw altogether
;---- CONV XOR A; clean
SBIT DEC E; decrease the loop counter
RET Z; the entire cycle - to leave
ADD HL, HL; move the senior level in
; Transfer
ADC A, A
DAA; adjust
JR NC, SBIT; result is greater than 99?
INC HL; yes - increased by 1
JR SBIT; return to the calculation of
2
********************************