程式語言 • 語言是用來溝通的工具, 人類有人類的語言, 電腦也有電腦的語言。我們必須將要電腦 做的事情,以電腦了解的語言來表達, 電腦才 能替我們完成工作。要和電腦溝通就要用 程式語言 (Programming Language)。 • 程式就是利用程式語言的敘述, 遵照其一定 的規則及所要處理工作的順序, 編排而成的 一連串命令。 7-1 程式語言 • 依電腦可接受的程度來區分, 程式語言可分 為以下幾種: 7-2 機械語言 • 電腦所使用的語言就是機械語言 (Machine Language) , 它們都是由 0 與 1 數字所組成, 跟人類的思考模式完全不同, 使得學習與撰 寫上都十分困難, 更別說是拿來發展程式。 7-3 組合語言 • 組合語言與機械語言仍然相當接近, 它改 用一些特別的字元來代替一大串 0與 1 所 組成的機械語 言碼, 增加了 程式的可讀 性。 7-4 組合語言 • 由於以機械語言與組合語言來編寫程式需 要非常瞭解電腦的內部構造, 並且直接控制 電腦硬體, 因此我們稱此兩者為低階語言。 • 對於缺乏電腦專業知識背景的人, 這兩種語 言學習起來還是滿困難的, 所以才有以下高 階語言的發展。 7-5 高階語言 • 高階語言是比較接近人類使用的電腦程式 語言。這類電腦語言採用類似英文的字彙 及數學運算式的語法來敘述人類要電腦做 的事, 因此用高階語言撰寫程式比較輕鬆, 而程式也比較容易閱讀。 • 目前較常用的高階語言有:Visual Basic、 Delphi、Java...等;早期流行的有BASIC、 FORTRAN、COBOL、C...等。 7-6 Chapter 7 Low-Level Programming Languages Computer Operations • A computer is a programmable electronic device that can store, retrieve, and process data • Data and instructions to manipulate the data are logically the same and can be stored in the same place • Store, retrieve, and process are actions that the computer can perform on data Machine Language • Machine language The instructions built into the hardware of a particular computer • Initially, humans had no choice but to write programs in machine language because other programming languages had not yet been invented Machine Language • Every processor type has its own set of specific machine instructions • The relationship between the processor and the instructions it can carry out is completely integrated • Each machine-language instruction does only one very low-level task Pep/7: A Virtual Computer • Virtual computer A hypothetical machine designed to contain the important features of real computers that we want to illustrate • Pep/7 – designed by Stanley Warford – has 32 machine-language instructions • We are only going to examine a few of these instructions Features in Pep/7 • The memory unit is made up of 4,096 bytes • Pep/7 Registers/Status Bits Covered – The program counter (PC) (contains the address of the next instruction to be executed) – The instruction register (IR) (contains a copy of the instruction being executed) – The accumulator (A register) – Status bit N (1 if A register is negative; 0 otherwise) – Status bit Z (1 if the A register is 0; and 0 otherwise) Features in Pep/7 Program Counter 應為16bits,雖然 前4碼都為0 Figure 7.1 Pep/7’s architecture Instruction Format • There are two parts to an instruction – The 8-bit instruction specifier – And optionally, the 16-bit operand specifier Figure 7.2 The Pep/7 instruction format Instruction Format • The instruction specifier is made up of several sections – The operation code – The register specifier – The addressing-mode specifier Instruction Format • The operation code specifies which instruction is to be carried out • The 1-bit register specifier is 0 if register A (the accumulator) is involved, which is the case in this chapter. • The 2-bit addressing-mode specifier says how to interpret the operand part of the instruction Instruction Format Figure 7.3 Difference between immediate-mode and direct-mode addressing Some Sample Instructions Figure 7.3 Subset of Pep/7 instructions A Program Example • Let’s write "Hello" on the screen Page 200 Pep/7 Simulator • A program that behaves just like the Pep/7 virtual machine behaves • To run a program, we enter the hexadecimal code, byte by byte with blanks between each Page 202 Assembly Language • Assembly languages A language that uses mnemonic codes to represent machine-language instructions – The programmer uses these alphanumeric codes in place of binary digits – A program called an assembler reads each of the instructions in mnemonic form and translates it into the machine-language equivalent Pep/7 Assembly Language Figure 7.5 Assembly Process Pr0502 CHARI h#000D,d ;Input first character CHARI h#000E,d ;Input second character CHARO h#000E,d ;Output second character CHARO h#000D,d ;Output first character STOP .BLOCK d#1 ;Storage for first char .BLOCK d#1 ;Storage for second char .END Assembler Listing Object Addr code 0000 D9000D CHARI h#000D,d ;Input first character 0003 D9000E CHARI h#000E,d ;Input second character 0006 E1000E CHARO h#000E,d ;Output second character 0009 E1000D CHARO h#000D,d ;Output first character 000C 00 STOP 000D 00 .BLOCK d#1 ;Storage for first char 000E 00 .BLOCK d#1 ;Storage for second char 000F Mnemon .END Operand Comment Pr0502-modified CHARI c1,d ;Input first character CHARI c2,d ;Input second character CHARO c2,d ;Output second character CHARO c1,d ;Output first character STOP c1: .BLOCK d#1 ;Storage for first char c2: .BLOCK d#1 ;Storage for second char .END Assembler Listing Object Addr code Symbol Mnemon 0000 D9000D CHARI c1,d ;Input first character 0003 D9000E CHARI c2,d ;Input second character 0006 E1000E CHARO c2,d ;Output second character 0009 E1000D CHARO c1,d ;Output first character 000C 00 STOP 000D 00 c1: .BLOCK d#1 ;Storage for first char 000E 00 c2: .BLOCK d#1 ;Storage for second char 000F .END Symbol Value Symbol Value c1 c2 000D 000E Operand Comment A New Program Our Completed Program Page 217 Pseudo Code7-22 Object Addr code 0000 70000B 0003 0000 0005 Symbol Mnemon Operand BR Main sum: .WORD d#0 0000 num1: .BLOCK d#2 0007 0000 num2: .BLOCK d#2 0009 0000 num3: .BLOCK d#2 000B 090003 Main: LOADA sum,d 000E E90005 DECI num1,d 0011 190005 ADDA num1,d 0014 E90007 DECI num2,d 0017 190007 ADDA num2,d 001A E90009 DECI num3,d 001D 190009 ADDA num3,d 0020 110003 STOREA sum,d 0023 F10003 DECO 0026 00 STOP 0027 .END sum,d Symbol Main num2 Sum Value 000B 0007 0003 Symbol Value num1 0005 num3 0009 Status Bits Status bits allow a program to make a choice. BRLT Set the PC to the operand, if N is 1 (A register is less than zero) BREQ Set the PC to operand, if Z is 1 (A register is equal to zero)