introduction to assembly
Post on 05-Jan-2016
20 Views
Preview:
DESCRIPTION
TRANSCRIPT
1CMSC 313, F ‘09
Introduction to Assembly
• TopicsTopics– Assembly Overview– Instructions– Hardware
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!
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.
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;
}}
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.
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.
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
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.
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
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
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
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?
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
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
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
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
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
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).
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
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
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
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
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).
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.
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.
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)
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
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!).
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!
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)
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.
top related