ZX Format #07
05 декабря 1997

Programmers - algorithms and examples of procedures for quick calculations on asemblere: multiplication and division of 3-byte numbers

<b>Programmers</b> - algorithms and examples of procedures for
quick calculations on asemblere: multiplication and division of 3-byte numbers
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.




Other articles:

From the authors - a few words about the next issue.

Ator magazine - ZX-Format No.7

Content - short contents of the magazine ZX-Format No.7

Toys - an overview of new products: Los Angeles Drugs Bust, Jungle Warfare, Dragonia, 3D Starfighter, Rock star ate my hamster, Star Cobtrol, Malstese joe's pool challenge, Dynamite dux, Blood Brodthers, Galactic Games, Motor Masscre, International Kickboxing.

Toys - Tai-Pan: a masterpiece of classic ZX Spectrum. The game that combines the quest and the manager. Interesting story and a rather detailed description.

Toys - Operation R.R. Opening a new graphic novel from quest'u GALAXY.

Toys - Adventyury: description of the game-parody "Bored of the ring" - "sick of the Rings" can be used as a description, but can simply be read as a parody of JRR Tolkien.

Toys - 3D construction kit: a detailed description of the editor of 3D games such as Castle Master. The article describes the internal language sistemmy, which in fact, makes a set of locations and objects in a full game.

Programmers - Basic - 7: The End of the description of Beta-Basic.

Programmers - library of mathematical procedures. Series of articles for those wishing to learn how to program in assembly language.

Programmers - algorithms and examples of procedures for quick calculations on asemblere: multiplication and division of 3-byte numbers

Programmers - Mod Format files. Describe the format of the standard MOD file used in the General Sound.

Programmers - BMC 2. Esche few words about the conversion of PC-shnyh images in 3-color.

Review - An overview of the passage of the regular Demo-party ENLiGHT 1997 with photographs of some of the guests.

Review - A few moments with the exhibition of video and audio Video FAIR, held in autumn in Saint-Petersburg.

Iron - Captain Nemo invites to share technology KAY all willing to do production.

Iron - Information on a new project by Scorpion - motherboard expansion graphics and memory GMX.

Iron - An article about the audio amplifier and a price list (for those wishing to buy a ready-amp or kit assembly).

animated cartoon - Description of a new turbo-assembler from STORM X-Trade.

animated cartoon - Riff Tracker New Music Editor for General sound.

animated cartoon - Description tekstovgo editor of ZX-Winword, allowing to make graphical inserts in the text, and its utility Sprite Cutter - Cutting these insertions.

animated cartoon - Head Ball - Golovobol: Description of a new game.

Interview - interviews with participants ENLiGHT'97: Digital Reality.

Interview - interviews with participants ENLiGHT'97: Extreme.

Interview - interviews with participants ENLiGHT'97: MMA, ALK.

Interview - interviews with participants ENLiGHT'97: Flash inc.

Interview - interviews with participants ENLiGHT'97: Progress.

Interview - interviews with participants ENLiGHT'97: Felix / Softland.

Interview - interviews with participants ENLiGHT'97: Slash.

Interview - interviews with participants ENLiGHT'97: Vyacheslav Mednonogov.

It was you - Lord of the teeth - 2. Continuation of the adventures of the brave Hobbits.

It was you - Switch - A small philosophical parable.

Mail - A masterpiece. Another interesting letter - "the majority of applications and system software - complete nonsense, and Labuda!

Mail - readers' letters: Nikolai Sergeyev, Jobman, Nikolai Parfenov, Dmitry Zalissky.

Mail - free advertising and announcements.

Miscellaneous - prospects for the software. A new RPG game from Odessa Group "Oberon" - an unknown world.

Miscellaneous - prospects for the software. STATE OF THE ART: a new graphical editor for the Spectrum.

Miscellaneous - Electronic Samizdat: A few words about a new kind of software - Electronic books.

Amiga Club - A new article of the former Spektrumista Max Petrov on Amiga.


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
Interview - Interview with Vyacheslav Mednonogovym (Copper Feet).
Championship - Report from passing under the auspices of the journal Open Championship Viruses.

В этот день...   3 May