introduction to assembly

31
1 CMSC 313, F ‘09 Introduction to Assembly Topics Topics Assembly Overview Instructions Hardware

Upload: lassie

Post on 05-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Assembly. Topics Assembly Overview Instructions Hardware. Assembly overview. Some terminology and concepts A peek at what we will spend the rest of the semester talking about. A chance to talk about things at a high-level - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Assembly

1CMSC 313, F ‘09

Introduction to Assembly

• TopicsTopics– Assembly Overview– Instructions– Hardware

Page 2: Introduction to Assembly

2CMSC 313, F ‘09

Assembly overview

Some terminology and conceptsSome terminology and concepts• A peek at what we will spend the rest of the A peek at what we will spend the rest of the

semester talking about.semester talking about.• A chance to talk about things at a high-levelA chance to talk about things at a high-level

– we will spend the rest of the course worrying about details!

Page 3: Introduction to Assembly

3CMSC 313, F ‘09

Programs and Processes

• ProgramProgram: a file (an executable : a file (an executable image image or set of or set of instructions that can be interpreted).instructions that can be interpreted).

• ProcessProcess: an instance of a program that is running.: an instance of a program that is running.

• A process is A process is alivealive, a program is just a file., a program is just a file.

Page 4: Introduction to Assembly

4CMSC 313, F ‘09

hello.c

#include <stdio.h>#include <stdio.h>

int main( )int main( )

{{

printf(“%s\n”, “Hello World”);printf(“%s\n”, “Hello World”);

return 0;return 0;

}}

Page 5: Introduction to Assembly

5CMSC 313, F ‘09

Text vs. Binary

The original C program (and some of the intermediate The original C program (and some of the intermediate representations) are text. Typically ASCII encoded.representations) are text. Typically ASCII encoded.

The object code and executable are binary.The object code and executable are binary.

– – anything that is not text is binary!anything that is not text is binary!

– – The actual program representation depends on theThe actual program representation depends on the

machine language (instruction set) supported by the machine language (instruction set) supported by the computer.computer.

Page 6: Introduction to Assembly

6CMSC 313, F ‘09

Machine Language

• A sequence of bits.A sequence of bits.• Organized in to words.Organized in to words.

– different architectures use different length words

typical sizes are 16, 32 or 64 bits.

• The bits are an encoding of some operations that The bits are an encoding of some operations that should take place in the CPU.should take place in the CPU.

Page 7: Introduction to Assembly

7CMSC 313, F ‘09

Instructions

• Each instruction does something relatively simple.Each instruction does something relatively simple.• move some bits around• treat some bits as base 2 numbers and apply arithmetic

operations.• send/read some bits to/from I/O devices.• select the group of bits that will make up the next

instruction

Page 8: Introduction to Assembly

8CMSC 313, F ‘09

Example Machine Language

• Each instruction is 4 bits long.Each instruction is 4 bits long.• There are 2There are 244=16 possible unique instructions.=16 possible unique instructions.• We can organize the bits to make it easy to design a We can organize the bits to make it easy to design a

machine that can execute the instructions.machine that can execute the instructions.– first 2 bits represent an operation.– last 2 bits represent what to operate on.

Page 9: Introduction to Assembly

9CMSC 313, F ‘09

A Student’s Machine Language

The first 2 bit determine the operationThe first 2 bit determine the operation

– 00: Eat– 01: Play– 10: Study– 11: Watch MTV

The last 2 bits depend on the operationThe last 2 bits depend on the operation

Page 10: Introduction to Assembly

10CMSC 313, F ‘09

00xy - EAT InstructionsThe last 2 bits specify what we eat:The last 2 bits specify what we eat:

– 00 Pizza– 01 Burger– 10 Macaroni and Cheese– 11 Softshell Crab

We have 4 different eat instructions.We have 4 different eat instructions.– 0000 eat pizza– 0001 eat burger– 0010 eat mac and cheese– 0011 eat softshell crab

Page 11: Introduction to Assembly

11CMSC 313, F ‘09

01xy - Play InstructionsThe last 2 bits specify what we play:The last 2 bits specify what we play:

– 00 Surf the Web– 01 Doom– 10 Music– 11 Pin-the-tail-on-the-donkey

We have 4 different play instructionsWe have 4 different play instructions– 0100 Surf the web– 0101 Play Doom– 0110 Play Music– 0111 Play Pin-the-tail-on-the-donkey

Page 12: Introduction to Assembly

12CMSC 313, F ‘09

11xy MTV Instructions

• 4 possible codes: 1100, 1101, 1110, 11114 possible codes: 1100, 1101, 1110, 1111• All four mean the same thing (the last 2 bits don’t All four mean the same thing (the last 2 bits don’t

matter)matter)• It’s OK to waste 2 bits, after all – we’re already It’s OK to waste 2 bits, after all – we’re already

wasting our time with MTV, right?wasting our time with MTV, right?

Page 13: Introduction to Assembly

13CMSC 313, F ‘09

10xy - Study InstructionsThe 3rd bit specifies what we study:The 3rd bit specifies what we study:

– x specifies what we study

• 0 Comp. Org.• 1 American History

The 4th bit specifies how hard we study:The 4th bit specifies how hard we study:– y specifies how hard we study

• 0 Holding book, but eyes closed• 1 Cram session

We have 4 different study instructionsWe have 4 different study instructions– 1000 - Study Comp Org Holding book with eyes closed– 1001 - Cram session for Am. History– 1010 - Study Am. History holding book with eyes close– 1011 - Cram session for Comp Org

Page 14: Introduction to Assembly

14CMSC 313, F ‘09

A Simple Program

00110011 Eat soft shell crab.Eat soft shell crab.

10101010 Study history with eyes closed.Study history with eyes closed.

01000100 Surf the web.Surf the web.

11011101 Watch MTVWatch MTV..10011001 Cram for Comp. Org.Cram for Comp. Org.

01010101 Play DoomPlay Doom

Page 15: Introduction to Assembly

15CMSC 313, F ‘09

GOBACK Instruction

Let’s make things more interesting:Let’s make things more interesting:Replace the MTV (11xy) instruction with Go back instructionReplace the MTV (11xy) instruction with Go back instruction

Last 2 bits determine how far back we go:Last 2 bits determine how far back we go:– 00 Repeat previous instruction– 01 Go back 1 instruction before previous– 10 back 2 instructions before previous– 11 back 3 instructions before previous

Page 16: Introduction to Assembly

16CMSC 313, F ‘09

Tomorrow’s Program

00110011 Eat soft shell crab. Eat soft shell crab.

1010 1010 Study history with eyes closed.Study history with eyes closed.

0100 0100 Surf the web.Surf the web.

1001 1001 Cram for Comp. Org.Cram for Comp. Org.

0101 0101 Play DoomPlay Doom

1110 1110 Go back 2 before previousGo back 2 before previous

Page 17: Introduction to Assembly

17CMSC 313, F ‘09

Von Neumann Computer

• Somehow put sequence of instructions (as well Somehow put sequence of instructions (as well as the data) in memoryas the data) in memory

• A control unit sequences through the memory A control unit sequences through the memory (instructions) one at a time.(instructions) one at a time.

• It is also possible to put other stuff (data) in the It is also possible to put other stuff (data) in the memory.memory.

• See the See the von Neumann architecturevon Neumann architecture entry in entry in Wikipedia for moreWikipedia for more

Page 18: Introduction to Assembly

18CMSC 313, F ‘09

Hardware

• Need memory to hold the sequence of Need memory to hold the sequence of instructions.instructions.

• Something to sequence through theSomething to sequence through the• instructions.instructions.• Something to execute one instruction.Something to execute one instruction.• Something to interface to the outside world (I/O).Something to interface to the outside world (I/O).

Page 19: Introduction to Assembly

19CMSC 313, F ‘09

Typical System Organization

Mainmemory

I/O bridge

Bus interface

ALU

Register file

CPU

System bus Memory bus

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Display

Disk

I/O bus Expansion slots forother devices suchas network adapters

hello executable stored on disk

PC

Page 20: Introduction to Assembly

20CMSC 313, F ‘09

Mainmemory

I/O bridge

Bus interface

ALU

Register file

CPU

System bus Memory bus

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Display

Disk

I/O bus Expansion slots forother devices suchas network adapters

PC

"hello"

Usertypes

"hello"

Reading hello command

hello executable stored on disk

Page 21: Introduction to Assembly

21CMSC 313, F ‘09

Mainmemory

I/O bridge

Bus interface

ALU

Register file

CPU

System bus Memory bus

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Display

Disk

I/O bus Expansion slots forother devices suchas network adapters

hello executable stored on disk

PC

hello code

"hello,world\n"

Loading hello executable

Page 22: Introduction to Assembly

22CMSC 313, F ‘09

Mainmemory

I/O bridge

Bus interface

ALU

Register file

CPU

System bus Memory bus

Disk controller

Graphicsadapter

USBcontroller

Mouse Keyboard Display

Disk

I/O bus Expansion slots forother devices suchas network adapters

hello executable stored on disk

PC

hello code

"hello,world\n"

"hello,world\n"

Displaying the output

Page 23: Introduction to Assembly

23CMSC 313, F ‘09

Instructions

• We will spend lots of time on real instruction sets We will spend lots of time on real instruction sets (IA32 - Intel Architecture for 32 bit machines).(IA32 - Intel Architecture for 32 bit machines).

• Data representation is determined by processor Data representation is determined by processor architecture.architecture.

• For now – we need to understand that each piece For now – we need to understand that each piece of data is just a bunch of bits (data looks just like of data is just a bunch of bits (data looks just like instructions).instructions).

Page 24: Introduction to Assembly

24CMSC 313, F ‘09

Interpreting Data

• It’s impossible to tell what a chunk of bits means without It’s impossible to tell what a chunk of bits means without any other informationany other information

0100100001001000– Could be the integer 72

– Could be an instruction.

– Could be the character ‘H’

– Could be a floating point number.

Page 25: Introduction to Assembly

25CMSC 313, F ‘09

Context Determines Data Type

• A computer executes instructions sequentially – A computer executes instructions sequentially – it just grabs the bits in the next memory location it just grabs the bits in the next memory location and assumes it’s an instruction.and assumes it’s an instruction.

• Some instructions add integers – in this case the Some instructions add integers – in this case the computer assumes the bits in memory represent computer assumes the bits in memory represent integer numbers.integer numbers.

Page 26: Introduction to Assembly

26CMSC 313, F ‘09

Memory Cache

Mainmemory(DRAM)

Memorybridge

Bus interfaceL2 cache(SRAM)

ALU

Register file

CPU chip

Cache bus System bus Memory bus

L1 cache

(SRAM)

Page 27: Introduction to Assembly

27CMSC 313, F ‘09

Memory Hierarchy

Registers

On-chip L1cache (SRAM)

Main memory(DRAM)

Local secondary storage(local disks)

Larger, slower,

and cheaper (per byte)storagedevices

Remote secondary storage(distributed file systems, Web servers)

Local disks hold files retrieved from disks on remote network servers.

Main memory holds disk blocks retrieved from local disks.

Off-chip L2cache (SRAM)

L1 cache holds cache lines retrieved from the L2 cache.

CPU registers hold words retrieved from cache memory.

L2 cache holds cache lines retrieved from memory.

L0:

L1:

L2:

L3:

L4:

L5:

Smaller,faster,and

costlier(per byte)storage devices

Page 28: Introduction to Assembly

28CMSC 313, F ‘09

Operating System

• One of the functions of the operating system (which One of the functions of the operating system (which is itself a running program) is to move programs in is itself a running program) is to move programs in to memory and start them running.to memory and start them running.

• The operating system must manage the memory of The operating system must manage the memory of the computer (the OS itself must be in memory!).the computer (the OS itself must be in memory!).

Page 29: Introduction to Assembly

29CMSC 313, F ‘09

Machine Code

• In the old days… humans would write programs In the old days… humans would write programs in machine code (determine every 1 and 0).in machine code (determine every 1 and 0).

• The 1s and 0s were put in to memory with a set of The 1s and 0s were put in to memory with a set of switches!switches!

Page 30: Introduction to Assembly

30CMSC 313, F ‘09

Assembly Language

• An assembly language program is a text file An assembly language program is a text file containing symbol descriptions of instructionscontaining symbol descriptions of instructions

• An assembler is a program that reads the An assembler is a program that reads the assembly language program and outputs assembly language program and outputs machine code (1s and 0s)machine code (1s and 0s)

Page 31: Introduction to Assembly

31CMSC 313, F ‘09

High Level Languages

• Many high-level languages translate program text Many high-level languages translate program text to assembly language (and then run through an to assembly language (and then run through an assembler).assembler).

• C compilers often do this, we will test this out by C compilers often do this, we will test this out by generating assembly language from C code.generating assembly language from C code.