Nicron #30
17 апреля 1997
  Бейсик  

Programming - BASIC calculator.

<b>Programming</b> - BASIC calculator.
(C) WLODEK BLACK

Hello!


            BASIC CALCULATOR

It is well known, and many have met with this: in the assembler
programs sometimes arises the need for complicated
mathematical calculations, algorithms which are beyond the
ADD, SBC, or RR. Programming such a "math" hard ... K
Fortunately, he provides us with the Spectrum tool for
mathematical problems in mashinnokodovyh programs. This
BASIC calculator - a powerful module of the operating system to
which can be accessed as a set of procedures for the
calculations. The calculator consists of an evaluator and
set of procedures for obtaining input data and issue
finished results. Site  purebasic . 

            Stack calculator

Input data, which will operate the calculator,
placed in a designated area of ​​memory - a stack of
calculator. With the engine stack it unites the principle of 
organization - "Last in - first out". Beginning and end of the 
stack calculator can be found in the system variables and STKBOT

STKEND (23651 and 23653; words). Should not be without at
need to change these addresses. One item of data for
Calculator - 5-byte numbers in the format of BASIC or
parameters are string values ​​(starting address and length of 
the string). Thus, each value on the stack calculator takes 5

bytes. Another property of the stack calculator - it grows from 
the bottom up.

To perform any action on numbers, they should be
put on a stack calculator. For example, we must add 5 and
7: send in a stack of 5, then 7, then execute the operation
"Fold." For entry into the stack calculator data available
multiple entry points for the "CALL":

# 2D22 - STK-DIGIT. Accumulator value = digit in ASCII -> stack
(The figures);
# 2D28 - STACK-A. Battery -> stack (in the form of a "floating
comma ");
# 2D2B - STACK-BC. BC -> stack (-"-);
# 2AB1 - STK-STORE. A, E, D, C, B -> stack in 5-byte internal
format, just send string parameters (in this case A = 0);
# 2ABB - the same without checks on the correctness (sometimes 
useful, if the system is deliberately distorted BASIC).


(How many of you had at one time worked with a programmable
calculator Elektronika MK-54 "(61, 52), must immediately
see the complete identity of the stack of the calculator and 
that. There are no psychological barriers - remember how you

used a calculator, and also program the
BASIC calculator ZX! The principle is absolutely the same: 
first, recorded on the data stack, then enter the codes of 
operations on them). Here's how it will look an example of "5 
+7" in the form sequence of instructions in assembly language:


    LD A, 5, number 5
    CALL # 2D28; STACK-A - A enters the stack
    LD A, 7, number 7
    CALL # 2D28

    RST # 28; call calculator
    DEFB # 0F; code addition
    DEFB # 38; completion calculator

    CALL # 2DA2; FP-TO-BC - a stack -> BC.

At the exit from the sample in a pair of BC will be the result 
of calculations. If you call an example of a PRINT USR, be 
printed result. Of course, add numbers such as 5 and 7, we can 
easily and straight into the machine code, but it was only an 
example showing how to work with a calculator.


Team RST # 28 is called a calculator. All subsequent bytes
will be regarded as the operation code calculator, until
meet code # 38 - the symbol of the end of the calculator.
Result of the calculation is placed on top of the current 
stack. His can be removed from the stack, or print on the 
screen or printer or leave on the stack for subsequent 
calculations. That sets procedures to obtain the results of 
calculations: 

# 2BF1 - STK-FETCH. Removes from the stack the last value and
puts it in the registers B, C, D, E, A. If the stack was
parameters of a character string, then: DE - start address, BC 
- length; # 2DA2 - FP-TO-BC. Removes from the stack and places 
high value it in a couple of BC, rounded to the nearest integer 
(int (N +0.5)); # 2DD5 - FP-TO-A - the same, but the placement 
of a 1-byte result a battery;

# 2DE3 - PRINT-FP - prints the current flow (associated with
screen, printer or other device), the value from the top
stack calculator in a form most suitable for the particular
value (whole or real). Options from the top of the stack
calculator is considered as number, if they are
parameters of the line, will be printed as a number (this
would be a mistake);
# 203C - prints the string given by: DE = address of the 
beginning, BC = length. Can be used independently of the 
calculator - easy to print character strings.


In the above example "5 +7" may be inserted at the end of CALL
# 2DE3 to output the result.
When using the PRINT-FP needed to take care of
inclusion of the desired channel and stream to output the 
results: 

    LD A, STREAM
    CALL # 1601

STREAM - channel number:
1 - service line screen;
2 - the main screen;
3 - printer.

The calculator performs the action and on the symbolic values.

Here is a list of operation codes calculator has both
DEFB after "RST # 28":

# 00 - jump true
# 01 - exchange
# 02 - delete
# 03 - subtract (subtraction; X-Y)
# 04 - mult (multiplication; X * Y)
# 05 - div (division; Y / X)
# 06 - to-power (exponentiation; X ^ Y)
# 07 - or
# 08 - no-'-no
# 09 - no-l-eq
# 0A - no-gr-eq
# 0B - nos-eql
# 0C - no-gtr
# 0D - no-less
# 0E - nos-eql
# 0F - add (addition; X + Y)
# 10 - str-'-no
# 11 - str-l-eq
# 12 - str-gr-eq
# 13 - strs-negl
# 14 - str-gtr
# 15 - str-less
# 16 - str-gtr
# 17 - strs-add
# 18 - val $ (recursion, only through the fp-calc-2)
# 19 - usr-s
# 1A - read-in
# 1B - neg
# 1C - code
# 1D - val (recursion, only through the fp-calc-2)
# 1E - len
# 1F - sin
# 20 - cos
# 21 - tan
# 22 - asn
# 23 - acs
# 24 - atn
# 25 - ln
# 26 - exp
# 27 - int
# 28 - sqr
# 29 - sgn
# 2A - abs
# 2B - peek
# 2C - int (Y +0.5)
# 2D - user-no
# 2E - str $
# 2F - chr $
# 30 - not
# 31 - duplicate
# 33 - jump
# 34 - stk-data
# 35 - djnz
# 36 - less-0
# 37 - greater-0
# 38 - end-calc (completion of the work with a calculator)
# 39 - get-argt
# 3A - trunc
# 3B - fp-calc-2 (secondary call calculator - Recursion)
# 3C - e-to-fp
# 3D - restack (Y integer -> Y in the floating form)
# 86 and beyond - series-2006
# A0 - 0 (constant, entering the stack constant)
# A1 - 1
# A2 - 1 / 2
# A3 - PI / 2
# C0 and more - st-mem-0
# E0, and more - get-mem-0

If a single operation - the result replaces the source operand
on top of the stack. If the operation is a two-place - are 
removed from the stack two source operands and the result is 
placed on the new vertex stack. Operations on string values 
​​vyponyayutsya not stack calculator - for them is given a 
place in time workspace on the calculator stack placed only

pointers to the location of the string data: the start address
Line and length (in the registers of the processor is: DE = 
address of the beginning, BC = length). Since one element of 
the stack contains 5 bytes as unused fifth byte is entered 0.


The group of service procedures to Record has the following
subprograms:

# 16B0 - cleaning editorial line, a temporary workspace
and a stack calculator;
# 16BF - clearing the temporary working area and a stack 
calculator; # 16C5 - cleaning up the stack calculator.


In the next issue we consider: input of arbitrary
real numbers; transmission parameters of the USR-sub
BASIC Variable BASIC; input string values ​​in character 
variables USRpodprogrammu through BASIC (for example, WORDSERV 
utilities, etc., in which the file names are entered through 
INPUT BASIC, and are used in mashinnokodovyh programs). 

To be continued.





Other articles:

Entry - the contents of rooms.

BBS - list of stations BBS ZXNet.

Soft - a bug in NICRON'e.

Soft - a glitch in the game PRINCE OF PERSIA.

Programming - BASIC calculator.

Story - make a bomb, part 2.

Search - search for game programs.

military enlistment office - how to avoid conscription.

Humor - Dembelskaya rocket ordinary Kochkurkina.

Humor - anecdotes.

Advertising - advertising and announcements.

Feedback - contact the publisher.


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

Similar articles:
Studies - Program - cataloger of disks.
Iron - We investigate the chip K561TL1. .
TOP ZX - Free games, magazines, sistemki ...

В этот день...   23 November