1 Computer Architecture and Assembly Language COS 217.

Slides:



Advertisements
Similar presentations
INSTRUCTION SET ARCHITECTURES
Advertisements

Computer Organization and Assembly Languages Yung-Yu Chuang
Introduction to 8086 Microprocessor
Computer Organization and Architecture
CS2422 Assembly Language & System Programming September 19, 2006.
IA-32 Processor Architecture
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
Assembly Language: IA-32 Instructions
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
CS2422 Assembly Language & System Programming November 28, 2006.
1 Assembly Language Professor Jennifer Rexford COS 217.
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
Microprocessors Introduction to ia32 Architecture Jan 31st, 2002.
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.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
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?
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
An Introduction to 8086 Microprocessor.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
Machine Instruction Characteristics
1 Fundamental of Computer Suthida Chaichomchuen : SCC
Low Level Programming Lecturer: Duncan Smeed Overview of IA-32 Part 1.
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Computer Architecture and Organization
1 ICS 51 Introductory Computer Organization Fall 2009.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Computers organization & Assembly Language Chapter 1 THE 80x86 MICROPROCESSOR.
Sahar Mosleh California State University San MarcosPage 1 Intel IA-32 Architecture This lecture describes the architecture of the Intel IA-32 processor.
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.
INTRODUCTION TO INTEL X-86 FAMILY
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 x86 Programming Model Microprocessor Computer Architectures Lab Components of any Computer System Control – logic that controls fetching/execution of.
BITS Pilani Pilani Campus Pawan Sharma Lecture / ES C263 INSTR/CS/EEE F241 Microprocessor Programming and Interfacing.
The Microprocessor & Its Architecture A Course in Microprocessor Electrical Engineering Department Universitas 17 Agustus 1945 Jakarta.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Computer Science 516 Intel x86 Overview. Intel x86 Family Eight-bit 8080, 8085 – 1970s 16-bit 8086 – was internally 16 bits, externally 8 bits.
Chapter 12 Processor Structure and Function. Central Processing Unit CPU architecture, Register organization, Instruction formats and addressing modes(Intel.
Chapter Overview General Concepts IA-32 Processor Architecture
Assembly language.
Credits and Disclaimers
IA32 Processors Evolutionary Design
x86 Processor Architecture
Introduction to 8086 Microprocessor
8086 Microprocessor.
Basic Microprocessor Architecture
Assembly IA-32.
Homework Reading Continue work on mp1
Basic of Computer Organization
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Assembly Language: IA-32 Instructions
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
CS 301 Fall 2002 Computer Organization
Machine-Level Programming 2 Control Flow
The Microprocessor & Its Architecture
Assembly Language (CSW 353)
Computer Architecture CST 250
Introduction to Microprocessor Programming
Other Processors Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
CSC 497/583 Advanced Topics in Computer Security
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

1 Computer Architecture and Assembly Language COS 217

2 Goals of Today’s Lecture Computer architecture  Central processing unit (CPU)  Fetch-decode-execute cycle  Memory hierarchy, and other optimization Assembly language  Machine vs. assembly vs. high-level languages  Motivation for learning assembly language  Intel Architecture (IA32) assembly language

3 Levels of Languages Machine language  What the computer sees and deals with  Every command is a sequence of one or more numbers Assembly language  Command numbers replaced by letter sequences that are easier to read  Still have to work with the specifics of the machine itself High-level language  Make programming easier by describing operations in a natural language  A single command replaces a group of low-level assembly language commands

4 Why Learn Assembly Language? 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 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 Some software is still written in assembly language  Code that really needs to run quickly  Code for embedded systems, network processors, etc.

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

6 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 Memory  Store instructions  Store data Random Access Memory (RAM) Control Unit ALU CPU Registers Data bus

7 Control Unit Instruction pointer  Stores the location of the next instruction –Address to use when reading from memory  Changing the instruction pointer –Increment by one to go to the next instruction –Or, load a new value to “jump” to a new location Instruction decoder  Determines what operations need to take place –Translate the machine-language instruction  Control the registers, arithmetic logic unit, and memory –E.g., control which registers are fed to the ALU –E.g., enable the ALU to do multiplication –E.g., read from a particular address in memory

8 Example: Kinds of Instructions Storing values in registers  count = 0  n Arithmetic and logic operations  Increment: count++  Multiply: n * 3  Divide: n/2  Logical AND: n & 1 Checking results of comparisons  while (n > 1)  if (n & 1) Jumping  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; }

9 Size of Variables Data types in high-level languages vary in size  Character: 1 byte  Short, int, and long: varies, depending on the computer  Pointers: typically 4 bytes  Struct: arbitrary size, depending on the elements Implications  Need to be able to store and manipulate in multiple sizes  Byte (1 byte), word (2 bytes), and extended (4 bytes)  Separate assembly-language instructions –e.g., addb, addw, addl  Separate ways to access (parts of) a 4-byte register

10 Four-Byte Memory Words Memory Byte order is little endian Byte 4 Byte 0 Byte 5 Byte 1Byte 2 Byte 6 Byte 3 Byte 7

11 IA32 General Purpose Registers General-purpose registers EAX EBX ECX EDX ESI EDI 31 0 AX BX CX DX 16-bit 32-bit DI SI AL AH BL CL DL BH CH DH 8 715

12 Registers for Executing the Code Execution control flow  Instruction pointer (EIP) –Address in memory of the current instruction  Flags (EFLAGS) –Stores the status of operations, such as comparisons –E.g., last result was positive/negative, was zero, etc. Function calls (more on these later!)  Stack register (ESP) –Address of the top of the stack  Base pointer (EBP) –Address of a particular element on the stack –Access function parameters and local variables

13 Other Registers that you don’t much care about Segment registers  CS, SS, DS, ES, FS, GS Floating Point Unit (FPU) (x87)  Eight 80-bit registers (ST0, …, ST7)  16-bit control, status, tag registers  11-bit opcode register  48-bit FPU instruction pointer, data pointer registers MMX  Eight 64-bit registers SSE and SSE2  Eight 128-bit registers  32-bit MXCRS register System  I/O ports  Control registers (CR0, …, CR4)  Memory management registers (GDTR, IDTR, LDTR)  Debug registers (DR0, …, DR7)  Machine specific registers  Machine check registers  Performance monitor registers

14 Reading IA32 Assembly Language Assembler directives: starting with a period (“.”)  E.g., “.section.text” to start the text section of memory  E.g., “.loop” for the address of an instruction Referring to a register: percent size (“%”)  E.g., “%ecx” or “%eip” Referring to a constant: 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 Comment: pound sign (“#”)  E.g., “# Purpose: Convert lower to upper case”

15 movl%edx, %eax andl$1, %eax je.else jmp.endif.else:.endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx.loop: cmpl$1, %edx jle.endloop jmp.loop.endloop: movl$0, %ecx Detailed Example count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } n %edx count %ecx

16 Machine-Language Instructions Instructions have the form op source, dest “dest  dest  source” operation (move, add, subtract, etc.) first operand (and destination) second operand

17 Machine Language Machine language encodes instructions as a sequence of integers easily decodable (fast!) by the machine Instruction format: operand Opcode specifies “what operation to perform” (add, subtract, load, jump, etc.) Operand specifies what data on which to perform the operation (register A, memory at address B, etc.) operandopcode

18 Instruction Opcode  What to do Source operands  Immediate (in the instruction itself)  Register  Memory location  I/O port Destination operand  Register  Memory location  I/O port Assembly syntax Opcode source1, [source2,] destination

19 How Many Instructions to Have? Need a certain minimum set of functionality  Want to be able to represent any computation that can be expressed in a higher-level language Benefits of having many instructions  Direct implementation of many key operations  Represent a line of C in one (or just a few) lines of assembly Disadvantages of having many instructions  Larger opcode size  More complex logic to implement complex instructions  Hard to write compilers to exploit all the available instructions  Hard to optimize the implementation of the CPU

20 CISC vs. RISC Complex Instruction Set Computer (old fashioned, 1970s style) Examples: Vax ( ) Motorola ( ) 8086/80x86/Pentium ( ) Instructions of various lengths, designed to economize on memory (size of instructions) Reduced Instruction Set Computer (“modern”, 1980s style) Examples: MIPS (1985-?) Sparc ( ) IBM PowerPC (1990-?) ARM Instructions all the same size and all the same format, designed to economize on decoding complexity (and time, and power drain)

21 Data Transfer Instructions mov{b,w,l} source, dest  General move instruction push{w,l} source pushl %ebx # equivalent instructions subl $4, %esp movl %ebx, (%esp) pop{w,l} dest popl %ebx # equivalent instructions movl (%esp), %ebx addl$4, %esp Many more in Intel manual (volume 2)  Type conversion, conditional move, exchange, compare and exchange, I/O port, string move, etc. esp

22 Data Access Methods Immediate addressing: data stored in the instruction itself  movl $10, %ecx Register addressing: data stored in a register  movl %eax, %ecx Direct addressing: address stored in instruction  movl 2000, %ecx Indirect addressing: address stored in a register  movl (%eax), %ebx Base pointer addressing: includes an offset as well  movl 4(%eax), %ebx Indexed addressing: instruction contains base address, and specifies an index register and a multiplier (1, 2, or 4)  movl 2000(,%ecx,1), %ebx

23 Effective Address Displacement movl foo, %eax Base movl (%eax), %ebx Base + displacement movl foo(%eax), %ebx movl 1(%eax), %ebx (Index * scale) + displacement movl (,%eax,4), %ebx Base + (index * scale) + displacement movl foo(,%eax,4), %ebx eax ebx ecx edx esp ebp esi edi *+ None 8-bit 16-bit 32-bit Offset = Base Index scale displacement

24 Bitwise Logic Instructions Simple instructions and{b,w,l} source, destdest = source & dest or{b,w,l} source, destdest = source | dest xor{b,w,l} source, destdest = source ^ dest not{b,w,l} destdest = ^dest sal{b,w,l} source, dest (arithmetic)dest = dest << source sar{b,w,l} source, dest (arithmetic)dest = dest >> source Many more in Intel Manual (volume 2)  Logic shift  Rotation shift  Bit scan  Bit test  Byte set on conditions

25 Arithmetic Instructions Simple instructions  add{b,w,l} source, destdest = source + dest  sub{b,w,l} source, destdest = dest – source  inc(b,w,l} destdest = dest + 1  dec{b,w,l} destdest = dest – 1  neg(b,w,l} destdest = ^dest  cmp{b,w,l} source1, source2source2 – source1 Multiply  mul (unsigned) or imul (signed) mull %ebx# edx, eax = eax * ebx Divide  div (unsigned) or idiv (signed) idiv %ebx# edx = edx,eax / ebx Many more in Intel manual (volume 2)  adc, sbb, decimal arithmetic instructions

26 EFLAG Register & Condition Codes CFCF 1 PFPF 0 AFAF 0 ZFZF SFSF TFTF IFIF DFDF OFOF IO P L NTNT 0 RFRF VMVM ACAC VIFVIF VIPVIP IDID Reserved (set to 0) Carry flag Identification flag Virtual interrupt pending Virtual interrupt flag Alignment check Virtual 8086 mode Resume flag Nested task flag I/O privilege level Overflow flag Interrupt enable flag Direction flag Trap flag Sign flag Zero flag Auxiliary carry flag or adjust flag Parity flag

27 Branch Instructions Conditional jump  j{l,g,e,ne,...} targetif (condition) {eip = target} Unconditional jump  jmp target  jmp *register ComparisonSignedUnsigned  ee “equal”  ne “not equal” >ga “greater,above”  geae “...-or-equal” <lb “less,below”  lebe “...-or-equal” overflow/carry oc no ovf/carry nonc

28 Making the Computer Faster Memory hierarchy  Ranging from small, fast storage to large, slow storage  E.g., registers, caches, main memory, disk, CDROM, … Sophisticated logic units  Have dedicated logic units for specialized functions  E.g., right/left shifting, floating-point operations, graphics, network,… Pipelining  Overlap the fetch-decode-execute process  E.g., execute instruction i, while decoding i-1, and fetching i-2 Branch prediction  Guess which way a branch will go to avoid stalling the pipeline  E.g., assume the “for loop” condition will be true, and keep going And so on… see the Computer Architecture class!

29 Memory Hierarchy Register: 1x L1 cache: 2-4x L2 cache: ~10x L3 cache: ~50x DRAM: ~ x Disks: ~30M x CD-ROM Jukebox: >1000M x 10 2 bytes 10 4 bytes 10 5 bytes 10 6 bytes 10 9 bytes bytes bytes Capacity Access time

30 Conclusion Computer architecture  Central Processing Unit (CPU) and Random Access Memory (RAM)  Fetch-decode-execute cycle  Instruction set Assembly language  Machine language represented with handy mnemonics  Example of the IA-32 assembly language Next time  Portions of memory: data, bss, text, stack, etc.  Function calls, and manipulating contents of the stack