为什么 BASIC 标签的增量为 10?

发布于 2025-01-11 03:47:59 字数 867 浏览 2 评论 0原文

在 BASIC 中,标签以 10 为增量。例如,来自 github/linguistmandlebrot.bas

10 REM Mandelbrot Set with ANSI Colors in BASIC
20 REM https://github.com/telnet23
30 REM 20 November 2020
40 CLS
50 MAXK = 32
60 MINRE = -2.5
70 MAXRE = 1.5
80 MINIM = -1.5
90 MAXIM = 1.5
100 FOR X = 1 TO WIDTH
110 FOR Y = 1 TO HEIGHT
120 LOCATE Y, X
130 REC = MINRE + (MAXRE - MINRE) / (WIDTH - 1) * (X - 1)
140 IMC = MINIM + (MAXIM - MINIM) / (HEIGHT - 1) * (Y - 1)
150 K = 0
160 REF = 0
170 IMF = 0
180 K = K + 1
190 REF = REC + REF * REF - IMF * IMF
200 IMF = IMC + REF * IMF + REF * IMF
210 IF REF * REF + IMF * IMF > 4 THEN GOTO 230
220 IF K < MAXK THEN GOTO 180
230 M = 40 + INT(8 / MAXK * (K - 1))
240 PRINT CHR$(27) + "[" + STR$(M) + "m";
250 PRINT " ";
260 PRINT CHR$(27) + "[49m";
270 NEXT Y
280 NEXT X

为什么它不以 1 为增量?那会更有意义。

In BASIC, tags are in increments of 10. For example, mandlebrot.bas from github/linguist:

10 REM Mandelbrot Set with ANSI Colors in BASIC
20 REM https://github.com/telnet23
30 REM 20 November 2020
40 CLS
50 MAXK = 32
60 MINRE = -2.5
70 MAXRE = 1.5
80 MINIM = -1.5
90 MAXIM = 1.5
100 FOR X = 1 TO WIDTH
110 FOR Y = 1 TO HEIGHT
120 LOCATE Y, X
130 REC = MINRE + (MAXRE - MINRE) / (WIDTH - 1) * (X - 1)
140 IMC = MINIM + (MAXIM - MINIM) / (HEIGHT - 1) * (Y - 1)
150 K = 0
160 REF = 0
170 IMF = 0
180 K = K + 1
190 REF = REC + REF * REF - IMF * IMF
200 IMF = IMC + REF * IMF + REF * IMF
210 IF REF * REF + IMF * IMF > 4 THEN GOTO 230
220 IF K < MAXK THEN GOTO 180
230 M = 40 + INT(8 / MAXK * (K - 1))
240 PRINT CHR$(27) + "[" + STR$(M) + "m";
250 PRINT " ";
260 PRINT CHR$(27) + "[49m";
270 NEXT Y
280 NEXT X

Why isn't it just increments in 1? That would make more sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2025-01-18 03:47:59

简而言之,BASIC 编号以 1 为增量,但程序员可以并且确实跳过一些增量。 BASIC 源自 Fortran,Fortran 也使用数字标签,并且通常使用 10 的增量。与 Fortran 不同,早期的 BASIC 需要对所有行进行编号,因此它们从标签变为行号。

BASIC 的编号增量大于 1,以允许在现有行之间添加新行。

  1. 大多数早期的家用计算机 BASIC 实现没有内置的行重新编号方法。
  2. 具有行号的 BASIC 实现中的代码执行按照行号的顺序发生。

这意味着如果您想添加新行,则需要在这些行之间保留空闲的数字。即使在具有 RENUM 实现的计算机上,重新编号也可能需要时间。因此,如果您想要标准增量,通常仍然只在会话结束时或当您认为已基本完成时进行 RENUM。

推测:程序员使用 10 的增量专门用于 BASIC 行号至少有两个原因。第一,传统。当它使用任何标准增量时,那个时代的 Fortran 代码似乎使用 10 的增量作为其标签。第二,外观。在那个时代较小的屏幕上,如果 BASIC 行都以相同的符号结尾,则更容易看到它们的开始位置,而零对于此目的来说是一个非常有用的符号。从个人经验来看,我遵循了在数百个边界和数千个边界上启动不同例程的不稳定传统,以利用行开头的多个零。这使得稍后在阅读代码时更容易识别这些例程的开始。

BASIC 源自 Fortran,Fortran 也使用数字,但作为标签。 Fortran 行仅在需要引用时才需要标签,例如使用 GO TO 来了解可以退出循环的位置,或者作为 WRITE 的 FORMAT。这些行的增量通常也大于 1(通常也为 10),以便在必要时在其间添加更多空间。这在技术上是没有必要的。由于它们是标签而不是行号,因此它们不需要是连续的。但大多数程序员为了可读性而将它们顺序排列。

在他常用的 Fortran 77 教程 ,埃里克·博曼写道:

通常,单个程序中会有许多循环和其他语句需要语句标签。程序员负责为每个程序(或子程序)中的每个标签分配唯一的编号。语句标签的数值没有意义,因此可以使用任何整数。通常,大多数程序员一次将标签增加 10。

BASIC 要求所有行都有编号,并且行号是连续的;这是设置行号的目的之一:BASIC 程序可能会乱序输入。这允许以后进行编辑。因此,可以在添加第 10 行和第 20 行之后添加第 15 行。这使得在现有行号之间保留潜在行号更加有用。

如果您查看包含 BASIC 程序列表的杂志,例如 Rainbow Magazine创意计算,您经常会看到数字随机地夹在十之间。根据风格,许多人在例程或子例程开始时使用比行号少的一个来注释例程。例程和数据部分也可能以数百甚至数千个开始。

使用这样的约定的程序员甚至可能不想对程序重新编号,因为这会扰乱他们的约定。 BASIC 程序通常是大量文本;任何提高可读性的约定都会受到欢迎。

甚至在家用计算机时代之前,十就是普遍接受的间距。在他的 Basic Basic,第二版(1978 年)中,用户将使用“远程终端”),James S. Coan 写道(第 2 页):

程序中相邻行的数量按惯例使用 10 的间隔,但并不要求如此。这是因为程序中的任何修改也必须有行号。因此,您可以使用中间数字来实现此目的。此时知道不必按顺序键入行号应该会令人感到欣慰。无论输入什么顺序,计算机都会按照数字顺序执行程序。

Coan 的 Basic Fortran 中有类似模式的示例。例如,第46页有一个简单的程序“搜索毕达哥拉斯三元组”;第一个标签为 12,其余标签分别为 20、30 和 40。

他使用了类似的模式,但没有以 10 为增量;例如,在Basic Fortran的第132页,Coan使用增量2作为他的标签,并将程序的计算部分保持在数百个,而程序的显示部分保持在两百个。 END 语句使用标签 9900。

同样,在 1982 年的 Elementary BASIC< /a>,亨利·莱德加德和安德鲁·辛格写道(第 27 页):

根据您使用的 Basic 版本,行号可以由 1 到 4 或 5 位数字组成。在这里,所有行号都由 4 位数字组成,这是几乎每个版本的 Basic 都接受的常见做法。行号必须按顺序排列。增加的行号通常以 10 为增量给出,我们也将遵循这一约定。此约定允许您对程序进行少量更改,而无需更改所有行号。

还有 Jerald R. Brown 的 1982 年Instant BASIC:第二版惊人(第 7 页):

您不必按行号顺序输入或键入程序。也就是说,您不必先输入第 10 行,然后输入第 20 行,然后输入第 30 行。如果我们输入的程序不符合行号顺序,计算机不会关心。它遵循行号而不它们输入或键入的顺序。这使得在已经存储的程序中插入更多语句变得容易计算机的内存。您可能已经注意到我们如何巧妙地将程序中的语句按 10 进行编号。这样可以轻松地在现有行号之间添加更多语句 - 例如,在第 10 行和第 20 行之间最多可以添加 9 个语句。

BASIC 程序中如何对行进行编号的大部分选择都是基于传统和对有效方法的模糊认识。在家庭计算机时代尤其如此,大多数用户并不参加如何使用 BASIC 的课程,而是通过阅读其他人的程序、从提供程序列表的许多书籍和杂志中输入这些程序来学习。以 10 递增并在这些增量之间插入新功能的传统是一个显而易见的传统。

您可以看到它扫描旧的代码书籍,例如 101 BASIC Computer Games< /a>.第一个程序“Amazin”将其行号增加 10。但在某些时候,用户/编码员在代码打印出玩家有多少美元后决定需要一个额外的空间;因此,额外的裸露 PRINT 位于第 195 行。游戏说明的显示全部保留在第 100 行和第 109 行之间,这是另一种常见模式。

Basket 第 30 页上列出的程序显示了启动的常见习惯甚至成百上千个单独的例程。这些例程中的行号继续增加 10。即使新功能(可能还有其他模式)在模式之外添加了几行,该模式也相当明显。

随着 BASIC 实现开始获得 RENUM 命令,更多 BASIC 代码列表出现,增量为 1。部分原因是使用增量 1 使用的内存较少。虽然行号本身使用固定数量的 RAM(结果是最大行号通常为 FFFF 左右 或 65525),对行号的引用往往不使用固定长度。因此,较小的行数总体上使用较少的 RAM。

根据程序的大小以及它使用的分支数量,与机器本身拥有的 RAM 量相比,这可能会很重要。

例如,我最近从 输入 SKETCH.BAS 程序1984 年 10 月 Rainbow 杂志,第 97 页。这是一本针对 TRS-80 彩色计算机的杂志和程序。该程序的行编号以 1 为增量。在 CLOAD 程序时,可用内存为 17049。使用 RENUM 10,1,10 以 10 为增量重新编号后,可用内存为 16,953。

节省 96 个字节听起来可能不多,但这是一个非常小的程序;它仍然是可用 RAM 的 0.5%。差异可能是程序适合可用 RAM 或不适合之间的差异。即使内存中根本没有程序,这台计算机也只有 22823 字节的可用 RAM。

The short answer is that BASIC numbering is in increments of one, but programmers can and do skip some of the increments. BASIC grew out of Fortran, which also used numeric labels, and often used increments of 10. Unlike Fortran, early BASIC required numbering all lines, so that they changed from labels to line numbers.

BASIC is numbered in increments greater than one to allow adding new lines between existing lines.

  1. Most early home computer BASIC implementations did not have a built-in means of renumbering lines.
  2. Code execution in BASIC implementations with line numbers happened in order of line number.

This meant that if you wanted to add new lines, you needed to leave numbers free between those lines. Even on computers with a RENUM implementation, renumbering could take time. So if you wanted standard increments you’d still usually only RENUM at the end of a session or when you thought you were mostly finished.

Speculation: Programmers use increments of 10 specifically for BASIC line numbers for at least two reasons. First, tradition. Fortran code from the era appears to use increments of 10 for its labels when it uses any standard increments at all. Second, appearance. On the smaller screens of the era it is easier to see where BASIC lines start if they all end in the same symbol, and zero is a very useful symbol for that purpose. Speaking from personal experience, I followed the spotty tradition of starting different routines on hundreds boundaries and thousands boundaries to take advantage of the multiple zeroes at the beginning of the line. This made it easier to recognize the starts of those routines later when reading through the code.

BASIC grew from Fortran, which also used numbers, but as labels. Fortran lines only required a label if they needed to be referred to, such as with a GO TO, to know where a loop can be exited, or as a FORMAT for a WRITE. Such lines were also often in increments greater than 1—and commonly also 10—so as to allow space to add more in between if necessary. This wasn’t technically necessary. Since they were labels and not line numbers, they didn’t need to be sequential. But most programmers made them sequential for readability.

In his commonly-used Fortran 77 tutorial, Erik Boman writes:

Typically, there will be many loops and other statements in a single program that require a statement label. The programmer is responsible for assigning a unique number to each label in each program (or subprogram). The numerical value of statement labels have no significance, so any integer numbers can be used. Typically, most programmers increment labels by 10 at a time.

BASIC required that all lines have numbers and that the line numbers be sequential; that was part of the purpose of having line numbers: a BASIC program could be entered out of order. This allowed for later edits. Thus, line 15 could be added after lines 10 and 20 had been added. This made leaving potential line numbers between existing line numbers even more useful.

If you look at magazines with BASIC program listings, such as Rainbow Magazine or Creative Computing, you’ll often see numbers sandwiched somewhat randomly between the tens. And depending on style, many people used one less than the line number at the start of a routine or subroutine to comment the routine. Routines and DATA sections might also start on even hundreds or even thousands.

Programmers who used conventions like this might not even want to renumber a program, as it would mess up their conventions. BASIC programs were often a mass of text; any convention that improved readability was savored.

Ten was a generally accepted spacing even before the home computer era. In his basic basic, second edition (1978, and expecting that the user would be using “a remote terminal”), James S. Coan writes (page 2):

It is conventional although not required to use intervals of 10 for the numbers of adjacent lines in a program. This is because any modification in the program must also have line numbers. So you can use the in-between numbers for that purpose. It should be comforting to know at this point that the line numbers do not have to be typed in order. No matter what order they are typed in, the computer will follow the numerical order in executing the program.

There are examples of similar patterns in Coan’s Basic Fortran. For example, page 46 has a simple program to “search for pythagorean triples”; while the first label is 12, the remaining labels are 20, 30, and 40, respectively.

He used similar patterns without increments of 10; for example, on page 132 of Basic Fortran, Coan uses increments of 2 for his labels, and keeps the calculation section of the program in the hundreds with the display section of the program in the two hundreds. The END statement uses label 9900.

Similarly, in their 1982 Elementary BASIC, Henry Ledgard and Andrew Singer write (page 27):

Depending on the version of Basic you are using, a line number can consist of 1 to 4 or 5 digits. Here, all line numbers will consist of 4 digits, a common practice accepted by almost every version of Basic. The line numbers must be in sequential order. Increasing line numbers are often given in increments of 10, a convention we will also follow. This convention allows you to make small changes to a program without changing all the line numbers.

And Jerald R. Brown’s 1982 Instant BASIC: 2nd Astounding Edition (p. 7):

You don’t have to enter or type in a program in line number order. That is, you don’t have to enter line 10 first, then line 20, and then line 30. If we type in a program out of line number order, the computer doesn’t care. It follows the line numbers not the order they were entered or typed in. This makes it easy to insert more statements in a program already stored in the computer’s memory. You may have noticed how we cleverly number the statements in our programs by 10's. This makes it easy to add more statements between the existing line numbers -- up to nine more statements between lines 10 and 20, for example.

Much of the choice of how to number lines in a BASIC program was based on tradition and a vague sense of what worked. This was especially true in the home computer era where most users didn’t take classes on how to use BASIC but rather learned by reading other people’s programs, typing them in from the many books and magazines that provided program listings. The tradition of incrementing by 10 and inserting new features between those increments was an obvious one.

You can see it scanning through old books of code, such as 101 BASIC Computer Games. The very first program, “Amazin” increments its line numbers by 10. But at some point, a user/coder decided they needed an extra space after the code prints out how many dollars the player has; so that extra naked PRINT is on line 195. And the display of the instructions for the game are all kept between lines 100 and 109, another common pattern.

The program listing on page 30 for Basket displays the common habit of starting separate routines at even hundreds and thousands. Line numbers within those routines continue to increment by 10. The pattern is fairly obvious even though new features (and possibly other patterns) have added several lines outside the pattern.

As BASIC implementations began to get RENUM commands, more BASIC code listings appeared with increments of one. This is partly because using an increment of one used less memory. While the line number itself used a fixed amount of RAM (with the result that the maximum line number was often somewhere around FFFF, or 65525), references to line numbers did not tend to use a fixed length. Thus, smaller line numbers used less RAM overall.

Depending on how large the program was, and how much branching it used, this could be significant compared to the amount of RAM the machine itself had.

For example, I recently typed in the SKETCH.BAS program from the October 1984 Rainbow Magazine, page 97. This is a magazine, and a program, for the TRS-80 Color Computer. This program uses increments of 1 for its line numbering. On CLOADing the program in, free memory stands at 17049. After using RENUM 10,1,10 to renumber it in increments of 10, free memory stands at 16,953.

A savings of 96 bytes may not sound like much, but this is a very small program; and it’s still half a percent of available RAM. The difference could be the difference between a program fitting into available RAM or not fitting. This computer only has 22823 bytes of RAM free even with no program in memory at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文