Nicron #31
24 апреля 1997 |
|
Programming - BASIC Calculator (continued).
WLODEK BLACK CALCULATOR BASIC (Continued) Work with real numbers in machine code, of course, more complicated than in BASIC, but not so much that it becomes incomprehensible. When using the calculator of the codes we must first clearly separate the data on the constants and changing along the way the data (do not want to use the term "Variables"). Constants makes sense to calculate in advance and recorded in the body of the program as a finished product - a 5-byte internal representation. Determine the value of the constants in the 5-byte representation easily directly in BASIC. Type the following line: 10 PRINT 2.7182818 (This is the number of "e"). After seeing the contents of memory at address 23872 (If TR DOS initialized), we find a code sequence: F5 32 2E 37 31 38 32 38 31 38 0E 82 2D F8 54 3A P 2. 7 1 8 2 8 1 8 R I N T Marker # 0E shares ASCII-record numbers, and an internal 5-byte format. Writing off somewhere, and the values of 5-byte stand after # 0E, we remember the constant value. Data after # 0E intended to register in the following order: 0E 82 2D F8 54 3A A E D C B Zanesya them in the processor registers in this order and calling then the procedure STK-STORE (# 2AB2), we obtain a stack Calculator value desired number. It is clear that by writing after the "PRINT" any number of viewing and then the memory can be learn 5-byte representation of any real value in range permitted by BASIC. Attention - glitch! And apologize. In the previous issue has crept inaccuracy. Here is the correct list of procedures STK-STORE: # 2AB1 - entry into the stack string parameters (A reset); # 2AB2 - entry into the stack 5-byte value numbers; # 2ABB - entry in the register stack A, E, D, C, B without control. There is the so-called "packaged" form of numbers, requires only 4 bytes to store values. To be frank, never tried to use it. In one clever books mentioned "glitch" packaged forms: 0.5 <> 1 / 2. Oh well ... If the calculations or stage I / O will be found any incorrectness, BASIC conversion to occur through RST # 08 and a stack of return by mistake. In other words, if the system BASIC was not modified (corrupted), for any traditional BASIC error would lead to "escape" of the user program in Normal mode for entering commands from the BASIC error message at the bottom screen. If this is unacceptable, it is necessary reorganize the stack return by mistake (ERR_SP). Honestly speaking, I do not know whether we should now tell you how it's done. Let's proceed like this: you write to me or to the address of the editorial board, If a story is needed, and it will appear. If in the course of the implementation program will require mashinnokodovoy enter numeric data from the outside, then at least with the keyboard it is wise to do as a normal character figures, as we enter the data to a request for INPUT. INPUT simulate the codes are not costs, better ensure that you enter a character string in any known to you the way and further transform introduced sequence of characters in the internal 5-byte representation of numbers. So we have: symbolic image numbers, should contact: the internal representation of numbers. Very easily this is done by evaluating the expression VAL "string". Parameters of the input string (starting address and length) are recorded on stack calculator, and then executes the function VAL: LD DE, ASC_A; address of the start line LD BC, ASC_B-ASC_A; length of the string CALL # 2AB1; STK-STORE for string parameters LD B, # 1D; function code VAL RST # 28; call calculator DEFB # 3B; code of the function fp-calc-2 ; At this point in the stack is an internal representation of DEFB ....... ; Codes of other operations calculator DEFB # 38, the output from the calculator ........... ASC_A DEFM "2.7182818"; character string representing the number of ASC_B Function VAL in his work is a calculator for recursively perform multiple recalculations of values ASC-codes and other intermediate values. Because of this, it can not be included in the overall List DEFB opcode calculator, as in this case other data are lost, and there is some mistake BASIC. To call a recursive function is provided special operation "fp-calc-2" - secondary call calculator. When using fp-calc-2 operation code is put into register B, and the body put the code DEFB # 3B - appeal to the fp-calc-2. After # 3B codes may be located downstream operations calculator. The same should apply to all functions, evaluates the expression including the string - VAL $. A string of characters defining number may not be an image of itself - in line You can write any expression, even with variable BASIC, and it will be evaluated! Not hard to imagine that this kind of work involved and the BASIC interpreter. If the function VAL (or VAL $) is only called to convert the expression, and then immediately carried out from the calculator (Say, for the preparation and input other data), access to VAL You can podsokratit, causing it not through the fp-calc-2, and directly CALL through the well-known address: ... bummer! The handbook ochepyatka ... Address VAL $ - # 35DE, and VAL -? Well, okay, VAL $, so VAL $. We show the use of VAL $. Here is a fragment of some small utilities that I use to process server disk processing "history", and similar concerns sysop: BASIC: INPUT "Source file:"; a $: INPUT "Destination file:"; b $: RANDOMIZE USR .... Codes: LD DE, VARLIN; address of string "and $" LD BC, 2, string length CALL # 2AB1; STK-STORE CALL # 35DE; VAL $ CALL # 2BF1; STK-FETCH ; At this place in DE - the address of a character variable a $, ; In BC - the length of a variable $ LD A, C; check no more than 8 characters in it? CP 9 JR C, VL8; if not more than 8 characters LD C, 8; otherwise take only the first 8 characters VL8 LD B, 0; it just in case - maybe someone else ; 264 characters introduce? :-) LD HL, FILNAM; buffer address file name EX DE, HL LDIR; file name of the variable a $ forwarded to clipboard ............. VARLIN DEFM "A $"; "image" of a character expression, consisting only of one variable a $ ............. FILNAM DEFM ""; buffer file name And so, without much difficulty, and, most importantly, very quickly is a name to a file in a buffer, from which then file name is used in accordance with the algorithm of the program. CALL # 35DE replaces the chain: LD B, # 18; opcode VAL $ RST # 28 DEFB # 3B, # 38, that is 3 bytes instead of 5, plus an increase in speed of execution. Of course, if after DEFB # 3B followed by other opcodes calculator, it is wiser to call a VAL $ fp-calc-2. Through the VAL and VAL $ can be easily calculated various useful functions type RND, INKEY $, etc: RND = VAL CHR $ 165; INKEY $ = VAL $ CHR $ 166. Moreover, VAL and VAL $ from a single simple value (not the expression) can be calculated directly without fp-calc-2. But still better not to risk it!
Other articles:
Similar articles:
В этот день... 21 November