• Aucun résultat trouvé

IO DEFINING CONSTANTS, VARIABLES, AND ARRAYS I0.1 CONSTANTS

Dans le document July CAT (Page 194-200)

oel& 0>

C. IO DEFINING CONSTANTS, VARIABLES, AND ARRAYS I0.1 CONSTANTS

A constant is a dictionary entry which causes a 32-bit integer to be pushed on the parameter stack. Once a constant is defined, its value is not intended to be changed at run time.

To define a constant, use the word "CONSTANT":

VALUE 'NAME CONSTANT

Here, "VALUE" is the number on the top of the stack and "NAME" is the name to be assigned to the constant. When you execute "NAME", "VALUE"

is pushed on the stack.

EXAMPLE:

5 'FIVE CONSTANT

This sets up a dictionary entry with name "FIVE". Executing the word

"FIVE" causes a 5 to be, pushed on the stack.

C.IO.2 VARIABLES

A variable is a dictionary entry which contains a 32-bit integer as its value. The value of a variable can be changed during program execution. When executed, it causes the address of its value to be pushed on the parameter stack.

Variables are defined as follows:

VALUE 'NAME VARIABLE

"VALUE" is the number on the top of the stack and "NAME" is the name to be assigned to the variable. "VALUE" is used to set the initial value of the variable. It is not an address where you want the variable to be stored.

EXAMPLE:

WICAT DIAGNOSTIC MONITOR (DIAMOND)

DEFINING CONSTfu~TS, VARIABLES, AND ARRAYS

100 'X VARIABLE

This defines a variable

"X"

with an initial value of 100.

C.IO.3 ARRAYS

Although DIAMOND has no built-in array handling facility, its ability to perform address arithmetic makes subscripting possible.

There are several methods for setting aside storage for an array. The simplest is to use the word "ARRAY":

LENGTH 'Nfu'1E ARRAY

This defines and zeros an array whose length (in 32-bit words) and name are specified. The array is just a variable with extra storage locations reserved. Referencing an array causes the address of the zeroth element to be pushed on the stack. (The elements run from a thru "LENGTH"-l.)

EXAMPLE:

100 'BUFFER ARRAY

This defines and zeroes a lOa-word array named "BUFFER".

C.lO.3.1 REFERENCING ARRAY ELEMENTS

To reference an element of an array, all that is necessary is to add an appropriate offset to the address of the zeroth element.

EXAMPLE:

NOTE

Since the first element has offset zero, the Nth element has offset N-l.

C-88

DEFINING CONSTANTS, VARIABLES, AND ARRAYS

Multidimensional subscripting is handled in a similar fashion.

EXAMPLE:

WICAT DIAGNOSTIC MONITOR (DIAMOND)

DEFINING CONSTANTS, VARIABLES, AND ARRAYS

Or, calling the row number J and the column number I:

<index>

=

10*J + I

Translating this last expression into RPN, the index can be written as:

J 10

*

I +

which is the expression in the middle of the second line of the example:

10 0 DO 10 0 DO I J + J 10

*

I + 4* X + LOOP LOOP

Since each element of the array is a longword (4 bytes long), the displacement from the beginning of the array is 4*<index> bytes, so this displacement is computed and added to "X", which is the starting address of the array, to get the address where an element should be stored:

10 0 DO 10 0 DO I J + J 10 * I + 4* X +! LOOP LOOP The following part of the statement computes I+J:

10 0 DO 10 0 DO I J + J 10 * I + 4* X +! LOOP LOOP So, the statement is of the form:

10 0 DO 10 0 DO <value> <address>

where:

<value>

=

"I J +"

<address>

=

"J 10

*

I + 4* X +"

LOOP LOOP

(the value to be stored at location I,J)

(the location of the I,J element)

The nested DO LOOPs run:I and J through the values 1 through 10.

C-90

C.11 THE DICTIONARY

THE DICTIONARY

The dictionary starts in low core and grows upward toward the top of memory. As each definition is made, it is appended to the high memory end of the dictionary.

The following word gets the address of a dictionary entry:

, NAME ADDRE S S

This word pushes two values on the stack if its execution is successful:

1. The address of "NAME". (Placed next to top of the stack) 2. A value which reflects the success or failure of the

operation. (Placed on top of the stack)

For example:

If the word "GORK" is defined:

0] 'GORK ADDRESS

-o

204078

If the word "GUCK" is not defined:

0] 'GUCK ADDRESS

=

-1

(There is no second element.)

WICAT DIAGNOSTIC HONITOR (DIAMOND) STRING HANDLING

C.12 STRING HANDLING

Executing a string literal causes a pointer to the length word of the string to be pushed on the stack.

EXAMPLE:

'LARK: 'NONSENSE. TYPE ;

Executing "LARK" causes "NONSENSE" to be typed.

C-92

NUMBER OUTPUT CONVERSION

C.l3 NUMBER OUTPUT CONVERSION

DIAMOND pe.'fforms number conversion using a small but powerful set of words which permit a variety of output formats to be generated.

RADIX Variable which contains current input and output radix.

TYO Convert the number on the top of the stack to an ASCII character.

Higher level words are used to provide number output in a default format:

=U Convert and output the number on the top of the stack.

(unsigned)

Convert and output the . number on the top of the stack.

(signed) stack. (signed)

=F Convert and output the number next to the top of the stack, displaying the number of digits specified by the number on top of the stack. (signed) If the number to be displayed requires fewer digits than specified, the output will be left zero filled.

=UF Convert and output the number next to the top of the stack, displaying the number of digits specified by the number on top of the stack. (unsigned) If the number to be displayed requires fewer digits than specified, the output will be left zero filled.

Dans le document July CAT (Page 194-200)

Documents relatifs