Computer organization Assembly Language Programming Rabie A Ramadan
Computer organization & Assembly Language Programming Rabie A. Ramadan http: //www. rabieramadan. org/ Ra. ramadan@uoh. edu. sa http: //www. rabieramadan. org/class es/2014 -2015/ICS 232/
Class Style n Do not think of the exam n n Feel free to stop me at any time n n Just think of the class materials and how much you learn from it I do not care how much I teach in class as long as you understand what I am saying There will be an interactive sessions in class n you solve some of the problems with my help Chapter 2 — Instructions: Language of the Computer — 2
When the time is up , just let me know…. Chapter 2 — Instructions: Language of the Computer — 3
Textbook S. Dandamudi, “Introduction to Assembly Language Programming, ” Second Edition, Springer, 2005.
Grading
Course Objectives n n n To introduce basic concepts of computer organization. To illustrate the computer organization concepts by Assembly Language programming. To teach Assembly language of most recent processor such as Intel Pentium processor.
n n n Outline A user’s view of computer systems n Why program in assembly language? What is assembly language? n n Relationship to machine language Advantages of high-level languages n n n Faster program development Easier maintenance Portability n n n Time-efficiency Space-efficiency Accessibility to hardware Typical applications Why learn assembly language? Performance: C versus assembly language n Multiplication example Chapter 1: Page 7
What is a Machine ? Process Directives Raw Material Final Product Machine In General: Device that processes a series of raw materials into the desired product following a well-defined process
Types of Machines Device Raw Material Products Food Processing Factory Natural Fruits, Vegetables, Meats, Dairy and Spices Manufactured (Packaged) Food Products Airport Planes, Travelers, Goods Application Files Decisions Data Administrative Office Computer
Computer Machine Model Machine = Computer Process Directives = Program = Suite of Instructions Instruction = Simple Operation
Basic Computer Organization (1) n n n Need a Unit to Execute Instructions Need a Unit to Contain Programs Need a Unit to Contain Input Data Need a Unit to Contain Intermediate Stage Data Need a Unit to Input Data Need a Unit to Output Data Central Processing Unit (CPU) Memory IO
Basic Computer Organization (3) Memory Cpu I/O Buses: Groups of Electrical Signals or Wires That Establish The Communication Between The Different Computer System Components
Memory Similar to a set of Storage Bins Each Bin is Called: A Memory Location Each Location has a Content And an Index The Content is The Data and the Index is The Address Data is Written in Memory And Read from The MEmory The CPU Reads Data From Memory And Writes Data Into Memory 0 Data 1 1 Data 2 2
Basic Computer Organization (3) Memory Cpu Address Bus Data Bus Control Bus Address Bus: Data Bus: Control Bus: I/O Specifies the Address of the Data being Accessed Carries the Data to be Transferred Specifies the Nature of the Transfer: (Memory Read/Write or I/O)
Memory n n n Storage Device 2 n Stores Programs and Data coded in binary n format. Technically “similar” Address to a two-dimensional array of “switches” A “switch” called a bit (abbr. for binary digit) n Address lines means 2 n words of m 1 bits each 0 m Data
Memory n 2 Operations: n n COE 205 Read: Copy Data stored in word of Address (on Address lines) to Data Bus Write: Store Data on Data Bus into word of Address (on Address lines) n Addr Memory Read Write m Data
Concept of Address n n It is an index in the memory It represents a “geographic” location of a word in the memory Number of Address lines and Word size determine Memory Capacity (Size) Most of the time: n COE 205 Memory size = 2 n words = 2 n * m bits
RAM n n n RAM: Random Access Memory Although the name is about the way memory is accessed. Historically, volatile memory has been called RAM. Volatile (do not retain information on power off) Used mainly as Central Memory for CPUs Two types of RAM n n COE 205 Static: Continuous Retention of Information Dynamic (DRAM): needs refresh cycle to maintain information
ROM n n n Non Volatile Used to store data (programs) that do not change often (fixed) Many types n n n Mask ROM: Values set at fabrication stage. Values cannot be changed Fuse PROM: Values set at burning phase. Values cannot be changed EPROM: Can be erased (UV) EEPROM: Electrically erased Flash EEPROM: Easily reprogrammable. New: NVRAM (Non Volatile RAM): Fast access time.
Disk Drives n n Hard Drive: suite of magnetic disks. Mechanically read and write data by moving a set of magnetic head over the disks CD-ROM, DVD-ROM: Suite of optical disks read by measuring the time of laser reflexion between “ 1” and “ 0” N S “ 1” “ 0”
Program Execution n n A Program is a suite of instructions Program Execution is Sequential Program is stored in Memory Program is executed by CPU COE 205 Instruction 1 Instruction 2 Instruction 3 Instruction n
CPU n n n Executes Programs Stored in Memory Executes Instructions ONE by ONE Only “knows” instructions: Instruction Set DO NOT know any notion of Program as a single entity. Everything is a suite of instructions COE 205
CPU Structure (1) Is Mainly a Data Processing Unit Controlled by a Control Unit. n Data Processing Unit: Datapath n n Registers (Scratch pad working space or temporary data storage) ALU: Arithmetic and Logic Unit Internal Buses Control Unit: Generates Commands to “drive” Datapath operations COE 205
CPU Structure (2) Data Path is Similar to a Pipe Structure where valves are controlled by the Control Unit COE 205 Register ALU Datapath Control Unit
Master Clock n n Instructions Executed step by step Need a “Rhythm” Generator to move forward in the steps: Clock Cycle Clock Frequency = 1/Clock Cycle Period : MHz Every CPU needs a Clock to control the transition from one execution step to the next COE 205 Time
Instruction Set n n n Instruction Set is the Catalog of the CPU Defines what are ALL the possible operations that the CPU can execute Only Instructions are recognized by CPU does NOT “understand” High Level Language (text). CPU understands instructions coded in numbers called machine code. COE 205
Instruction parameters n Each Instruction specifies an action or a suite of actions: n n n Action(s) “identifier” or Operation Code or Opcode Action arguments or operands Methods specifying how to access the operands, called addressing modes Instruction specified as: <Opcode> <Operand 1, addr_mode 1> <Operand 2, addr_mode 2> …. COE 205
Number of Operands n n Many types of Instruction Sets Instruction Set with One Operand: Implicit Register Called Accumulator. Everything goes to and from the accumulator: Instruction Set with Two Operands: Many registers can be used as accumulators Instruction Set with Three Operands: Mainly Register Based. COE 205
Fetch – Decode - Execute Cpu Fetch Address of next Instruction Read Command Opcode Decode Execute COE 205 Instruction Decoder Execute Reg Memory Fetch Immediate Decode
Address of Programs n Where the Address of next instruction is Stored ? Need for an Instruction Pointer Called: “Program Counter” PC • Critical Component of CPU • Conveniently useful for changing program sequence (Branch instructions)
Instruction Register n Where is the current instruction going to be stored during its execution ? Need for a Register Data Called: “Instruction Register” • Critical Component of CPU • Internal Register. Cannot be used (accessed) by instructions • Holds the current instruction until its execution is completed • Tightly Coupled to the decoding portion of the control unit • Connected to the datapath (to transfer operand fields) Bus Opcode Op 1 Instruction Decoder Op 2
Program in Memory n Binary code (machine code). Memory (8 -bit) B 8 00 MOV AX, 5 05 03 C 3 EB E 7 ADD AX, BX JMP Next
High Level Languages n n n n Machine independent. Cannot be run directly on the target machine Need to be translated to machine language Compiler: program that translates a HLL program to a machine language program of a specific platform The Machine language program produced by the compiler is the executable program. Translating HLL programs to machine language programs is not a one-toone mapping n A HLL statement translated to one or more machine language instructions n Usually, machine language programs produced by compilers are not efficient Deals with Data types (integer, real, complex, user-defined) vs. machine language: no data types only binary words. COE 205
Assembly Language n Text version of machine language n Human friendly representation of machine language n Based on mnemonics (easy to memorize abbreviations of actions) instead of dealing with opcode numbers. n Complicated format simplified with some conventions n Text file translated into machine code by the Assembler COE 205
Assembler n n n Program that assemble the programs written in assembly language into machine language Because there is a ONE to ONE mapping between instructions written in assembly language and machine language instructions, the process is called: assembly rather than translation. Disassembly (reverse process) is also easy because of the ONE to ONE relation between the assembly language instructions and the machine language instructions COE 205 MOV DX, 1 BA 0001 MOV AX, BX 8 B C 3 MOV AX, CX 8 B C 1 MOV AX, DX 8 B C 2 ADD AX, 1 83 C 0 01 ADD AX, 2 83 C 0 02 ADD AX, BX 03 C 3 ADD AX, CX 03 C 1 ADD AX, i 03 06 0000 SUB AX, 1 83 E 8 01 SUB AX, BX 2 B C 3 ADD AX, 1234 h 05 1234
Linker n n Program used to link together separately assembled/compiled programs into a single executable code Allows the programmers to develop different parts of a large program separately, test them separately and ‘freeze’ them for future use. Allows the programmer to develop store portions of programs that have been intensively tested and used into a “program library” for anyone to re-use them. Produces modular programs and greatly enables the management of large programming projects
Debugger/Monitor These are tools that allow the assembly programmers to: n n n Display and alter the contents of memory and registers while running their code, Perform disassembly of their machine code (show the assembly language equivalent), Permit them to run their programs, stop (or halt) them, run them step-by-step or insert break points. n Break points: Positions in the program that if are encountered during run time, the program will be halted so the programmer can examine the memory and registers contents and determine what went wrong.
A User’s View of Computer Systems Chapter 1: Page 38
What Is Assembly Language? n Some example assembly language instructions: inc mov and add n result class_size, 45 mask 1, 128 marks, 10 MIPS Examples andi $t 2, $t 1, 15 addu $t 3, $t 1, $t 2 move $t 2, $t 1 Some points to note: n n Assembly language instructions are cryptic Mnemonics are used for operations n n inc for increment, mov for move (i. e. , copy) Assembly language instructions are low level n Cannot write instructions such as mov marks, value Chapter 1: Page 39
What Is Assembly Language? (Cont’d) n Some simple high-level language instructions can be expressed by a single assembly instruction Assembly Language C inc result++; mov class_size, 45 class_size = 45; and mask 1, 128 mask 1 &= 128; add marks, 10 marks += 10; Chapter 1: Page 40
What Is Assembly Language? (Cont’d) n Most high-level language instructions need more than one assembly instruction C size = value; sum += x + y + z; Assembly Language mov AX, value mov size, AX mov AX, sum add add mov AX, x AX, y AX, z sum, AX Chapter 1: Page 41
What Is Assembly Language? (Cont’d) n Readability of assembly language instructions is much better than the machine language instructions n Machine language instructions are a sequence of 1 s and 0 s Assembly Language Machine Language (in Hex) inc result FF 060 A 00 mov and add class_size, 45 mask, 128 marks, 10 C 7060 C 002 D 00 80260 E 0080 83060 F 000 A Chapter 1: Page 42
What Is Assembly Language? (Cont’d) n MIPS examples Assembly Language Machine (in Hex) nop 0000 move $t 2, $t 15 000 A 2021 andi addu $t 2, $t 1, 15 $t 3, $t 1, $t 2 312 A 000 F 012 A 5821 Chapter 1: Page 43
Why Program in Assembly Language? n Two main reasons: n Efficiency n n Accessibility to system hardware Space-efficiency n n Space-efficiency Time-efficiency Assembly code tends to be compact Time-efficiency n Assembly language programs tend to run faster n Only a well-written assembly language program runs faster n Easy to write an assembly program that runs slower than its high-level language equivalent Chapter 1: Page 44
Typical Applications n n Application that need one of the three advantages of the assembly language Time-efficiency n Time-convenience n n Good to have but not required for functional correctness n Graphics Time-critical n n Necessary to satisfy functionality Real-time applications n Aircraft navigational systems n Process control systems n Robot control software n Missile control software Chapter 1: Page 45
Performance: C versus Assembly Language C version AL version Last slide Chapter 1: Page 46
- Slides: 46