Assembly Language for Intel-Based Computers, 4 th Edition Kip R. Irvine Chapter 1: Basic Concepts Chapter Overview Welcome to Assembly Language Virtual Machine Concept Data Representation Boolean Operations 3 Printing this Slide Show Welcome to Assembly Language (cont) To print this slide show with black characters on a white background, do the following: Select Print... from the File menu. Select Handouts in the Print what drop-down box. Select 2 or 3 in the Slides per page drop-down box. Select the Pure black and white check box (optional) Click on OK to begin printing. Assembly language is the oldest programming language. Of all languages, it bears the closest resemblance to the native language of a computer. Direct access to a computer s hardware To understand a great deal about your computer s architecture and operating system 2 4 1
Some Good Questions to Ask What is an assembler? A program that converts source-code programs from assembly language into machine language MASM (Microsoft Assembler), TASM (Borland Turbo Assembler) Linker (a companion program of Assembler) combines individual files created by an assembler into a single executable program. Debugger provides a way for a programmer to trace the execution of a program and examine the contents of memory. Some Good Questions to Ask (Cont.) How do C++ and Java relate to AL? E.g., X=(Y+4) *3 How does assembly language (AL) relate to machine language? One-to-one relationship Is AL portable? A language whose source program can be compiled and run on a wide variety of computer systems is said to be portable. AL makes no attempt to be portable. It is tied to a specific processor family 5 7 Some Good Questions to Ask (Cont.) Some Good Questions to Ask (Cont.) What hardware/software do I need? A computer with an Intel386, Intel486 or one of the Pentium processors (IA-32 processor family) OS: Microsoft Windows, MS-DOS, LINUX running a DOS emulator Editor Assembler Linker (Microsoft 16-bit linker: LINK.EXE, 32-bit linker: LINK32.EXE), Debugger (16-bit MS-DOS programs: MASM CodeView, TASM Turbo Debugger. 32-bit Windows console programs: Microsoft Visual Studio msdev.exe) What types of programs will I create? 16-Bit Real-Address Mode: MS-DOS, DOS emulator 32-Bit Protected Mode: Microsoft Windows Why learn AL? Embedded system programs Programs to be highly optimized for both space and runtime speed To gain an overall understanding of the interaction between the hardware, OS and application programs Device driver: programs that translate general operating system commands into specific references to hardware details 6 8 2
Assembly Language Applications Virtual Machine Concept AL is used to optimize certain sections of application programs for speed and to access computer hardware Virtual Machines Specific Machine Levels Some representative types of applications: Business application for single platform Hardware device driver Business application for multiple platforms Embedded systems & computer games (see next panel) * HL = high-level 9 11 Comparing ASM to High-Level Languages Virtual Machines Virtual machine concept A most effective way to explain how a computer s hardware and software are related In terms of programming Language: Each computer has a native machine language (language L0) that runs directly on its hardware A more human-friendly language is usually constructed above machine language, called Language L1 Programs written in L1 can run two different ways: Interpretetation L0 program interprets and executes L1 instructions one by one Translation L1 program is completely translated into an L0 program, which then runs on the computer hardware 10 12 3
Virtual Machines (Cont.) In terms of a hypothetical computer VM1 can execute commands written in language L1. VM2 can execute commands written in language L2. The process can repeat until a virtual machine VMncan be designed that supports a powerful, easy-to-use language. The Java programming language is based on the virtual machine concept. A program written in the Java language is translated by a Java compiler into Java byte code. Java byte code: a low-level language that is quickly executed at run time by Java virtual machine (JVM). The JVM has been implemented on many different computer systems, making Java programs relatively systemindependent Digital Logic Level 0 CPU, constructed from digital logic gates System bus Memory 13 15 Specific Machine Levels Microarchitecture High-Level Language Assembly Language Operating System Instruction Set Architecture Level 5 Level 4 Level 3 Level 2 Level 1 Interprets conventional machine instructions (Level 2) Executed by digital hardware (Level 0) Microarchitecture Level 1 Digital Logic Level 0 14 16 4
Instruction Set Architecture Assembly Language Level 2 Also known as conventional machine language Executed by Level 1 program (microarchitecture, Level 1) Each machine-language instruction is executed by several microinstructions Level 4 Instruction mnemonics such as ADD, SUB and MOV that are easily translated to the instruction set architecture level (Level 2) Calls functions written at the operating system level (Level 3) Programs are translated into machine language (Level 2) 17 19 Operating System High-Level Language Level 3 Provides services to Level 4 programs Programs translated and run at the instruction set architecture level (Level 2) Level 5 Application-oriented languages C++, C#, Virtual Basic Programs compile into assembly language (Level 4) 18 20 5
Data Representation Binary Numbers Binary Numbers Translating between binary and decimal Binary Addition Integer Storage Sizes Hexadecimal Integers Translating between decimal and hexadecimal Hexadecimal subtraction Signed Integers Binary subtraction Character Storage Each digit (bit) is either 1 or 0 Each bit represents a power of 2: Every binary number is a sum of powers of 2 1 1 1 1 1 1 1 1 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 21 23 Binary Numbers Translating Binary to Decimal Digits are 1 and 0 1 = true 0 = false MSB most significant bit LSB least significant bit Bit numbering: MSB LSB 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 15 0 Weighted positional notation shows how to calculate the decimal value of each binary bit: dec = (D n-1 2 n-1 ) + (D n-2 2 n-2 ) +... + (D 1 2 1 ) + (D 0 2 0 ) D = binary digit binary 00001001 = decimal 9: (1 2 3 ) + (1 2 0 ) = 9 22 24 6
Translating Unsigned Decimal to Binary Integer Storage Sizes Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: Standard sizes: byte 8 word 16 doubleword 32 quadword 64 37 = 100101 Practice: What is the largest unsigned integer that may be stored in 20 bits? 25 27 Binary Addition Hexadecimal Integers Starting with the LSB, add each pair of digits, include the carry if present. carry: 0 0 0 0 0 1 0 0 1 (4) All values in memory are stored in binary. Because long binary numbers are hard to read, we use hexadecimal representation. + 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 26 28 7
Translating Binary to Hexadecimal Powers of 16 Each hexadecimal digit corresponds to 4 binary bits. Example: Translate the binary integer 000101101010011110010100 to hexadecimal: Used when calculating hexadecimal values up to 8 digits long: 29 31 Converting Hexadecimal to Decimal Converting Decimal to Hexadecimal Multiply each digit by its corresponding power of 16: dec= (D 3 16 3 ) + (D 2 16 2 ) + (D 1 16 1 ) + (D 0 16 0 ) Hex 1234 equals (1 16 3 ) + (2 16 2 ) + (3 16 1 ) + (4 16 0 ), or decimal 4,660. Hex 3BA4 equals (3 16 3 ) + (11 * 16 2 ) + (10 16 1 ) + (4 16 0 ), or decimal 15,268. decimal 422 = 1A6 hexadecimal 30 32 8
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. Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive 1 1 36 28 28 6A 42 45 58 4B 78 6D 80 B5 sign bit 1 1 1 1 0 1 1 0 Negative 21 / 16 = 1, rem 5 0 0 0 0 1 0 1 0 Positive Important skill: Programmers frequently add and subtract the addresses of variables and instructions. If the highest digit of a hexadecmal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D 33 35 Hexadecimal Subtraction Forming the Two's Complement When a borrow is required from the digit to the left, add 10h to the current digit's value: 10h + 5 = 15h Negative numbers are stored in two's complement notation Additive Inverse of any binary integer Steps: Complement (reverse) each bit Add 1 1 C6 75 A2 47 24 2E Practice: The address of var1 is 00400020. The address of the next variable after var1 is 0040006A. How many bytes are used by var1? Note that 00000001 + 11111111 = 00000000 34 36 9
Binary Subtraction When subtracting A B, convert B to its two's complement Add A to ( B) Ranges of Signed Integers The highest bit is reserved for the sign. This limits the range: 1 1 0 0 1 1 0 0 0 0 1 1 1 1 0 1 1 0 0 1 Practice: Subtract 0101 from 1001. Practice: What is the largest positive value that may be stored in 20 bits? 37 39 Learn How To Do the Following: Character Storage Form the two's complement of a hexadecimal integer Convert signed binary to decimal Convert signed decimal to binary Convert signed decimal to hexadecimal Convert signed hexadecimal to decimal Character sets Standard ASCII(0 127) Extended ASCII (0 255) ANSI (0 255) Unicode (0 65,535) Null-terminated String Array of characters followed by a null byte Using the ASCII table back inside cover of book 38 40 10
Numeric Data Representation Boolean Algebra pure binary Be a number stored in memory in its raw format Can be calculated directly Stored in multiples of 8 bits ASCII binary string of digits: "01010101" ASCII decimal string of digits: "65" ASCII hexadecimal string of digits: "9C" Format ACSII Binary ASCII decimal ASCII hexadecimal ASCII octal Value 0100000001 65 41 101 Based on symbolic logic, designed by George Boole Boolean expressions created from: NOT, AND, OR 41 43 Boolean Operations NOT NOT AND OR Operator Precedence Truth Tables Inverts (reverses) a boolean value Truth table for Boolean NOT operator: Digital gate diagram for NOT: NOT 42 44 11
AND Truth table for Boolean AND operator: Operator Precedence Examples showing the order of operations: Digital gate diagram for AND: AND 45 47 OR Truth Tables (1 of 3) Truth table for Boolean OR operator: A Boolean function has one or more Boolean inputs, and returns a single Boolean output. A truth tableshows all the inputs and outputs of a Boolean function Digital gate diagram for OR: OR Example: X Y 46 48 12
Example: X Y Truth Tables (2 of 3) 49 Truth Tables (3 of 3) Example: (Y S) (X S) S X Y mux Z Two-input multiplexer 50 13