ASSEMBLY LANGUAGE Introduction of Assembly Language Introduction Assembly
ASSEMBLY LANGUAGE Introduction of Assembly Language
Introduction Assembly
Content q Course Description q Basic Concepts of Assembly Language v. Welcome to Assembly Language v. Virtual Machine Concept v. Data Representation v. Boolean Operations
Course Description q Prerequisites: § Structured Programming Language q Textbook References: § Assembly language for x 86 processors q Resources: § http: //web. sau. edu/Lillis. Kevin. M/csci 240/masmdocs/ § http: //kipirvine. com/asm/4 th/asmsources/ § http: //forum. codecall. net/topic/62064 -assembly-language-resources/
Course Description q Grading : • Final Exam • Year work • Oral • Laboratory and Practice • Sum 65 10 10 15 100 q Timing: • Lecture • Practice • Exam 3 2 3
Content q Course Description q Basic Concepts of Assembly Language v. Welcome to Assembly Language v. Virtual Machine Concept v. Data Representation v. Boolean Operations
Basic Concepts q Welcome to Assembly Language § Some Good Questions to Ask q Virtual Machine Concept q Data Representation q Boolean Operations
Welcome to Assembly Language q 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? 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 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: 1 s and 0 s § 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 High. Level Language?
Basic Concepts q Welcome to Assembly Language § Some Good Questions to Ask § Assembly Language Applications q Virtual Machine Concept q Data Representation q 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); Gener al gi st Data er Re Assembly Language: mov eax, A mul B add eax, C call Write. Intel Machine Language: A 1 F 7 03 E 8 0000 25 00000004 05 00000008 00500000
Virtual machines Abstractions for computers
High-level language q. Level 5 • Application-oriented languages • Programs are compiled into assembly language (Level 4) cout << (A * B + C);
Assembly language q. 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 Write. Int
Operating system q. Level 3 • Provides services • Programs translated and run at the instruction set architecture level (Level 2)
Instruction set architecture q. Level 2 • Also known as conventional machine language • Executed by Level 1 program (microarchitecture, Level 1) A 1 F 7 03 E 8 0000 25 00000004 05 00000008 00500000
Micro-architecture q. Level 1 • Interprets conventional machine instructions (Level 2) • Executed by digital hardware (Level 0)
Digital logic q. Level 0 • CPU, constructed from digital logic gates • System bus • Memory
Basic Concepts q Welcome to Assembly Language § Some Good Questions to Ask § Assembly Language Applications q Virtual Machine Concept q Data Representation q 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. 3 V 2. 8 V 0. 5 V 0. 0 V 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: • 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
Translating binary to decimal • Weighted positional notation shows how to calculate the decimal value (Dec) of each binary bit: dec = (Dn-1 2 n-1) + (Dn-2 2 n-2) +. . . + (D 1 21) + (D 0 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.
Integer storage sizes Standard sizes: 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 = (D 3 163) + (D 2 162) + (D 1 161) + (D 0 160) Examples: • Hex 1234 = (1 163) + (2 162) + (3 161) + (4 160) = decimal 4, 660. • Hex 3 BA 4 = (3 163) + (11 * 162) + (10 161) + (4 160) = decimal 15, 268.
Converting decimal to hexadecimal 422 = 1 A 6 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 6 D 1 28 58 80 1 6 A 4 B B 5 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 10 h to the current digit's value: -1 C 6 A 2 24 75 47 2 E Practice: The address of var 1 is 00400020. The address of the next variable after var 1 is 0040006 A. How many bytes are used by var 1?
Signed integers • The highest bit indicates the sign. 1 = negative, 0 = positive If the highest digit of a hexadecimal integer is > 7, the value is negative. Examples: 8 A, C 5, A 2, 9 D
Two's complement notation For Binary Steps: – Complement (reverse) each bit – Add 1 Note that 00000001 + 1111 = 0000
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 0100 0101 0100 – 0100 0011 +1011 1101 2’s comp 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 – 01010100 2’s comp + 10101100 11101111 0001 2’s comp • The carry of 0 indicates that a correction of the result is required. • Result = – (0001)
Ranges of signed integers • The highest bit is reserved for the sign. This limits the range:
Basic Concepts q Welcome to Assembly Language § Some Good Questions to Ask § Assembly Language Applications q Virtual Machine Concept q Data Representation q 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:
AND • Truth if both are true • Truth table for Boolean AND operator: Digital gate diagram for AND:
OR • True if either is true • Truth table for Boolean OR operator: Digital gate diagram for 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 q Assembly language helps you learn how software is constructed at the lowest levels q Assembly language has a one-to-one relationship with machine language q Each layer in a computer's architecture is an abstraction of a machine § layers can be hardware or software q Boolean expressions are essential to the design of computer hardware and software
Lecturer Website • http: //www. bu. edu. eg/staff/rashaabdelkreem 14
- Slides: 59