1 Assembly Language: Overview. 2 If you’re a computer, What’s the fastest way to multiply by 5? What’s the fastest way to divide by 5?

Slides:



Advertisements
Similar presentations
Practical Malware Analysis
Advertisements

Chapter 2 Data Manipulation Dr. Farzana Rahman Assistant Professor Department of Computer Science James Madison University 1 Some sldes are adapted from.
Computer Organization and Architecture
Processor Function Topic 3.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Room: E-3-31 Phone: Dr Masri Ayob TK 2123 COMPUTER ORGANISATION & ARCHITECTURE Lecture 5: CPU and Memory.
1 Lecture 2: Review of Computer Organization Operating System Spring 2007.
Processor Technology and Architecture
1 Computer Architecture and Assembly Language COS 217.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
1 Assembly Language Professor Jennifer Rexford COS 217.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Chapter 4 Processor Technology and Architecture. Chapter goals Describe CPU instruction and execution cycles Explain how primitive CPU instructions are.
Choice for the rest of the semester New Plan –assembler and machine language –Operating systems Process scheduling Memory management File system Optimization.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Group 5 Alain J. Percial Paula A. Ortiz Francis X. Ruiz.
CEG 320/520: Computer Organization and Assembly Language Programming1 CEG 320/520 Computer Organization and Assembly Language Programming.
Basic Operational Concepts of a Computer
Processor Structure & Operations of an Accumulator Machine
Levels of Architecture & Language CHAPTER 1 © copyright Bobby Hoggard / material may not be redistributed without permission.
Machine Instruction Characteristics
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Computer Systems Organization CS 1428 Foundations of Computer Science.
Computers organization & Assembly Language Chapter 0 INTRODUCTION TO COMPUTING Basic Concepts.
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
Module : Algorithmic state machines. Machine language Machine language is built up from discrete statements or instructions. On the processing architecture,
CSC 2400 Computer Systems I Lecture 9 Deeper into Assembly.
Important Concepts  Parts of the CPU  Arithmetic/Logic Unit  Control Unit  Registers  Program Counter  Instruction Register  Fetch/Decode/Execute.
1 ICS 51 Introductory Computer Organization Fall 2009.
Computer Structure & Architecture 7b - CPU & Buses.
Electronic Analog Computer Dr. Amin Danial Asham by.
COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE Lecture 21 & 22 Processor Organization Register Organization Course Instructor: Engr. Aisha Danish.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Overview von Neumann Architecture Computer component Computer function
Question What technology differentiates the different stages a computer had gone through from generation 1 to present?
X86 Assembly Language We will be using the nasm assembler (other assemblers: MASM, as, gas)
1 Assembly Language Part 2 Professor Jennifer Rexford COS 217.
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
Simple ALU How to perform this C language integer operation in the computer C=A+B; ? The arithmetic/logic unit (ALU) of a processor performs integer arithmetic.
CMSC 104, Lecture 061 Stored Programs A look at how programs are executed.
1 Basic Processor Architecture. 2 Building Blocks of Processor Systems CPU.
Computer Organization 1
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Week 6 Dr. Muhammad Ayaz Intro. to Assembly Language.
Operating Systems A Biswas, Dept. of Information Technology.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
Chapter 12 Processor Structure and Function. Central Processing Unit CPU architecture, Register organization, Instruction formats and addressing modes(Intel.
CPSC 121: Models of Computation
Chapter Overview General Concepts IA-32 Processor Architecture
Computers’ Basic Organization
Assembly language.
Control Unit Lecture 6.
William Stallings Computer Organization and Architecture 8th Edition
Homework Reading Labs PAL, pp
Introduction of microprocessor
Number Representations and Basic Processor Architecture
Lecture 30 (based on slides by R. Bodik)
CS 301 Fall 2002 Computer Organization
Fundamentals of Computer Organisation & Architecture
Processor Organization and Architecture
Homework Reading Machine Projects Labs PAL, pp
Computer Instructions
Computer Architecture and System Programming Laboratory
Presentation transcript:

1 Assembly Language: Overview

2 If you’re a computer, What’s the fastest way to multiply by 5? What’s the fastest way to divide by 5?

3 Second Half of the Course Toward the hardware  Computer architecture  Assembly language  Machine language Toward the operating system  Virtual memory  Dynamic memory management  Processes and pipes  Signals and system calls

4 Goals of Today’s Lecture Help you learn…  Basics of computer architecture  Relationship between C and assembly language  IA-32 assembly language through an example Why?  Write faster code in high-level languages  Understand how the underlying hardware works  Know how to write assembly code when needed

5 Three Levels of Languages

6 High-Level Language Make programming easier by describing operations in a semi- natural language Increase the portability of the code One line may involve many low-level operations Examples: C, C++, Java, Pascal, … count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; }

7 Assembly Language Tied to the specifics of the underlying machine Commands and names to make the code readable and writeable by humans Hand-coded assembly code may be more efficient E.g., IA-32 from Intel movl%edx, %eax andl$1, %eax jeelse jmpendif else: endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx loop: cmpl$1, %edx jleendloop jmploop endloop: movl$0, %ecx

8 Machine Language Also tied to the underlying machine What the computer sees and deals with Every instruction is a sequence of one or more numbers All stored in memory on the computer, and read and executed Unreadable by humans A A A 000B 000C 000D 000E 000F FE10 FACE CAFE ACED CEDE ABC DEF F00D EEEE 1111 EEEE B1B2 F1F

9 Why Learn Assembly Language? Write faster code (even in high-level language)  By understanding which high-level constructs are better  … in terms of how efficient they are at the machine level Understand how things work underneath  Learn the basic organization of the underlying machine  Learn how the computer actually runs a program  Design better computers in the future Some software is still written in assembly language  Code that really needs to run quickly  Code for embedded systems, network processors, etc.

10 Why Learn Intel IA-32 Assembly? Program natively on our computing platform  Rather than using an emulator to mimic another machine Learn instruction set for the most popular platform  Most likely to work with Intel platforms in the future But, this comes at some cost in complexity  IA-32 has a large and varied set of instructions  More instructions than are really useful in practice Fortunately, you won’t need to use everything

11 Computer Architecture

12 A Typical Computer CPU ChipsetMemory I/O bus CPU... Network ROM

13 Von Neumann Architecture Central Processing Unit  Control unit –Fetch, decode, and execute  Arithmetic and logic unit –Execution of low-level operations  General-purpose registers –High-speed temporary storage  Data bus –Provide access to memory Random Access Memory (RAM) Control Unit ALU CPU Registers Data bus

14 Von Neumann Architecture Memory  Store executable machine-language instructions (text section)  Store data (rodata, data, bss, heap, and stack sections) Random Access Memory (RAM) Control Unit ALU CPU Registers Data bus TEXT RODATA DATA BSS HEAP STACK

15 Control Unit: Instruction Pointer Stores the location of the next instruction  Address to use when reading machine-language instructions from memory (i.e., in the text section) Changing the instruction pointer (EIP)  Increment by one to go to the next instruction  Or, load a new value to “jump” to a new location EIP

16 Control Unit: Instruction Decoder Determines what operations need to take place  Translate the machine-language instruction Control what operations are done on what data  E.g., control what data are fed to the ALU  E.g., enable the ALU to do multiplication or addition  E.g., read from a particular address in memory ALU src1src2 dst operationflag/carry ALU

17 Registers Small amount of storage on the CPU  Can be accessed more quickly than main memory Instructions move data in and out of registers  Loading registers from main memory  Storing registers to main memory Instructions manipulate the register contents  Registers essentially act as temporary variables  For efficient manipulation of the data Registers are the top of the memory hierarchy  Ahead of main memory, disk, tape, …

18 Keeping it Simple: All 32-bit Words Simplifying assumption: all data in four-byte units  Memory is 32 bits wide  Registers are 32 bits wide In practice, can manipulate different sizes of data EAX EBX

19 C Code vs. Assembly Code

20 Kinds of Instructions Reading and writing data  count = 0  n Arithmetic and logic operations  Increment: count++  Multiply: n * 3  Divide: n/2  Logical AND: n & 1 Checking results of comparisons  Is (n > 1) true or false?  Is (n & 1) non-zero or zero? Changing the flow of control  To the end of the while loop (if “n > 1”)  Back to the beginning of the loop  To the else clause (if “n & 1” is 0) count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; }

21 Variables in Registers Registers n %edx count %ecx Referring to a register: percent sign (“%”) count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; }

22 Immediate and Register Addressing count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } movl$0, %ecx addl$1, %ecx Referring to a immediate operand: dollar sign (“$”) Read directly from the instruction written to a register

23 Immediate and Register Addressing count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } movl%edx, %eax andl$1, %eax Computing intermediate value in register EAX

24 movl%edx, %eax addl%eax, %edx addl$1, %edx Immediate and Register Addressing count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } Adding n twice is cheaper than multiplication!

25 sarl$1, %edx Immediate and Register Addressing count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } Shifting right by 1 bit is cheaper than division!

26 Changing Program Flow Cannot simply run next instruction  Check result of a previous operation  Jump to appropriate next instruction Flags register (EFLAGS)  Stores the status of operations, such as comparisons, as a side effect  E.g., last result was positive, negative, zero, etc. Jump instructions  Load new address in instruction pointer Example jump instructions  Jump unconditionally (e.g., “}”)  Jump if zero (e.g., “n&1”)  Jump if greater/less (e.g., “n>1”) count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; }

27 Conditional and Unconditional Jumps Comparison cmpl compares two integers  Done by subtracting the first number from the second –Discarding the results, but setting flags as a side effect  Example: –cmpl $1, %edx (computes %edx – 1) –jle endloop (checks whether result was 0 or negative) Logical operation andl compares two integers  Example: –andl $1, %eax (bit-wise AND of %eax with 1) –je else (checks whether result was 0) Also, can do an unconditional branch jmp  Example: –jmp endif and jmp loop

28 … loop: cmpl$1, %edx jleendloop jmploop endloop: Jump and Labels: While Loop while (n>1) { } Checking if 1 is less than or equal to EDX.

29 movl%edx, %eax andl$1, %eax jeelse jmpendif else: endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx loop: cmpl$1, %edx jleendloop jmploop endloop: movl$0, %ecx Jump and Labels: While Loop count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; }

30 movl%edx, %eax andl$1, %eax jeelse jmpendif else: endif: … Jump and Labels: If-Then-Else if (n&1)... else... “then” block “else” block …

31 movl%edx, %eax andl$1, %eax jeelse jmpendif else: endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx loop: cmpl$1, %edx jleendloop jmploop endloop: movl$0, %ecx Jump and Labels: If-Then-Else count=0; while(n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } “then” block “else” block

32 movl%edx, %eax andl$1, %eax jeelse jmpendif else: endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx loop: cmpl$1, %edx jleendloop jmploop endloop: movl$0, %ecx Making the Code More Efficient… count=0; while(n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } Replace with “jmp loop”

33 movl%edx, %eax andl$1, %eax jeelse jmpendif else: endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx loop: cmpl$1, %edx jleendloop jmploop endloop: movl$0, %ecx Complete Example count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } n %edx count %ecx

34 Reading IA-32 Assembly Language Referring to a register: percent size (“%”)  E.g., “%ecx” or “%eip” Referring to immediate operand: dollar sign (“$”)  E.g., “$1” for the number 1 Storing result: typically in the second argument  E.g. “addl $1, %ecx” increments register ECX  E.g., “movl %edx, %eax” moves EDX to EAX Assembler directives: starting with a period (“.”)  E.g., “.section.text” to start the text section of memory Comment: pound sign (“#”)  E.g., “# Purpose: Convert lower to upper case”

35 Conclusions Assembly language  In between high-level language and machine code  Programming the “bare metal” of the hardware  Loading and storing data, arithmetic and logic operations, checking results, and changing control flow To get more familiar with IA-32 assembly  Read more assembly-language examples –Chapter 3 of Bryant and O’Hallaron book  Generate your own assembly-language code –gcc217 –S –O2 code.c