Assembly Language: Overview! Jennifer Rexford!

Similar documents
Assembly Language: Function Calls" Jennifer Rexford!

Instruction Set Architecture

CHAPTER 7: The CPU and Memory

Central Processing Unit (CPU)

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

İSTANBUL AYDIN UNIVERSITY

Summary of the MARIE Assembly Language

MACHINE ARCHITECTURE & LANGUAGE

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

OAMulator. Online One Address Machine emulator and OAMPL compiler.

CPU Organisation and Operation

1 Classical Universal Computer 3

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Lecture 27 C and Assembly

Machine-Level Programming II: Arithmetic & Control

CS61: Systems Programing and Machine Organization

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

MICROPROCESSOR AND MICROCOMPUTER BASICS

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

X86-64 Architecture Guide

1/20/2016 INTRODUCTION

(Refer Slide Time: 00:01:16 min)

Computer Organization. and Instruction Execution. August 22

Intel 8086 architecture

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

EC 362 Problem Set #2

CS:APP Chapter 4 Computer Architecture. Wrap-Up. William J. Taffe Plymouth State University. using the slides of

LSN 2 Computer Processors

Programming from the Ground Up. Jonathan Bartlett

Return-oriented programming without returns

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

CPU Organization and Assembly Language

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

Computer Organization and Architecture

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

CHAPTER 4 MARIE: An Introduction to a Simple Computer

Chapter 5 Instructor's Manual

Processor Architectures

Microprocessor & Assembly Language

Machine Programming II: Instruc8ons

Instruction Set Architecture (ISA)

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Programming from the Ground Up

Chapter 2 Basic Structure of Computers. Jin-Fu Li Department of Electrical Engineering National Central University Jungli, Taiwan

CSC 2405: Computer Systems II

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

Operating System Overview. Otto J. Anshus

Programming from the Ground Up

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Levels of Programming Languages. Gerald Penn CSC 324

Administrative Issues

Chapter 2 Logic Gates and Introduction to Computer Architecture

Compilers I - Chapter 4: Generating Better Code

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Here is a diagram of a simple computer system: (this diagram will be the one needed for exams) CPU. cache

Management Challenge. Managing Hardware Assets. Central Processing Unit. What is a Computer System?

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

Instruction Set Design

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Notes on Assembly Language

CHAPTER 6 TASK MANAGEMENT

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Language Processing Systems

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

An Overview of Stack Architecture and the PSC 1000 Microprocessor

4 Machine Language. Make everything as simple as possible, but not simpler. Albert Einstein ( )

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

Learning Outcomes. Simple CPU Operation and Buses. Composition of a CPU. A simple CPU design

EE361: Digital Computer Organization Course Syllabus

Instruction Set Architecture

A Tiny Guide to Programming in 32-bit x86 Assembly Language

PART B QUESTIONS AND ANSWERS UNIT I

Python Programming: An Introduction to Computer Science

Introduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

CS 40 Computing for the Web

CHAPTER 1 ENGINEERING PROBLEM SOLVING. Copyright 2013 Pearson Education, Inc.

TYPES OF COMPUTERS AND THEIR PARTS MULTIPLE CHOICE QUESTIONS

How To Write Portable Programs In C

CSCI 4717 Computer Architecture. Function. Data Storage. Data Processing. Data movement to a peripheral. Data Movement

Hardware Assisted Virtualization

A3 Computer Architecture

Let s put together a Manual Processor

picojava TM : A Hardware Implementation of the Java Virtual Machine

Chapter 7D The Java Virtual Machine

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Computer Organization and Components

Chapter 1 Computer System Overview

Copyright 2012 Pearson Education, Inc. Chapter 1 INTRODUCTION TO COMPUTING AND ENGINEERING PROBLEM SOLVING

Central Processing Unit

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

Transcription:

Assembly Language: Overview! Jennifer Rexford! 1

Goals of this Lecture! Help you learn:! The basics of computer architecture! The relationship between C and assembly language! IA-32 assembly language, through an example! 2

Context of this Lecture! Second half of the course! Starting Now! Afterward! C Language! Assembly Language! language! levels! tour! Application Program! Operating System! service! levels! tour! Machine Language! Hardware! 3

Three Levels of Languages! 4

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

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 $0, %ecx loop: cmpl $1, %edx jle endloop addl $1, %ecx movl %edx, %eax andl $1, %eax je else movl %edx, %eax addl %eax, %edx addl %eax, %edx addl $1, %edx jmp endif else: sarl $1, %edx endif: jmp loop endloop: 6

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! 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 9222 9120 1121 A120 1121 A121 7211 0000 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 000A 000B 000C 000D 000E 000F 0000 0000 0000 FE10 FACE CAFE ACED CEDE 1234 5678 9ABC DEF0 0000 0000 F00D 0000 0000 0000 EEEE 1111 EEEE 1111 0000 0000 B1B2 F1F5 0000 0000 0000 0000 0000 0000 Unreadable by humans! 7

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.! 8

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! 9

Computer Architecture! 10

A Typical Computer! CPU... CPU Memory Chipset I/O bus ROM Network 11

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! Control Unit Registers CPU ALU Data bus Random Access Memory (RAM) 12

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

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 to go to the next instruction! Or, load a new value to jump to a new location! EIP 14

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! src1 src2 operation ALU ALU flag/carry dst 15

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,! 16

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! EAX EBX In practice, can manipulate different sizes of data! 17

C Code vs. Assembly Code! 18

Kinds of Instructions! count = 0; while (n > 1) { } count++; if (n & 1) n = n*3 + 1; else n = n/2; 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)! 19

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

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

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 22

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

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

Changing Program Flow! count=0; while (n>1) { } count++; if (n&1) n = n*3+1; else n = n/2; 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 )! 25

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 26

Jump and Labels: While Loop! while (n>1) { loop: cmpl $1, %edx jle endloop Checking if EDX is less than or equal to 1. } jmp loop endloop: 27

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

Jump and Labels: If-Then-Else! if (n&1)... else... else block then block else: endif: movl %edx, %eax andl $1, %eax je else jmp endif 29

Jump and Labels: If-Then-Else! movl $0, %ecx count=0; while(n>1) { } count++; if (n&1) n = n*3+1; else n = n/2; else block then block loop: cmpl $1, %edx jle endloop addl $1, %ecx movl %edx, %eax andl $1, %eax je else movl %edx, %eax addl %eax, %edx addl %eax, %edx addl $1, %edx jmp endif else: sarl $1, %edx endif: jmp loop endloop: 30

Making the Code More Efficient! movl $0, %ecx count=0; while(n>1) { } count++; if (n&1) n = n*3+1; else n = n/2; Replace with jmp loop loop: cmpl $1, %edx jle endloop addl $1, %ecx movl %edx, %eax andl $1, %eax je else movl %edx, %eax addl %eax, %edx addl %eax, %edx addl $1, %edx jmp endif else: sarl $1, %edx endif: jmp loop endloop: 31

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

Reading IA-32 Assembly Language! Referring to a register: percent sign ( % )! 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! 33

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! 34