Introduction to Assembly Programming Natawut Nupairoj Assembly Language
Introduction to Assembly Programming Natawut Nupairoj Assembly Language 1
Outline • • What is assembly? How does it look like? Type of instructions. Assembler and other tools. Natawut Nupairoj Assembly Language 2
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 3
Level of Languages swap(int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } • High level: C / Java / Pascal • Low level: Assembly / Bytecode • Machine Language C Compiler swap: muli $2, $5, 4 add $2, $4, $2 lw $15, 0($2). . . Natawut Nupairoj Assembler 000010001101101100110000. . . Assembly Language 4
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: – 3 D graphic library • When there is no compiler ! Natawut Nupairoj Assembly Language 5
When to Use Assembly • When you want to understand internal architecture of a CPU ! – Complex Instruction Set Computers (CISC) • Intel x 86, 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 6
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 7
Inside Computer Natawut Nupairoj Assembly Language 8
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 9
Example: adding two numbers • Sparc: r 2 = r 0 + r 1 add %r 0, %r 1, %r 2 • MIPS: s 2 = s 0 + s 1 add $s 2, $s 0, $s 1 • IBM 370: R 1 = R 1 + R 2 AR R 1, R 2 Natawut Nupairoj Assembly Language 10
Instruction Formats (Cont(’ • Limited number of operands per instruction: r 5 = r 1 + 8 - r 2 + r 3 add %r 1, 8, %r 1 sub %r 1, %r 2, %r 1 add %r 1, %r 3, %r 5 Natawut Nupairoj ! r 1 = r 1 + 8 ! r 1 = r 1 - r 2 ! r 5 = r 1 + r 3 Assembly Language 11
Translation Process • Assembler: – translate assembly to a binary code. – check syntax. – produce an object file (not executable). • Linker: – combine or more object files. – resolve references to other object files / libraries. – produce an executable program. Natawut Nupairoj Assembly Language 12
Translation Process (Cont(’ Assembly Program Object File Assembler Object File Natawut Nupairoj Assembly Language Libraries Linker Executable File 13
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. Natawut Nupairoj Assembly Language 14
- Slides: 14