Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 000010001101101100110000 000010001101101100110000 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: – 3D 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 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 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 CPU Input A L U MEMORY Output Register 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: r2 = r0 + r1 add %r0, %r1, %r2 • MIPS: s2 = s0 + s1 add $s2, $s0, $s1 • IBM 370: R1 = R1 + R2 AR R1, R2 Natawut Nupairoj Assembly Language 10 Instruction Formats (Cont’) • Limited number of operands per instruction: r5 = r1 + 8 - r2 + r3 add %r1, 8, %r1 sub %r1, %r2, %r1 add %r1, %r3, %r5 Natawut Nupairoj ! r1 = r1 + 8 ! r1 = r1 - r2 ! r5 = r1 + r3 Assembly Language 11 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 12 Translation Process (Cont’) Assembly Program Object File Assembler Object File 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