natawut nupairojassembly language1 introduction to assembly programming
TRANSCRIPT
Natawut Nupairoj Assembly Language 1
Introduction to Assembly Programming
Natawut Nupairoj Assembly Language 2
Outline
• What is assembly ?• How does it look like ?• Type of instructions.• Assembler and other tools.
Natawut Nupairoj Assembly Language 3
What is Assembly ?
• Symbolic representation of machine language.– opcodes– operands– labels
• More readable to human (not computer).
add A, B 1000111000010110
• Easy to translate to machine language.
Natawut Nupairoj Assembly Language 4
Level of Languages
swap(int v[], int k)
{ int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
swap:
muli $2, $5, 4
add $2, $4, $2
lw $15, 0($2)
...
000010001101101100110000
000010001101101100110000
000010001101101100110000
000010001101101100110000
...
C Compiler
Assembler
•High level: C / Java / Pascal•Low level: Assembly / Bytecode•Machine Language
Natawut Nupairoj Assembly Language 5
When to Use Assembly
• When speed and size matter !– Equipment that must response very quickly.– Embedded devices.– Device driver.– When the resource is limited.
• When we use the specialized instructions:– 3D graphic library
• When there is no compiler !
Natawut Nupairoj Assembly Language 6
When to Use Assembly
• When you want to understand internal architecture of a CPU !– Complex Instruction Set Computers (CISC)
• Intel x86, Intel Pentium, etc.
– Reduce Instruction Set Computers (RISC)• DEC Alpha, Sun SPARC, HP P/A, MIPS, Pentium II/III/4,
etc.
– Very-Large Instruction Word (VLIW)• Intel Itanium (Pentium 4), Transmeta Crusoe.
– Pentium II/III/4 are special cases• Outside CICS, inside RISC.
Natawut Nupairoj Assembly Language 7
Drawbacks of Assembly
• Machine-dependent:– must be rewritten on another computer architecture.– not portable.
• Longer codes to write.• Difficult to read and understand.
Natawut Nupairoj Assembly Language 8
Inside Computer
MEMORY
CPU
ALU
R egister
I nput
O utput
Natawut Nupairoj Assembly Language 9
Instruction Formats
• Different CPUs, different formats.• Something in common:
– opcode: instruction• What is the command ?• Arithmetic• Branch
– operand: perform that command on ?• What is the data ?• registers• memory• constant
Natawut Nupairoj Assembly Language 10
Example: adding two numbers
• Sparc: r2 = r0 + r1add %r0, %r1, %r2
• MIPS: s2 = s0 + s1add $s2, $s0, $s1
• IBM 370: R1 = R1 + R2AR R1, R2
Natawut Nupairoj Assembly Language 11
Instruction Formats (Cont’)
• Limited number of operands per instruction:
r5 = r1 + 8 - r2 + r3
add %r1, 8, %r1 ! r1 = r1 + 8sub %r1, %r2, %r1 ! r1 = r1 - r2add %r1, %r3, %r5 ! r5 = r1 + r3
Natawut Nupairoj Assembly Language 12
Translation Process
• Assembler:– translate assembly to a binary code.– check syntax.– produce an object file (not executable).
• Linker:– combine one or more object files.– resolve references to other object files / libraries.– produce an executable program.
Natawut Nupairoj Assembly Language 13
Translation Process (Cont’)
Assembly Program
Assembler
ObjectFile
ObjectFile
ObjectFile
Libraries
Linker
Executable File
Natawut Nupairoj Assembly Language 14
Other Tools
• Debugger:– trace assembly program.– run a program in a step-by-step fashion.– can display values of memory and registers.
• Profiler:– estimate time that a program spends in each
subroutine.– find the one with the longest time, optimize it.