Föreläsning 6 Maskinära programmering Högnivåspråk och översättning Processenhetens struktur Zebo Peng, IDA, LiTH 1 Machine Codes Machine codes [maskinkod] are instructions a computer can execute directly. Different computer types have different machine codes. All information in machines codes is given in binary format. 0 Ex. Machine code 1 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 MOVE R2 Address of X Zebo Peng, IDA, LiTH 2 1
Assembly Languages An assembly language uses symbolic names to represent operation codes, operand addresses, and instruction addresses: 0 Each instruction of the machine instruction set is represented by an operation identifier. 0 Reference to an operand is expressed by a symbolic address. 0 The next instruction address when a jump occurs is also expressed by a symbolic address. Assembly code MOVE R2 X Different computer types have different assembly codes. Zebo Peng, IDA, LiTH 3 Assembling Before an assembly program can be executed, it must be translated into machine codes. This translation is done by a program called an assembler [assemblator]. Assembly program Assembler Machine codes Zebo Peng, IDA, LiTH 4 2
The Assembling Tasks Translate the operation identifiers into their corresponding binary codes. Assign the symbolic addresses physical memory addresses (or logical addresses, relative addresses). Perform macro expansion. 0 A macro is a sequence of instructions which is defined once and used at any place where the sequences should execute. 0 Macro expansion places the machine code sequences at each place the macro is used. Handle subroutine calls. Zebo Peng, IDA, LiTH 5 Macro definition: DIV X Y : Macro vs. Subroutine Subroutine definition: DIV X Y : Macro usage: Subroutine usage: DIV 120 60; DIV 121 60; DIV 120 60; DIV 121 60; Machine codes: Machine codes: Sub1 120 60; Sub1 120 60; Zebo Peng, IDA, LiTH 6 3
High-Level Languages Application programs are typically written in a highlevel language which has instructions much more powerful than assembly instructions. A high-level language is not related to the architecture of any computer, while machine and assembly languages are machine dependent. It takes shorter time to program in a high-level language, but it generates also less efficient codes. The translation of a high-level language program to its machine codes can be done by a compiler or an interpreter. Zebo Peng, IDA, LiTH 7 High-Level Language Examples C++ C Fortran Java Pascal Ada Prolog Lisp Simula Smalltalk Basic Cobol Zebo Peng, IDA, LiTH 8 4
Compilation [ [Kompilering] A program is translated into machine codes before execution starts. HL language program Compiler Machine codes Input data Execution Output data Zebo Peng, IDA, LiTH 9 Objectives of a Compiler It should produce machine codes which are as efficient as possible during execution. The compiler itself should be efficient and take little memory space. It should provide the user with as accurate diagnosis information as possible. Zebo Peng, IDA, LiTH 10 5
Different Types of Compilers An optimizing compiler [optimerande kompilator] puts in very much time to generate as efficient machine codes as possible. 0 Used usually when the development and test of a program is finished, and the final machine codes are to be generated. A test compiler [testkompilatorn] compiles quickly and generates codes which are not efficient. 0 Used when a program is tested. A course compiler [kurskompilator] puts in time to find all possible bugs and generate meaningful debug information. 0 Used in education. Zebo Peng, IDA, LiTH 11 Interpretation [ [Interpretering] An interpreted program is not translated into machine codes. Rather the instructions of the program are executed by the interpreter. The interpreter reads the program instructions as date. For each instruction in the program, a sequence of instruction of the interpreter is executed. HL language program Input data Interpreter Output data Zebo Peng, IDA, LiTH 12 6
System vs. Application Program System programs are programs which make it easier to run application programs: 0 Operative systems 0 Assemblers 0 Compilers and interpreters 0 Linkers 0 Loaders Application programs are the programs the end-users execute to accomplish certain tasks. Zebo Peng, IDA, LiTH 13 Summary A computer program can be written with different programming languages. Only programs in machine codes can be directly executed. Other programs must be first translated into machine codes before execution. Generally speaking, a translator take a program written in a source language [källspråk] and translate it into another program in a target language [målspråk] or object language [objektspråk]. Zebo Peng, IDA, LiTH 14 7
CPU The main function of the CPU is to execute instructions stored in the main memory. The major components of the CPU: 0 ALU: perform the computer s data processing functions. 0 Control unit: control the operation of the CPU and hence the computer. 0 A set of registers: provide storage internal to the CPU. 0 Internal CPU bus: provide data movement paths among the control unit, ALU, and registers. Zebo Peng, IDA, LiTH 15 CPU Internal Structure Zebo Peng, IDA, LiTH 16 8
Registers CPU must have some working space (temporary storage). These storage units are called registers. They are the top level component in the memory hierarchy. Number and function of the registers vary between different computers. Registration organization is one of the major design decisions. Zebo Peng, IDA, LiTH 17 Register Organization The registers serve two main functions: 0 User-Visible Registers: used by machine or assembly language programmers to minimize memory access. General-purpose registers Data registers Address registers Condition code registers 0 Control and Status Registers: used by the control unit to control the operation of the CPU, and by the operating system to control the execution of programs. Zebo Peng, IDA, LiTH 18 9
Control and Status Registers Program Counter (PC) Instruction Register (IR) Memory Address Register (MAR) Memory Buffer (Transfer) Register (MBR) Program status word (PSW) used to store the status of current instruction execution, such as 0 Zero set when the computation result is zero; 0 Overflow indicating arithmetic overflow; 0 Interrupt control to enable or disable interrupts; 0 Supervisor indicating if the execution is in supervisor mode or user mode. Zebo Peng, IDA, LiTH 19 Register Organization Examples Zebo Peng, IDA, LiTH 20 10
Summary The structure and function of the CPU are the most important architectural features of a computer system. It is essential to reduce the need to access the main memory in order to increase CPU performance, which can be achieved by using registers and a cache. Zebo Peng, IDA, LiTH 21 11