manual.txt
来自「open source of basic interpreter of 68」· 文本 代码 · 共 1,901 行 · 第 1/5 页
TXT
1,901 行
Page 25
Comparisons
SB68k supports a wide range of comparisons, for use with
control structures such as IF-ELSE-ENDIF and DO-LOOP. All
comparisons test two 32-bit values and return TRUE if the
values meet the comparison test.
= (equal-to) yields TRUE if the two 32-bit values are equal.
< (less-than) yields TRUE if the first 32-bit value is less
than the second 32-bit value. This is a signed comparison;
$80000000 is less than 0.
> (greater-than) yields TRUE if the first 32-bit value is
greater than the second 32-bit value. This is a signed
comparison; 0 is greater than $80000000.
<> (not-equal-to) yields TRUE if the two 32-bit values are
not equal.
>< (not-equal-to) yields TRUE if the two 32-bit values are
not equal.
<= (less-than-or-equal-to) yields TRUE if the first 32-bit
value is less than or equal to the second 32-bit value.
This is a signed comparison.
>= (greater-than-or-equal-to) yields TRUE if the first 32-
bit value is greater than or equal to the second 32-bit
value. This is a signed comparison.
<* (less-than unsigned) yields TRUE if the first 32-bit
value is less than the second 32-bit value. This is an
unsigned comparison; 0 is less than $80000000 unsigned.
>* (greater-than unsigned) yields TRUE if the first 32-bit
value is greater than the second 32-bit value. This is an
unsigned comparison; $80000000 is greater than 0 unsigned.
SB68k does not allow you to store the result of a comparison
in a variable. SB68k also does not allow multiple
comparisons in a single operation. Control structures such
as IF-ELSE-ENDIF can use only one comparison in the IF-
clause.
Page 26
Control structures
SB68k supports several structures for controlling the flow
of your program. Used properly, these control structures
can improve the quality of your program design, making your
source file easier to read, understand, and debug.
Some of the following control structures allow or require a
comparison clause. Such clauses consist of an expression, a
comparison operator, and a second expression. The
comparison clause is evaluated as your program runs and,
depending on the evaluation, control transfers within the
control structure.
Example:
while a < 5
a = a + 1
wend
Here, the comparison clause "a < 5" determines whether
control remains in the WHILE-WEND loop or transfers to the
line following the WEND statement.
All comparison clauses may contain one and only one
comparison operator. Multiple comparisons, such as:
while a < 5 and c-1 = 3
are illegal and will generate compilation errors.
Note also that you do not enclose comparisons within
parentheses. Doing so will result in a compilation error.
WHILE-WEND is a conditional loop structure. Control
executes all statements between the WHILE statement and the
WEND statement for so long as the comparison in the WHILE
clause is TRUE. When the comparison becomes FALSE, control
exits the loop by transferring to the line following the
WEND statement.
Example:
while a < 500 ' loop while a is less than 500
a = a + 1 ' increment a
wend ' end of loop
This example loops for so long as the value in A is less
than (signed) 500.
Note that WHILE-WEND is obsolete, even though SB68k still
supports it. WHILE-WEND has been replaced by the more
flexible DO-LOOP structure.
Page 27
DO-LOOP is a loop structure that may be either conditional
or unconditional. Control executes all statements between
the DO statement and the LOOP statement. You may include an
optional comparison clause in either the DO statement or in
the LOOP statement.
Examples:
do ' start of a do-loop
gosub process ' do something useful
loop ' end of loop
The above example will loop forever, since it has no
comparison clause. An endless loop is often used as the
core of a large program.
do while a < 500 ' loop while a is less than
500
a = a + 1 ' increment a
loop ' end of loop
This example mimics the WHILE-WEND loop above.
do until a > b ' loop until a is greater than
b
a = a + c/2 ' change value of a
loop ' end of loop
The above example loops until the value in A is greater
(signed) than the value in B.
do ' start of a do-loop
a = peekb($1000) ' read a value from an I/O
port
loop while a = 0 ' loop while a equals 0
The above example loops for so long as the value read from
the I/O port equals 0.
do ' start of a do-loop
a = peekb($1000) ' read a value from an I/O
port
loop until a = $ff ' loop until a equals $ff
The above example loops until the value read from the I/O
port equals $ff.
The DO-LOOP provides great flexibility for loop control,
because you can control when the comparison occurs in the
Page 28
loop. If you place the comparison in the DO statement, the
test is performed before the body of the loop is executed.
By placing the comparsion in the LOOP statement, you can
force the body of the loop to execute before the comparison
is performed.
The FOR-NEXT structure creates an iterated loop. This means
that a selected variable, called the index variable,
controls exactly how many times the loop is executed.
Control executes all statements between the FOR statement
and the NEXT statement until the value in the index variable
EXCEEDS a specified limit. The index variable is always
tested at the top of the loop. The comparison is signed for
the usual FOR-NEXT loop, although you can use an unsigned
comparison if necessary.
Example:
for n = 1 to 10
a = a + n
next
Here, the variable N starts with a value of one. The
statement inside the loop is executed ten times, with N
incrementing each time the NEXT statement executes.
Eventually, N holds the value 11 when the FOR statement
executes. At this point, the statement inside the loop is
not executed. Instead, control passes directly to the
statement following the NEXT statement.
Sometimes you must use a limit larger than $7fffffff. Since
SB68k uses 32-bit math, numbers larger than $7fffffff are
treated as negative in signed comparisons. Therefore, the
following example:
for n = 1 to $90000000
a = a + 1
next
will exit immediately, as SB68k treats $90000000 as a
negative number, and 1 is already greater than a negative
number.
To change the above example to use an unsigned comparison,
use the TO* operator. The above example becomes:
for n = 1 to* $90000000
a = a + 1
next
This loop will execute the expected $90000000 times.
Page 29
Remember to leave room on your limit value so that the index
variable can actually exceed the limit. For example:
for n = 1 to* $ffffffff
a = a + 1
next
This loop will never end, since the value of N can never
exceed the limit of $ffffffff.
Note that it is legal (but not good practice) to modify the
index variable inside the loop.
Normally, the index variable increments by one each time
control reaches the bottom of the loop. You can change the
value by which the index variable increments with the STEP
operator.
Example:
for n = 1 to 10 step 2
a = a + n
next
Here, the variable N starts with a value of one, and
increments by two each time control reaches the NEXT
statement. Thus, N takes on the values 1, 3, 5, 7, 9, and
11. When N becomes 11 at the top of the loop, control
immediately passes to the statement following the NEXT
statement.
The SELECT statment marks the beginning of a SELECT-CASE
control structure. The SELECT-CASE structure allows your
code to select one option out of a list, based on the value
of an argument, called the selector. Only the CASE
statement associated with the matching selector value, if
any, is executed.
The general format of a SELECT-CASE structure is:
select foo ' use value of FOO as selector
case 123 ' if FOO = 123...
. ' execute this code
. '
endcase ' end of case 1
case 456 ' if FOO = 456...
. ' execute this code
. '
endcase ' end of case 2
endselect ' end of select structure
Page 30
This structure replaces the older ON-GOTO construct, and
allows creation of code that is easier to debug and
maintain.
The SELECT-CASE structure supports variations for handling
special cases. For example:
select foo + 3*j ' use expression for selector
case 1 ' if selector = 1...
case n ' or N...
case 'X' ' or ASCII character X...
foo = n + 3 ' change FOO
endcase ' end of first case
case 2 ' if selector = 2...
case 3 ' or 3...
foo = n + 2 ' change FOO
endcase ' end of second case
foo = 4 ' default action...
endselect
This example shows the use of multiple CASE statements
within a CASE clause. This feature comes in handy if your
code must perform the same function for a group of selector
values.
This example also shows the method for performing a default
action. The line FOO = 4 executes if no CASE clause matches
the selector value. Note that the default clause does not
require an initial CASE statement or a terminating ENDCASE
statement.
Also, this example shows that the selector value for this
example is an expression. You can use any valid algebraic
expression as the selector value in a SELECT statement.
Note, however, that CASE statements only accept a numeric
value, a constant, or a variable as an argument. You cannot
use an algebraic expression as the argument to a CASE
statement! Doing so will generate a compiler error.
Finally, this example shows that you can change the selector
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?