Basic Concepts COE 205 Computer Organization and Assembly Language Computer Engineering Department King Fahd University of Petroleum and Minerals Overview Welcome to COE 205 Assembly-, Machine-, and High-Level Languages Assembly Language Programming Tools Programmer’s View of a Computer System Basic Computer Organization Data Representation Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 2 Welcome to COE 205 Assembly language programming Basics of computer organization CPU design Software Tools Microsoft Macro Assembler (MASM) version 6.15 Link Libraries provided by Author (Irvine32.lib and Irivine16.lib) Microsoft Windows debugger ConTEXT Editor Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 3 Textbook Kip Irvine: Assembly Language for Intel-Based Computers 4th edition (2003) 5th edition (2007) Read the textbook! Key for learning and obtaining a good grade Online material http://assembly.pc. ccse.kfupm.edu.sa / Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 4 Course Objectives After successfully completing the course, students will be able to: Describe the basic components of a computer system, its instruction set architecture and its basic fetch-execute cycle operation. Describe how data is represented in a computer and recognize when overflow occurs. Recognize the basics of assembly language programming including addressing modes. Analyze, design, implement, and test assembly language programs. Recognize, analyze, and design the basic components of a simple CPU including datapath and control unit design alternatives. Recognize various instruction formats. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 5 Course Learning Outcomes Ability to analyze, design, implement, and test assembly language programs. Ability to use tools and skills in analyzing and debugging assembly language programs. Ability to design the datapath and control unit of a simple CPU. Ability to demonstrate self-learning capability. Ability to work in a team. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 6 Required Background The student should already be able to program confidently in at least one high-level programming language, such as Java or C. Prerequisite COE 202: Fundamentals of computer engineering ICS 102: Introduction to computing Only students with computer engineering major should be registered in this course. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 7 Grading Policy Programming Assignments 15% Quizzes 10% Exam I 15% (Sat. Nov. 3, 2007) Exam II 20% (Sat. Dec. 29, 2007) Laboratory 20% Final 20% Attendance will be taken regularly. Excuses for officially authorized absences must be presented no later than one week following resumption of class attendance. Late assignments will be accepted but you will be penalized 10% per each late day. A student caught cheating in any of the assignments will get 0 out of 15%. No makeup will be made for missing Quizzes or Exams. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 8 Course Topics Introduction and Information Representation: 6 lectures Introduction to computer organization. Instruction Set Architecture. Computer Components. Fetch-Execute cycle. Signed number representation ranges. Overflow. Assembly Language Concepts: 7 lectures Assembly language format. Directives vs. instructions. Constants and variables. I/O. INT 21H. Addressing modes. 8086 Assembly Language Programming: 19 lectures Register set. Memory segmentation. MOV instructions. Arithmetic instructions and flags (ADD, ADC, SUB, SBB, INC, DEC, MUL, IMUL, DIV, IDIV). Compare, Jump and loop (CMP, JMP, Cond. jumps, LOOP). Logic, shift and rotate. Stack operations. Subprograms. Macros. I/O (IN, OUT). String instructions. Interrupts and interrupt processing, INT and IRET. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 9 Course Topics CPU Design: 12 lectures Register transfer. Data-path design. 1-bus, 2-bus and 3bus CPU organization. Fetch and execute phases of instruction processing. Performance consideration. Control steps. CPU-Memory interface circuit. Hardwired control unit design. Microprogramming. Horizontal and Vertical microprogramming. Microprogrammed control unit design. Instruction Set Formats: 1 lecture Fixed vs. variable instruction format. Examples of instruction formats. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 10 Next … Welcome to COE 205 Assembly-, Machine-, and High-Level Languages Assembly Language Programming Tools Programmer’s View of a Computer System Basic Computer Organization Data Representation Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 11 Some Important Questions to Ask What is Assembly Language? Why Learn Assembly Language? What is Machine Language? How is Assembly related to Machine Language? What is an Assembler? How is Assembly related to High-Level Language? Is Assembly Language portable? Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 12 A Hierarchy of Languages Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 13 Assembly and Machine Language Machine language Native to a processor: executed directly by hardware Instructions consist of binary code: 1s and 0s Assembly language Slightly higher-level language Readability of instructions is better than machine language One-to-one correspondence with machine language instructions Assemblers translate assembly to machine code Compilers translate high-level programs to machine code Either directly, or Indirectly via an assembler Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 14 Compiler and Assembler Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 15 Instructions and Machine Language Each command of a program is called an instruction (it instructs the computer what to do). Computers only deal with binary data, hence the instructions must be in binary format (0s and 1s) . The set of all instructions (in binary form) makes up the computer's machine language. This is also referred to as the instruction set. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 16 Instruction Fields Machine language instructions usually are made up of several fields. Each field specifies different information for the computer. The major two fields are: Opcode field which stands for operation code and it specifies the particular operation that is to be performed. Each operation has its unique opcode. Operands fields which specify where to get the source and destination operands for the operation specified by the opcode. The source/destination of operands can be a constant, the memory or one of the general-purpose registers. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 17 Assembly vs. Machine Code Instruction Address Machine Code Assembly Instruction Basic Concepts 0005 B8 0001 MOV AX, 1 0008 B8 0002 MOV AX, 2 000B B8 0003 MOV AX, 3 000E B8 0004 MOV AX, 4 0011 BB 0001 MOV BX, 1 0014 B9 0001 MOV CX, 1 0017 BA 0001 MOV DX, 1 001A 8B C3 MOV AX, BX 001C 8B C1 MOV AX, CX 001E 8B C2 MOV AX, DX 0020 83 C0 01 ADD AX, 1 0023 83 C0 02 ADD AX, 2 0026 03 C3 ADD AX, BX 0028 03 C1 ADD AX, CX 002A 03 06 0000 ADD AX, i 002E 83 E8 01 SUB AX, 1 0031 2B C3 SUB AX, BX 0033 05 1234 ADD AX, 1234h COE 205 – Computer Organization and Assembly Language – KFUPM slide 18 Translating Languages English: D is assigned the sum of A times B plus 10. High-Level Language: D = A * B + 10 A statement in a high-level language is translated typically into several machine-level instructions Intel Assembly Language: Intel Machine Language: mov eax, A A1 00404000 mul B F7 25 00404004 add eax, 10 83 C0 0A mov D, eax Basic Concepts A3 00404008 COE 205 – Computer Organization and Assembly Language – KFUPM slide 19 Advantages of High-Level Languages Program development is faster High-level statements: fewer instructions to code Program maintenance is easier For the same above reasons Programs are portable Contain few machine-dependent details Can be used with little or no modifications on different machines Compiler translates to the target machine language However, Assembly language programs are not portable Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 20 Why Learn Assembly Language? Two main reasons: Accessibility to system hardware Space and time efficiency Accessibility to system hardware Assembly Language is useful for implementing system software Also useful for small embedded system applications Space and Time efficiency Understanding sources of program inefficiency Tuning program performance Writing compact code Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 21 Assembly vs. High-Level Languages Some representative types of applications: Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 22 Next … Welcome to COE 205 Assembly-, Machine-, and High-Level Languages Assembly Language Programming Tools Programmer’s View of a Computer System Basic Computer Organization Data Representation Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 23 Assembler Software tools are needed for editing, assembling, linking, and debugging assembly language programs An assembler is a program that converts source-code programs written in assembly language into object files in machine language Popular assemblers have emerged over the years for the Intel family of processors. These include … TASM (Turbo Assembler from Borland) NASM (Netwide Assembler for both Windows and Linux), and GNU assembler distributed by the free software foundation You will use MASM (Macro Assembler from Microsoft) Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 24 Linker and Link Libraries You need a linker program to produce executable files It combines your program's object file created by the assembler with other object files and link libraries, and produces a single executable program LINK32.EXE is the linker program provided with the MASM distribution for linking 32-bit programs We will also use a link library for input and output Called Irvine32.lib developed by Kip Irvine Works in Win32 console mode under MS-Windows Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 25 Assemble and Link Process Source File Source File Source File Assembler Object File Assembler Object File Linker Assembler Object File Link Libraries Executable File A project may consist of multiple source files Assembler translates each source file separately into an object file Linker links all object files together with link libraries Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 26 Debugger Allows you to trace the execution of a program Allows you to view code, memory, registers, etc. You will use the 32-bit Windows debugger Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 27 Editor Allows you to create assembly language source files Some editors provide syntax highlighting features and can be customized as a programming environment Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 28 Next … Welcome to COE 205 Assembly-, Machine-, and High-Level Languages Assembly Language Programming Tools Programmer’s View of a Computer System Basic Computer Organization Data Representation Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 29 Programmer’s View of a Computer System Increased level of abstraction Application Programs High-Level Language Level 5 Assembly Language Level 4 Operating System Instruction Set Architecture Level 2 Microarchitecture Level 1 Digital Logic Basic Concepts Level 3 Level 0 COE 205 – Computer Organization and Assembly Language – KFUPM Each level hides the details of the level below it slide 30 Programmer's View – 2 Application Programs (Level 5) Written in high-level programming languages Such as Java, C++, Pascal, Visual Basic . . . Programs compile into assembly language level (Level 4) Assembly Language (Level 4) Instruction mnemonics are used Have one-to-one correspondence to machine language Calls functions written at the operating system level (Level 3) Programs are translated into machine language (Level 2) Operating System (Level 3) Provides services to level 4 and 5 programs Translated to run at the machine instruction level (Level 2) Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 31 Programmer's View – 3 Instruction Set Architecture (Level 2) Specifies how a processor functions Machine instructions, registers, and memory are exposed Machine language is executed by Level 1 (microarchitecture) Microarchitecture (Level 1) Controls the execution of machine instructions (Level 2) Implemented by digital logic (Level 0) Digital Logic (Level 0) Implements the microarchitecture Uses digital logic gates Logic gates are implemented using transistors Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 32 Instruction Set Architecture (ISA) Collection of assembly/machine instruction set of the machine, Machine resources that can be managed with these instructions Memory, Programmer-accessible registers. Provides a hardware/software interface Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 33 Next … Welcome to COE 205 Assembly-, Machine-, and High-Level Languages Assembly Language Programming Tools Programmer’s View of a Computer System Basic Computer Organization Data Representation Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 34 Basic Computer Organization Since the 1940's, computers have 3 classic components: Processor, called also the CPU (Central Processing Unit) Memory and Storage Devices I/O Devices Interconnected with one or more buses Bus consists of data bus Data Bus registers Address Bus Control Bus Processor (CPU) ALU CU Memory I/O Device #1 I/O Device #2 clock control bus address bus Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 35 Processor Processor consists of Datapath ALU Registers Control unit ALU Performs arithmetic and logic instructions Control unit (CU) Generates the control signals required to execute instructions Implementation varies from one processor to another Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 36 Clock Synchronizes Processor and Bus operations Clock cycle = Clock period = 1 / Clock rate Cycle 1 Cycle 2 Cycle 3 Clock rate = Clock frequency = Cycles per second 1 Hz = 1 cycle/sec 1 KHz = 103 cycles/sec 1 MHz = 106 cycles/sec 1 GHz = 109 cycles/sec 2 GHz clock has a cycle time = 1/(2×109) = 0.5 nanosecond (ns) Clock cycles measure the execution of instructions Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 37 Memory Ordered sequence of bytes The sequence number is called the memory address Byte addressable memory Each byte has a unique address Supported by almost all processors Physical address space Determined by the address bus width Pentium has a 32-bit address bus Physical address space = 4GB = 232 bytes Itanium with a 64-bit address bus can support Up to 264 bytes of physical address space Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 38 Address Space Address Space is the set of memory locations (bytes) that can be addressed Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 39 Address, Data, and Control Bus Address Bus Memory address is put on address bus If memory address = a bits then 2a locations are addressed Data Bus: bi-directional bus Data can be transferred in both directions on the data bus Control Bus Signals control transfer of data Read request Processor address bus Address Register Basic Concepts a bits data bus Data Register Write request Done transfer Memory d bits 0 1 2 3 read Bus Control ... write done COE 205 – Computer Organization and Assembly Language – KFUPM 2a – 1 slide 40 Memory Read and Write Cycles Read cycle 1. Processor places address on the address bus 2. Processor asserts the memory read control signal 3. Processor waits for memory to place the data on the data bus 4. Processor reads the data from the data bus 5. Processor drops the memory read signal Write cycle 1. Processor places address on the address bus 2. Processor places the data on the data bus 3. Processor asserts the memory write control signal 4. Wait for memory to store the data (wait states for slow memory) 5. Processor drops the memory write signal Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 41 Reading from Memory Multiple clock cycles are required Memory responds much more slowly than the CPU Address is placed on address bus Read Line (RD) goes low, indicating that processor wants to read CPU waits (one or more cycles) for memory to respond Read Line (RD) goes high, indicating that data is on the data bus Cycle 1 Cycle 2 Cycle 3 Cycle 4 CLK Address ADDR RD Data DATA Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 42 Memory Devices Volatile Memory Devices Data is lost when device is powered off RAM = Random Access Memory DRAM = Dynamic RAM 1-Transistor cell + trench capacitor Dense but slow, must be refreshed Typical choice for main memory SRAM: Static RAM 6-Transistor cell, faster but less dense than DRAM Typical choice for cache memory Non-Volatile Memory Devices Basic Concepts Stores information permanently ROM = Read Only Memory Used to store the information required to startup the computer Many types: ROM, EPROM, EEPROM, and FLASH FLASH memory can be erased electrically in blocks COE 205 – Computer Organization and Assembly Language – KFUPM slide 43 Processor-Memory Performance Gap CPU: 55% per year Performance 1000 “Moore’s Law” 100 Processor-Memory Performance Gap: (grows 50% per year) 10 DRAM: 7% per year 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 1 1980 – No cache in microprocessor 1995 – Two-level cache on microprocessor Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 44 The Need for a Memory Hierarchy Widening speed gap between CPU and main memory Processor operation takes less than 1 ns Main memory requires more than 50 ns to access Each instruction involves at least one memory access One memory access to fetch the instruction A second memory access for load and store instructions Memory bandwidth limits the instruction execution rate Cache memory can help bridge the CPU-memory gap Cache memory is small in size but fast Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 45 Typical Memory Hierarchy Registers are at the top of the hierarchy Typical size < 1 KB Microprocessor Access time: 0.5 – 1 ns Registers L2 Cache (512KB – 8MB) L1 Cache Access time: 2 – 10 ns L2 Cache Main Memory (1 – 2 GB) Access time: 50 – 70 ns Faster Level 1 Cache (8 – 64 KB) Memory Bus Memory Disk Storage (> 200 GB) Access time: milliseconds Basic Concepts Bigger Access time < 0.5 ns I/O Bus Disk, Tape, etc COE 205 – Computer Organization and Assembly Language – KFUPM slide 46 Magnetic Disk Storage Disk Access Time = Seek Time + Rotation Latency + Transfer Time Read/write head Sector Actuator Recording area Seek Time: head movement to the desired track (milliseconds) Rotation Latency: disk rotation until desired sector arrives under the head Transfer Time: to transfer data Basic Concepts Track 2 Track 1 Track 0 Arm Direction of rotation Platter Spindle COE 205 – Computer Organization and Assembly Language – KFUPM slide 47 Example on Disk Access Time Given a magnetic disk with the following properties Rotation speed = 7200 RPM (rotations per minute) Average seek = 8 ms, Sector = 512 bytes, Track = 200 sectors Calculate Time of one rotation (in milliseconds) Average time to access a block of 32 consecutive sectors Answer Rotations per second = 7200/60 = 120 RPS Rotation time in milliseconds = 1000/120 = 8.33 ms Average rotational latency = time of half rotation = 4.17 ms Time to transfer 32 sectors = (32/200) * 8.33 = 1.33 ms Average access time = 8 + 4.17 + 1.33 = 13.5 ms Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 48 Input Devices Key Cap Spring Mechanical switch c d e f 8 9 a b 4 5 6 7 0 1 2 3 Logical arrangement of keys Basic Concepts Conductor-coated membrane Contacts Membrane switch COE 205 – Computer Organization and Assembly Language – KFUPM slide 49 Output Devices Cleaning of excess toner Charging Fusing of toner Rotating drum Heater Light from optical system Rollers Toner Sheet of paper Laser printing Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 50 I/O Controllers I/O devices are interfaced via an I/O controller I/O controller uses the system bus to communicate with processor I/O controller takes care of low-level operation details Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 51 Next … Welcome to COE 205 Assembly-, Machine-, and High-Level Languages Assembly Language Programming Tools Programmer’s View of a Computer System Basic Computer Organization Data Representation Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 52 Data Representation Binary Numbers Hexadecimal Numbers Base Conversions Integer Storage Sizes Binary and Hexadecimal Addition Signed Integers and 2's Complement Notation Binary and Hexadecimal subtraction Carry and Overflow Character Storage Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 53 Binary Numbers Digits are 1 and 0 1 = true 0 = false MSB – most significant bit LSB – least significant bit Bit numbering: MSB LSB 1011001010011100 15 Basic Concepts 0 COE 205 – Computer Organization and Assembly Language – KFUPM slide 54 Binary Numbers Each digit (bit) is either 1 or 0 1 1 1 1 1 1 1 1 Each bit represents a power of 2: 27 26 25 24 23 22 21 20 Every binary number is a sum of powers of 2 Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 55 Converting Binary to Decimal Weighted positional notation shows how to calculate the decimal value of each binary bit: Decimal = (dn-1 2n-1) + (dn-2 2n-2) + ... + (d1 21) + (d0 20) d = binary digit binary 00001001 = decimal 9: (1 23) + (1 20) = 9 Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 56 Convert Unsigned Decimal to Binary Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: least significant bit most significant bit 37 = 100101 Basic Concepts stop when quotient is zero COE 205 – Computer Organization and Assembly Language – KFUPM slide 57 Hexadecimal Integers Binary values are represented in hexadecimal. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 58 Converting Binary to Hexadecimal • Each hexadecimal digit corresponds to 4 binary bits. • Example: Translate the binary integer 000101101010011110010100 to hexadecimal: Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 59 Converting Hexadecimal to Decimal Multiply each digit by its corresponding power of 16: Decimal = (d3 163) + (d2 162) + (d1 161) + (d0 160) d = hexadecimal digit Examples: Hex 1234 = (1 163) + (2 162) + (3 161) + (4 160) = Decimal 4,660 Hex 3BA4 = (3 163) + (11 * 162) + (10 161) + (4 160) = Decimal 15,268 Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 60 Converting Decimal to Hexadecimal Repeatedly divide the decimal integer by 16. Each remainder is a hex digit in the translated value: least significant digit most significant digit stop when quotient is zero Decimal 422 = 1A6 hexadecimal Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 61 Integer Storage Sizes byte Standard sizes: word doubleword quadword 8 16 32 64 What is the largest unsigned integer that may be stored in 20 bits? Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 62 Binary Addition Start with the least significant bit (rightmost bit) Add each pair of bits Include the carry in the addition, if present carry: 0 0 0 0 0 1 0 0 (4) 0 0 0 0 0 1 1 1 (7) 0 0 0 0 1 0 1 1 (11) bit position: 7 6 5 4 3 2 1 0 + Basic Concepts 1 COE 205 – Computer Organization and Assembly Language – KFUPM slide 63 Hexadecimal Addition Divide the sum of two digits by the number base (16). The quotient becomes the carry value, and the remainder is the sum digit. 36 42 78 28 45 6D 1 1 28 58 80 6A 4B B5 21 / 16 = 1, remainder 5 Important skill: Programmers frequently add and subtract the addresses of variables and instructions. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 64 Signed Integers Several ways to represent a signed number Sign-Magnitude 1's complement 2's complement Divide the range of values into 2 equal parts First part corresponds to the positive numbers (≥ 0) Second part correspond to the negative numbers (< 0) Focus will be on the 2's complement representation Has many advantages over other representations Used widely in processors to represent signed integers Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 65 Two's Complement Representation Positive numbers Signed value = Unsigned value Negative numbers Signed value = 2n – Unsigned value n = number of bits Negative weight for MSB Another way to obtain the signed value is to assign a negative weight to most-significant bit 1 0 -128 64 1 1 0 1 0 0 32 16 8 4 2 1 = -128 + 32 + 16 + 4 = -76 Basic Concepts 8-bit Binary Unsigned value value Signed value 00000000 0 0 00000001 1 +1 00000010 2 +2 ... ... ... 01111110 126 +126 01111111 127 +127 10000000 128 -128 10000001 129 -127 ... ... ... 11111110 254 -2 11111111 255 -1 COE 205 – Computer Organization and Assembly Language – KFUPM slide 66 Forming the Two's Complement starting value 00100100 = +36 step1: reverse the bits (1's complement) 11011011 step 2: add 1 to the value from step 1 + sum = 2's complement representation 11011100 = -36 1 Sum of an integer and its 2's complement must be zero: 00100100 + 11011100 = 00000000 (8-bit sum) Ignore Carry The easiest way to obtain the 2's complement of a binary number is by starting at the LSB, leaving all the 0s unchanged, look for the first occurrence of a 1. Leave this 1 unchanged and complement all the bits after it. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 67 Sign Bit Highest bit indicates the sign. 1 = negative, 0 = positive sign bit 1 1 1 1 0 1 1 0 0 0 0 0 1 0 1 0 Negative Positive If highest digit of a hexadecimal is > 7, the value is negative Examples: 8A and C5 are negative bytes A21F and 9D03 are negative words B1C42A00 is a negative double-word Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 68 Sign Extension Step 1: Move the number into the lower-significant bits Step 2: Fill all the remaining higher bits with the sign bit This will ensure that both magnitude and sign are correct Examples Sign-Extend 10110011 to 16 bits 10110011 = -77 11111111 10110011 = -77 Sign-Extend 01100010 to 16 bits 01100010 = +98 00000000 01100010 = +98 Infinite 0s can be added to the left of a positive number Infinite 1s can be added to the left of a negative number Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 69 Two's Complement of a Hexadecimal To form the two's complement of a hexadecimal Subtract each hexadecimal digit from 15 Add 1 Examples: 2's complement of 6A3D = 95C2 + 1 = 95C3 2's complement of 92F0 = 6D0F + 1 = 6D10 2's complement of FFFF = 0000 + 1 = 0001 No need to convert hexadecimal to binary Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 70 Binary Subtraction When subtracting A – B, convert B to its 2's complement Add A to (–B) – 00001100 00000010 00001010 00001100 + 11111110 (2's complement) 00001010 (same result) Carry is ignored, because Negative number is sign-extended with 1's You can imagine infinite 1's to the left of a negative number Adding the carry to the extended 1's produces extended zeros Practice: Subtract 00100101 from 01101001. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 71 Hexadecimal Subtraction When a borrow is required from the digit to the left, add 16 (decimal) to the current digit's value 16 + 5 = 21 -1 - 11 C675 A247 242E + C675 5DB9 242E (2's complement) (same result) Last Carry is ignored Practice: The address of var1 is 00400B20. The address of the next variable after var1 is 0040A06C. How many bytes are used by var1? Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 72 Ranges of Signed Integers The unsigned range is divided into two signed ranges for positive and negative numbers Practice: What is the range of signed values that may be stored in 20 bits? Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 73 Carry and Overflow Carry is important when … Adding or subtracting unsigned integers Indicates that the unsigned sum is out of range Either < 0 or >maximum unsigned n-bit value Overflow is important when … Adding or subtracting signed integers Indicates that the signed sum is out of range Overflow occurs when Adding two positive numbers and the sum is negative Adding two negative numbers and the sum is positive Can happen because of the fixed number of sum bits Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 74 Carry and Overflow Examples We can have carry without overflow and vice-versa Four cases are possible 1 0 0 0 0 1 1 1 1 1 15 + 1 1 1 1 0 0 0 0 1 1 1 1 15 + 0 0 0 0 1 0 0 0 8 1 1 1 1 1 0 0 0 245 (-8) 0 0 0 1 0 1 1 1 23 0 0 0 0 0 1 1 1 7 Carry = 0 Overflow = 0 Carry = 1 1 1 0 1 0 0 1 1 1 1 79 + Overflow = 0 1 1 1 1 0 1 1 0 1 0 218 (-38) + 0 1 0 0 0 0 0 0 64 1 0 0 1 1 1 0 1 157 (-99) 1 0 0 0 1 1 1 1 143 (-113) 0 1 1 1 0 1 1 1 Carry = 0 Basic Concepts Overflow = 1 Carry = 1 119 Overflow = 1 COE 205 – Computer Organization and Assembly Language – KFUPM slide 75 Character Storage Character sets Standard ASCII: 7-bit character codes (0 – 127) Extended ASCII: 8-bit character codes (0 – 255) Unicode: 16-bit character codes (0 – 65,535) Unicode standard represents a universal character set Defines codes for characters used in all major languages Used in Windows-XP: each character is encoded as 16 bits UTF-8: variable-length encoding used in HTML Encodes all Unicode characters Uses 1 byte for ASCII, but multiple bytes for other characters Null-terminated String Array of characters followed by a NULL character Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 76 Printable ASCII Codes 0 1 2 3 ! " # 4 5 6 7 8 9 A B C D E F $ % & ' ( ) * + , - . / 3 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 4 @ A B C D E F G H I J K L M N O 5 P Q R S T U V W X Y Z [ \ ] ^ _ 6 ` a b c d e f g h i j k l m n o 7 p q r s t u v w x y z { | } ~ 2 space DEL Examples: ASCII code for space character = 20 (hex) = 32 (decimal) ASCII code for 'L' = 4C (hex) = 76 (decimal) ASCII code for 'a' = 61 (hex) = 97 (decimal) Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 77 Control Characters The first 32 characters of ASCII table are used for control Control character codes = 00 to 1F (hex) Not shown in previous slide Examples of Control Characters Character 0 is the NULL character used to terminate a string Character 9 is the Horizontal Tab (HT) character Character 0A (hex) = 10 (decimal) is the Line Feed (LF) Character 0D (hex) = 13 (decimal) is the Carriage Return (CR) The LF and CR characters are used together They advance the cursor to the beginning of next line One control character appears at end of ASCII table Character 7F (hex) is the Delete (DEL) character Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 78 Terminology for Data Representation Binary Integer Integer stored in memory in its binary format Ready to be used in binary calculations ASCII Digit String A string of ASCII digits, such as "123" ASCII binary String of binary digits: "01010101" ASCII decimal String of decimal digits: "6517" ASCII hexadecimal String of hexadecimal digits: "9C7B" Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 79 Summary Assembly language helps you learn how software is constructed at the lowest levels Assembly language has a one-to-one relationship with machine language An assembler is a program that converts assembly language programs into machine language A linker combines individual files created by an assembler into a single executable file A debugger provides a way for a programmer to trace the execution of a program and examine the contents of memory and registers A computer system can be viewed as consisting of layers. Programs at one layer are translated or interpreted by the next lower-level layer Binary and Hexadecimal numbers are essential for programmers working at the machine level. Basic Concepts COE 205 – Computer Organization and Assembly Language – KFUPM slide 80