Quick calculations in assembler
music by COOPER
(C) GreenFort
_
Yes, many problems with this math, and in fact necessary, and
you just everywhere. That vector makes itself know, scoring in
his toy and even the calculation of luminaries in the
firmament. Anyway, there are always situations where speed
PZUshnogo calculator is not enough. We offer you a series of
articles on mathematical operations. It is understood that a
person reading these lines have already mastered the basics of
programming in assembler.
Let's start with a simple:
1. DIVISION.
The principle of the procedure division is based
on long division, the only difference only in the number
system: it is binary.
1.1 Пример_простейшей_процедуры_деления:
INPUT: C = Dividend, Divisor B =
OUTPUT: L = result, A = balance
; L = C / B:
DIVIS XOR A; reset the current OS
; Tatka
DIVIS2 LD L, # 01; counter (shift 8 times)
D1 RL C; read the current discharge
RLA; drive bits
CP B; a result of the current
; Discharge
JR C, ZER; transition if the current
; Bit = 0
SUB B; tek.razryad = 1, de-
; Drive
SLI L; entry level = 1
JR NC, D1; transition, if the counter is not
; Overwhelmed
RET; output
ZER SLA L; entry level = 0
JR NC, D1; transition, if the counter is not
; Overwhelmed
RET; output
L - is used as the counter and stores
result.
A - remainder of the division of C sequentially read bits in
the register A.
B - remains unchanged after division.
Protsedurku can, incidentally, to reveal.
If you need more precision of the case, then you can continue
to divide.
Example:
LD C, that share will
LD B, in that share will
CALL DIVIS; primary division
; C = 0, A-residue
LD H, L; preservation result
CALL DIVIS2; secondary division
; Without zeroing the balance
he findings in HL:
H = integer part of result
L = fractional part of result
The accuracy: + - (1 / 256), ie two
decimal places are provided.
1.2 Protsedurka_deleniya_3-h_baytovyh_chisel
At the entrance: A, H, L - three bytes of the dividend
B, D, E - three bytes of the divisor
At the exit: A, H, L - result
; A, H, L = (A, H, L) / (B, D, E)
DIVISIO
LD C, A; byte divisible
; Store in C
XOR A; zeroing drive
EXX; rezryadov
LD HL, # 01; counter (24) and
LD B, H; three-byte keeper
EXX; result
DIV1SLA L; sample discharge
RL H
RL C
RLA
CP B; drive more
; Divider?
JR C, DIV2; if less, the results
; Tat 0)
JR NZ, DIV3; if more
And the result = 1 drive decreases
EX AF, AF '; preservation A
LD A, C; similar test
CP D; younger of the two bytes for JR C, DIV22; kopitelya and
divider JR NZ, DIV32
LD A, H
CP E
JR C, DIV22
DIV32 EX AF, AF '; drive = divisor
DIV3EX AF, AF '
LD A, L; drive-divider
LD L, H; (lower 2 digits)
LD H, C
AND A
SBC HL, DE
LD C, H
LD H, L
LD L, A
JR NC, DIV33
EX AF, AF '; drive-divider
SUB B; MSB in
DEC A; overflow junior
DIV34 EXX; NeuStar vrezultat
SLI L; current bit = 1
RL H
RL B
EXX
JP NC, DIV1; counter has not yet con
; Creased
JP DIVEXIT; counter overflowed
; The end of the division
DIV22 EX AF, AF '; entry in the result = 0
DIV2EXX
SLA L
RL H
RL B
EXX
JP NC, DIV1; counter has not yet con
; Creased
DIVEXIT EXX; counter overflowed
PUSH HL; end division
LD A, B
EXX
POP HL
RET
DIV33 EX AF, AF '; drive-divider
SUB B; MSB without
JP DIV34; overflow junior
Then, quite convenient to use the result for the addition or
subtraction:
Addition of the result with a 3-byte number
.
CALL DIVISIO
LD B ,1-th byte, which adds
LD DE, 2 and 3 bytes, which adds
ADD HL, DE
ADC A, B
.
2. MULTIPLICATION.
Multiplication, and division as based on
Multiplication of a column.
2.1 Простейшая_процедурка_умножения.
Example:
; HL = B * C
LD HL, # 0; training results at
LD E, B; koef.tekuschego discharge
LD D, H
LD B, # 08; counter
MCYCSRL C; second sample discharge
; Second factor
JP NC, NOADD; bit = 0 (DE * 0 = 0)
ADD HL, DE; bit = 1 (DE * 1 = DE)
NOADD SLA E; replacement ratio times RL D; series on track.
discharge DJNZ MCYC; Cycle 8
RET; output
Of course, immediately apparent that
this construction can be opened.
2.2 Umnozhenie_3-h_baytovyh_chisel.
; A, H, L = A, H, L * B, D, E
MULTI EXX
LD HL, FLAG; dumping transport
RES 0, (HL)
LD B, 24; counter
LD HL, # 0, the result (C, H, L)
LD C, H
PF2 EXX
SRL B; sample discharge
RR D; second factor
RR E
JR NC, PF3; bit = 0, the transition to
; Select the next level
EX AF, AF '; addition to the response
PUSH HL; current koef.razryada
EXX
POP DE
ADD HL, DE
ADC A, C
JP NC, NOFLAG
And the result does not fit into the three bytes!
LD A, (FLAG); handler overflow OR 1; of
LD (FLAG), A
RET
NOFLAG LD C, A
EXX
EX AF, AF '
-PF3 SLA L; move to the next
RL H; current coefficients. (AHL * 2)
RLA
EXX
DJNZ PF2; cycle at 24
LD A, C
RET; output
FLAGDB 0
1 in the variable FLAG in case of overflow the result,
although very often the program all thought and the need is not
there. All possible subsequent operations (addition,
subtraction) with the answer, such as in division.
If you will understand, then we think you will not
easily will be able to remake
procedures and a greater number of bytes
to improve the accuracy of calculations.
We remind you that protsedurki for 3-byte numbers you can use
as for counting integers and fractional.
To be continued.