L4K ︰小海龜繪圖《V》

難道 TurtleArt 之『積木』不該以『組合』為依歸的嗎?

電腦裡的『中央處理器CPU Central Processing Unit,是計算機的心臟,從開機後就『不停的』執行著指令,這些 指令構成了 CPU 懂得的指令集,按照  CPU 硬體設計架構分成了複雜指令集 CISC ,比如 PC 上用的 Intelx86,和精簡指令集 RISC ,就像樹莓派上用的 ARM。這些指令資料構成的程式存放在隨機記憶體 RAM ── 可以想像成『編了地址』,可以用任意地址讀寫的『記憶單元』 ── 上,由 CPU 依序『解譯』執行程式裡的指令,存取程式裡的資料。這種『儲存程式』的計算機模型依循著 Von Neumann 先生的計算機『建築學』architecture 而設計。在這個 CPU 指令解譯器裡,有一個『指令地址指標』── 稱作程式計數器 Program Counter ── 告訴 CPU 『下一個』指令在 RAM 上的哪個地址。每個指令不論是『算術邏輯運算』、『記憶體資料存取』和『控制陳述』 …等等的指令執行時,都會『更新』這個程式計數器。於是 CPU 就能從硬體設計上開機時所固有『起始的』地址上『讀取』fetch 第一個指令,然後開始『執行』execution,再讀取下一個指令再執行,一直的讀取.執行持續不停。由於指令與資料並不區分,都放在相同的記憶體上,就可能發生把資料當作指令或是將指令當成資料的情況,一般會導致『當機』。這個前面說的指令集,就是 CPU 唯一會的『機器語言』── 以二進制方式儲存在記憶體上的一長排位元組。即使最簡單的『組合語言Assembler 功能僅僅提供機器語言的『助憶碼』Assembly Mnemonic ,也得把用它寫的程式『組譯』成機器碼 Machine Code,CPU 才能執行;更不要說這個組合語言自己也得能執行才能進行組譯。假使你覺得組合語言很有意思,你可以看看劍橋大學開的烘焙 Pi 開發作業系統公開課;或是裸裸金屬樹莓派編程打算寫的一本書;以及樹莓派組合碼部落客。這裡藉著簡單介紹『思考著的極客』一文上用 Gnu as 組譯程式寫的 first.s 原始碼欣賞程式組譯這回事︰

/* — first.s */
/* This is a comment */
.global main /* ‘main’ is our entry point and must be global */
.func main /* ‘main’ is a function */

main: /* This is main */
mov r0, #2 /* Put a 2 inside the register r0 */
bx lr /* Return from main */

 

人世間所謂『高階、低階』語言已爭議長久的矣??

雖說第一個高階程式語言是德國Konrad Zuse 先生的 Plankalkül,大約構想於第二次世界大戰末期,然而第一個廣為使用的卻是福傳 Fortran ── Formula Translation 的縮寫。1957 年IBM 當時的工程師約翰·華納·巴克斯 John Warner Backus 因深切體會到編寫程式困難,需要更好的程式語言所創。他也就是知名的『 BNF 』巴科斯-諾爾範式 Backus–Naur Form 的創始者之一,這個範式 是一種表示無上下文脈絡關係 Context Free 文法語言,可以用來描述 CF 這一類的形式語言,這包括了絕大部分的電腦程式語言。其後又有了 1969 年在 Bell Labs circa 由 Ken ThompsonDennis Ritchie 所發展的 B 語言,然後於 1971 年之際演變成 New B,最終於 1972 年變成了今天的 C 語言 ── After B ──,形成了美麗的『ABC』語言傳奇

事實上 Von Neumann 的計算機架構,對於電腦程式語言的發展,有著極為深遠的影響,產生了現在叫做 Von Neumann 程式語言,與Von Neumann 的計算機架構,同形 isomorphism 同構

program variables ↔ computer storage cells
程式變數 對映  計算機的儲存單元

control statements ↔ computer test-and-jump instructions
控制陳述  計算機的『測試.跳至』指令

assignment statements ↔ fetching, storing instructions
賦值陳述  計算機的取得、儲存指令

expressions ↔ memory reference and arithmetic instructions.
表達式  記憶體參照和算術指令

John Warner Backus 曾經斷言,由於電腦圈長期過度強調 Von Neumann 的程式語言與計算機架構,已經產生了『惡性循環』,使得非此類的語言由於不符合經濟而日漸式微,比方 APL 語言 ── 註︰有興趣的,可以參照這裡在 Raspbian 上安裝 ──。

行文至此,不得不提及 Charles H. Moore 先生,他認為像 asgcc 那樣的程式語言,依循著 compile-link-go 編譯.連接.執行的步驟都該叫做『第三代』的電腦語言,因而想發明『第四代 FOURTH』語言,但由於 IBM 1130 電腦作業系統,限制檔案名稱長度最多個字元,以至於 FOURTH 被迫變成了現今稱作『Forth』的電腦程式語言。這是一個以『堆疊 Stack』為中心,用『字典』定義『』方式的一種交互式 interactive 的編譯.解譯寫程式的環境,十分有趣獨特。有時還真不好說它算是低階還是高階語言,彷彿是種『由低到高』的程式編寫世界。你可以參考這份文件的說明,進到 Forth 的國度裡『逍遙遊』!!

─── 摘自《CPU 機器語言的『解譯器』

 

或許『結構化編程』實有所指的乎!

Structured programming

Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops—in contrast to using simple tests and jumps such as the go to statement which could lead to “spaghetti code” causing difficulty to both follow and maintain.

It emerged in the late 1950s with the appearance of the ALGOL 58 and ALGOL 60 programming languages,[1] with the latter including support for block structures. Contributing factors to its popularity and widespread acceptance, at first in academia and later among practitioners, include the discovery of what is now known as the structured program theorem in 1966,[2] and the publication of the influential “Go To Statement Considered Harmful” open letter in 1968 by Dutch computer scientist Edsger W. Dijkstra, who coined the term “structured programming”.[3]

Structured programming is most frequently used with deviations that allow for clearer programs in some particular cases, such as when exception handling has to be performed.

Elements

Control structures

Following the structured program theorem, all programs are seen as composed of control structures:

  • “Sequence”; ordered statements or subroutines executed in sequence.
  • “Selection”; one or a number of statements is executed depending on the state of the program. This is usually expressed with keywords such as if..then..else..endif.
  • “Iteration”; a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as while, repeat, for or do..until. Often it is recommended that each loop should only have one entry point (and in the original structural programming, also only one exit point, and a few languages enforce this).
  • “Recursion”; a statement is executed by repeatedly calling itself until termination conditions are met. While similar in practice to iterative loops, recursive loops may be more computationally efficient, and are implemented differently as a cascading stack.

Graphical representations of the three basic patterns using NS diagrams (blue) and flow charts (green).

Subroutines

Subroutines; callable units such as procedures, functions, methods, or subprograms are used to allow a sequence to be referred to by a single statement.

Blocks

Blocks are used to enable groups of statements to be treated as if they were one statement. Block-structured languages have a syntax for enclosing structures in some formal way, such as an if-statement bracketed by if..fi as in ALGOL 68, or a code section bracketed by BEGIN..END, as in PL/I, whitespace indentation as in Python – or the curly braces {...} of C and many later languages.

History

Theoretical foundation

The structured program theorem provides the theoretical basis of structured programming. It states that three ways of combining programs—sequencing, selection, and iteration—are sufficient to express any computable function. This observation did not originate with the structured programming movement; these structures are sufficient to describe the instruction cycle of a central processing unit, as well as the operation of a Turing machine. Therefore, a processor is always executing a “structured program” in this sense, even if the instructions it reads from memory are not part of a structured program. However, authors usually credit the result to a 1966 paper by Böhm and Jacopini, possibly because Dijkstra cited this paper himself.[4] The structured program theorem does not address how to write and analyze a usefully structured program. These issues were addressed during the late 1960s and early 1970s, with major contributions by Dijkstra, Robert W. Floyd, Tony Hoare, Ole-Johan Dahl, and David Gries.

Debate

P. J. Plauger, an early adopter of structured programming, described his reaction to the structured program theorem:

Us converts waved this interesting bit of news under the noses of the unreconstructed assembly-language programmers who kept trotting forth twisty bits of logic and saying, ‘I betcha can’t structure this.’ Neither the proof by Böhm and Jacopini nor our repeated successes at writing structured code brought them around one day sooner than they were ready to convince themselves.[5]

Donald Knuth accepted the principle that programs must be written with provability in mind, but he disagreed (and still disagrees[citation needed]) with abolishing the GOTO statement. In his 1974 paper, “Structured Programming with Goto Statements”,[6] he gave examples where he believed that a direct jump leads to clearer and more efficient code without sacrificing provability. Knuth proposed a looser structural constraint: It should be possible to draw a program’s flow chart with all forward branches on the left, all backward branches on the right, and no branches crossing each other. Many of those knowledgeable in compilers and graph theory have advocated allowing only reducible flow graphs[when defined as?].[who?]

Structured programming theorists gained a major ally in the 1970s after IBM researcher Harlan Mills applied his interpretation of structured programming theory to the development of an indexing system for the New York Times research file. The project was a great engineering success, and managers at other companies cited it in support of adopting structured programming, although Dijkstra criticized the ways that Mills’s interpretation differed from the published work.[citation needed]

As late as 1987 it was still possible to raise the question of structured programming in a computer science journal. Frank Rubin did so in that year with an open letter titled “”GOTO considered harmful” considered harmful”.[7] Numerous objections followed, including a response from Dijkstra that sharply criticized both Rubin and the concessions other writers made when responding to him.

Outcome

By the end of the 20th century nearly all computer scientists were convinced that it is useful to learn and apply the concepts of structured programming. High-level programming languages that originally lacked programming structures, such as FORTRAN, COBOL, and BASIC, now have them.

 

也許『可計算性』與『實務應用』真有所不同的耶!!

故而小海龜繪圖果從善如流也☆

 

repeat   if   if_else  action_flow

 

while   forever   wait_for   stop