Assembly Introduction Assembly Content Course Description Basic Concepts of Assembly Language Welcome to Assembly Language Virtual Machine Concept Data Representation Boolean Operations Course Description Prerequisites: Structured Programming Language Textbook References: Assembly Language for Intel-Based Computers. Assembly language for x86 processors The Art of Assembly Language Resources: http://web.sau.edu/LillisKevinM/csci240/masmdocs/ http://kipirvine.com/asm/4th/asmsources/ http://forum.codecall.net/topic/62064-assembly-language-resources/ Course Description Grading : • Final Exam • Year work • Oral • Laboratory and Practice • Sum 65 10 10 15 100 Timing: • Lecture • Practice • Exam 3 2 3 Content Course Description Basic Concepts of Assembly Language Welcome to Assembly Language Virtual Machine Concept Data Representation Boolean Operations Basic Concepts Welcome to Assembly Language Some Good Questions to Ask Virtual Machine Concept Data Representation Boolean Operations Welcome to Assembly Language Some Good 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? What is Assembly Language? A low-level processor-specific programming language design to match the processor’s machine instruction set. Each assembly language instruction matches exactly one machine language instruction. We will focus Instructions. on Intel based Assembly It covers many different versions of CPUs that followed, from Intel; the 80188, 80186, 80286, 80386, 80486, Pentium, Pentium Pro, and so on. It describes the basics of 32-bit assembly language programming. What is Assembly Language? A Hierarchy of Languages Why Learn Assembly Language? To learn how high-level language code gets translated into machine language. To learn the computer’s hardware by direct access to memory, video controller, sound card, keyboard… To speed up applications by direct access to hardware. What is Machine Language ML? Machine languages are lowest-level programming language and are the only languages understood by computers without translation. While easily understood by computers, machine languages are almost impossible for humans because they consist entirely of binary digits. Every CPU has its own specific machine language. What is Machine Language ML? Each ML instruction contains an op code (operation code) and zero or more operands. Examples: Opcode Operand Meaning ------------------------------------------40 05 0005 increment the AX register add 0005 to AX The accumulator. General-purpose register. How is Assembly related to 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 What is an Assembler? An assembler is a type of computer program that interprets software programs written in assembly language into machine language, code and instructions that can be executed by a computer. For Example, MASM (Macro Assembler from Microsoft) How is Assembly related to HighLevel Language? Basic Concepts Welcome to Assembly Language Some Good Questions to Ask Assembly Language Applications Virtual Machine Concept Data Representation Boolean Operations Virtual Machine Concept A virtual machine (VM) is a software program or operating system that • exhibits the behavior of a separate computer. • is capable of performing tasks such as running applications and programs in a separate computer. VM (virtual machine) is a layer of abstraction that gives a program one simplified interface for interacting with a variety of physical computers and their operating systems. Translating languages English: Display the sum of A times B plus C. C++: cout << (A * B + C); Assembly Language: mov eax,A mul B add eax,C call WriteInt Intel Machine Language: A1 F7 03 E8 00000000 25 00000004 05 00000008 00500000 Virtual machines Abstractions for computers High-Level Language Level 5 Assembly Language Level 4 Operating System Level 3 Instruction Set Architecture Level 2 Microarchitecture Level 1 Digital Logic Level 0 High-level language Level 5 • Application-oriented languages • Programs are compiled into assembly language (Level 4) cout << (A * B + C); Assembly language Level 4 • Instruction mnemonics that have a 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) mov eax, A mul B add eax, C call WriteInt Operating system Level 3 • Provides services • Programs translated and run at the instruction set architecture level (Level 2) High-Level Language Level 5 Assembly Language Level 4 Operating System Level 3 Instruction Set Architecture Level 2 Microarchitecture Level 1 Digital Logic Level 0 Instruction set architecture Level 2 • Also known as conventional machine language • Executed by Level 1 program (microarchitecture, Level 1) A1 F7 03 E8 00000000 25 00000004 05 00000008 00500000 Micro-architecture Level 1 • Interprets conventional machine instructions (Level 2) • Executed by digital hardware (Level 0) Digital logic Level 0 • CPU, constructed from digital logic gates • System bus • Memory Basic Concepts Welcome to Assembly Language Some Good Questions to Ask Assembly Language Applications Virtual Machine Concept Data Representation Boolean Operations Data representation • Computer is a construction of digital circuits with two states: on and off • You need to have the ability to translate between different representations to examine the content. • Common number systems: binary, octal, decimal and hexadecimal Binary representations • Electronic Implementation – Easy to store with bi-stable elements – Reliably transmitted on noisy and inaccurate wires 0 3.3V 2.8V 0.5V 0.0V 1 0 Binary numbers • Digits are 1 and 0 (a binary digit is called a bit) 1 = true 0 = false • MSB –most significant bit • LSB –least significant bit • Bit numbering: MSB LSB 1011001010011100 15 0 • A bit string could have different interpretations Unsigned binary integers • 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 27 26 25 24 23 22 21 20 Translating binary to decimal • Weighted positional notation shows how to calculate the decimal value (Dec) of each binary bit: dec = (Dn-1 2n-1) + (Dn-2 2n-2) + ... + (D1 21) + (D0 20) D = binary digit binary 00001001 = decimal 9: (1 23) + (1 20) = 9 Translating unsigned decimal to binary • Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: 37 = 100101 Binary addition • Starting with the LSB, add each pair of digits, include the carry if present. + bit position: carry: 1 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) 7 6 5 4 3 2 1 0 Integer storage sizes byte Standard sizes: word doubleword quadword 8 16 32 64 Practice: What is the largest unsigned integer that may be stored in 20 bits? Large measurements • Kilobyte (KB), bytes • Megabyte (MB), bytes • Gigabyte (GB), bytes • Terabyte (TB), bytes • Petabyte, bytes • Exabyte, bytes • Zettabyte, bytes • Yottabyte, bytes Hexadecimal integers • All values in memory are stored in binary. Because long binary numbers are hard to read, we use hexadecimal representation. Translating binary to hexadecimal • Each hexadecimal digit corresponds to 4 binary bits. • Example: Translate the binary integer 000101101010011110010100 to hexadecimal: Converting hexadecimal to decimal • Multiply each digit by its corresponding power of 16: dec = (D3 163) + (D2 162) + (D1 161) + (D0 160) 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. Converting decimal to hexadecimal decimal 422 = 1A6 hexadecimal 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 Important skill: Programmers frequently add and subtract the addresses of variables and instructions. Hexadecimal subtraction • When a borrow is required from the digit to the left, add 10h to the current digit's value: -1 C6 A2 24 75 47 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? Signed integers • The 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 the highest digit of a hexadecimal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D Two's complement notation For Binary Steps: – Complement (reverse) each bit – Add 1 Note that 00000001 + 11111111 = 00000000 Hexadecimal Two’s Complement Steps: – Complement (reverse) each digit (to reverse the bits of a hexadecimal digit is to subtract the digit from 15.) – Add 1 Unsigned 2’s Complement Subtraction • Example: Find 0101 01002 – 0100 00112 • 84 -67 = 84 +(-67) = 17 1 0101 0100 0101 0100 – 0100 0011 +1011 1101 2’s comp 0001 0001 • The carry of 1 indicates that no correction of the result is required. Unsigned 2’s Complement Subtraction • Example: Find 010000112 – 010101002 • 67 - 84= 67+ (-84) = -17 0 01000011 01000011 – 01010100 2’s comp + 10101100 11101111 00010001 2’s comp • The carry of 0 indicates that a correction of the result is required. • Result = – (00010001) Ranges of signed integers • The highest bit is reserved for the sign. This limits the range: Basic Concepts Welcome to Assembly Language Some Good Questions to Ask Assembly Language Applications Virtual Machine Concept Data Representation Boolean Operations Boolean algebra • Boolean expressions created from: – NOT, AND, OR NOT • Inverts (reverses) a Boolean value • Truth table for Boolean NOT operator: Digital gate diagram for NOT: NOT AND • Truth if both are true • Truth table for Boolean AND operator: Digital gate diagram for AND: AND OR • True if either is true • Truth table for Boolean OR operator: Digital gate diagram for OR: OR Implementation of gates Operator Precedence • Examples showing the order of operations: Truth Tables (1 of 2) • A Boolean function has one or more Boolean inputs, and returns a single Boolean output. • A truth table shows all the inputs and outputs of a Boolean function Example: X Y Truth Tables (2 of 2) • Example: X Y 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 Each layer in a computer's architecture is an abstraction of a machine layers can be hardware or software Boolean expressions are essential to the design of computer hardware and software Lecturer Website • http://www.bu.edu.eg/staff/rashaabdelkreem14