SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Assembly Language Basic Concepts Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
Introduction ,[object Object],[object Object],[object Object],Motaz K. Saad, Dept. of CS
Computer Design Motaz K. Saad, Dept. of CS Programmer’s View Logic Designer’s View Instruction Set Design Machine Organization Machine Language Machine Implementations Computer Interface Hardware Components Compiler/System View Logic Designer’s View
Computer Architecture Design I/O system Instr. Set Proc. Compiler Operating System Application Digital Design Instruction Set Architecture Datapath & Control  Software Hardware Circuit Design Motaz K. Saad, Dept. of CS
Architectures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Motaz K. Saad, Dept. of CS
Below Your Program High-level language program in C void swap(int v[ ], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Motaz K. Saad, Dept. of CS swap: muli $2, $5, 4 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 Compiler Assembly language Program (for MIPS) 00000000101000010000000000011000 00000000100011100001100000100001 10001100011000100000000000000000 10001100111100100000000000000100 10101100111100100000000000000000 10101100011000100000000000000100 00000011111000000000000000001000 Assembler Binary machine language program (for MIPS)
Try it out ! ,[object Object],[object Object],[object Object],[object Object],[object Object],gcc –S file.c Compile only; do not assemble or link Motaz K. Saad, Dept. of CS
gcc – S  hello.c ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Motaz K. Saad, Dept. of CS
Trying it out on different architectures! #include   <stdio.h> void  swap ( int   v [ ],  int   k ); void   print_vector ( int   v [ ]); int  main( int  argc ,  char  * argv [ ]) { int  v []={1,3,5,7,9,-1}; print_vector(v); swap(v,2); print_vector(v); } void   swap ( int   v [],  int   k ) { int   temp ; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } void   print_vector ( int   v []) { int   i ; for (i=0 ; v[i]>0 ; i++) printf(&quot; %d  &quot;,v[i]); printf(&quot;  &quot;); } Motaz K. Saad, Dept. of CS
swap: # vars= 8, regs= 2/0, args= 0, extra= 8 .frame $fp,24,$31 .mask 0x50000000,-4 .fmask 0x00000000,0 .set noreorder .cpload $25 .set reorder subu $sp,$sp,24 .cprestore 0 sw $fp,20($sp) sw $28,16($sp) move $fp,$sp sw $4,24($fp) sw $5,28($fp) lw $2,28($fp) move $3,$2 sll $2,$3,2 lw $3,24($fp) addu $2,$2,$3 lw $3,0($2) sw $3,8($fp) lw $2,28($fp) move $3,$2 sll $2,$3,2 lw $3,24($fp) addu $2,$2,$3 lw $3,28($fp) move $4,$3 sll $3,$4,2 lw $4,24($fp) addu $3,$3,$4 addu $4,$3,4 # page 1 $ gcc -S swap.c [on a MIPS R12K machine] lw $3,0($4) sw $3,0($2) lw $2,28($fp) move $3,$2 sll $2,$3,2 lw $3,24($fp) addu $2,$2,$3 addu $3,$2,4 lw $2,8($fp) sw $2,0($3) # page 2 Motaz K. Saad, Dept. of CS
swap: # vars= 0, regs= 0/0, args= 0, extra= 0 .frame $sp,0,$31 .mask 0x00000000,0 .fmask 0x00000000,0 .set noreorder .cpload $25 .set reorder sll $5,$5,2 addu $5,$5,$4 lw $2,4($5) lw $3,0($5) sw $2,0($5) .set noreorder .set nomacro j $31 sw $3,4($5) .set macro .set reorder .end swap .rdata .align 2 $ gcc -O3 -S swap.c [on a MIPS R12K machine] Motaz K. Saad, Dept. of CS
swap: pushl %ebp movl %esp, %ebp subl $4, %esp movl 12(%ebp), %eax imull $4, %eax, %edx movl 8(%ebp), %eax movl (%eax,%edx), %eax movl %eax, -4(%ebp) movl 12(%ebp), %eax imull $4, %eax, %ecx movl 8(%ebp), %edx movl 12(%ebp), %eax imull $4, %eax, %eax addl 8(%ebp), %eax addl $4, %eax movl (%eax), %eax movl %eax, (%edx,%ecx) movl 12(%ebp), %eax imull $4, %eax, %eax addl 8(%ebp), %eax leal 4(%eax), %edx movl -4(%ebp), %eax movl %eax, (%edx) leave ret $ gcc -S swap.c [on a Pentium III machine] swap: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx pushl %ebx movl 12(%ebp), %eax movl (%edx,%eax,4), %ebx movl 4(%edx,%eax,4), %ecx movl %ecx, (%edx,%eax,4) movl %ebx, 4(%edx,%eax,4) movl (%esp), %ebx leave ret $ gcc -O3 -S swap.c [on a Pentium III machine] Motaz K. Saad, Dept. of CS
swap: .prologue 2, 2 .vframe r2 mov r2 = r12  ;; .body st8 [r2] = r32 mov r14 = r2  ;; adds r14 = 8, r2  ;; st4 [r14] = r33 mov r14 = r2 adds r16 = 12, r2  ;; mov r14 = r2  ;; adds r14 = 8, r2  ;; ld4 r14 = [r14]  ;; sxt4 r15 = r14 addl r14 = 4, r0  ;; setf.sig f6 = r15 setf.sig f7 = r14  ;; xma.l f6 = f6, f7, f0  ;; getf.sig r15 = f6 ld8 r14 = [r2]  ;; add r14 = r15, r14  ;; ld4 r14 = [r14]  ;; st4 [r16] = r14 mov r14 = r2  ;; adds r14 = 8, r2  ;; ld4 r14 = [r14]  ;; sxt4 r15 = r14 addl r14 = 4, r0  ;; setf.sig f6 = r15 setf.sig f7 = r14  ;; xma.l f6 = f6, f7, f0  ;; getf.sig r15 = f6 ld8 r14 = [r2]  ;; add r16 = r15, r14 mov r14 = r2  ;; #page 1 adds r14 = 8, r2  ;; $ gcc -S swap.c [on an Itanium I machine] ld4 r14 = [r14]  ;; sxt4 r15 = r14 addl r14 = 4, r0  ;; setf.sig f6 = r15 setf.sig f7 = r14  ;; xma.l f6 = f6, f7, f0  ;; getf.sig r15 = f6 ld8 r14 = [r2]  ;; add r14 = r15, r14  ;; adds r14 = 4, r14  ;; ld4 r14 = [r14]  ;; st4 [r16] = r14 mov r14 = r2  ;; adds r14 = 8, r2  ;; ld4 r14 = [r14]  ;; sxt4 r15 = r14 addl r14 = 4, r0  ;; setf.sig f6 = r15 setf.sig f7 = r14  ;; xma.l f6 = f6, f7, f0  ;; getf.sig r15 = f6 ld8 r14 = [r2]  ;; add r14 = r15, r14  ;; adds r15 = 4, r14 mov r14 = r2  ;; adds r14 = 12, r2  ;; ld4 r14 = [r14]  ;; st4 [r15] = r14 .restore sp mov r12 = r2 br.ret.sptk.many b0 .endp swap# .section .rodata .align 8 # page 2 Motaz K. Saad, Dept. of CS
Converting Source into Executable Files  Motaz K. Saad, Dept. of CS
A More Complete Story Source file Motaz K. Saad, Dept. of CS Compiler Assembler file Object file Assembler Linker Source file Compiler Assembler file Object file Assembler Source file Compiler Assembler file Object file Assembler Program library Program library Executable file
Converting Source into Executable Files  Motaz K. Saad, Dept. of CS
The Linker  Motaz K. Saad, Dept. of CS
Anatomy of an Object File Motaz K. Saad, Dept. of CS Size and position of other pieces. Machine Code Binary Data Representation. References that must change if the program is moved  in memory. Associate addresses with external label. Unresolved references. Compilation information to allow mapping of addresses to source code.
When to use Assembly Language? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Motaz K. Saad, Dept. of CS
Comparing Assembly Language to High-Level Languages Motaz K. Saad, Dept. of CS
Specific Machine Levels Motaz K. Saad, Dept. of CS

Weitere ähnliche Inhalte

Was ist angesagt?

Dma transfer
Dma transferDma transfer
Dma transfer
gmnithya
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
Anuj Modi
 
Microprogram Control
Microprogram Control Microprogram Control
Microprogram Control
Anuj Modi
 
Cache coherence
Cache coherenceCache coherence
Cache coherence
Employee
 

Was ist angesagt? (20)

Computer architecture instruction formats
Computer architecture instruction formatsComputer architecture instruction formats
Computer architecture instruction formats
 
DMA and DMA controller
DMA and DMA controllerDMA and DMA controller
DMA and DMA controller
 
ADDRESSING MODES
ADDRESSING MODESADDRESSING MODES
ADDRESSING MODES
 
Addressing modes
Addressing modesAddressing modes
Addressing modes
 
Assembly and Machine Code
Assembly and Machine CodeAssembly and Machine Code
Assembly and Machine Code
 
Computer architecture register transfer languages rtl
Computer architecture register transfer languages rtlComputer architecture register transfer languages rtl
Computer architecture register transfer languages rtl
 
Dma transfer
Dma transferDma transfer
Dma transfer
 
DMA operation
DMA operationDMA operation
DMA operation
 
Addressing Modes
Addressing ModesAddressing Modes
Addressing Modes
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Clock-8086 bus cycle
Clock-8086 bus cycleClock-8086 bus cycle
Clock-8086 bus cycle
 
Stack Operations
Stack Operations Stack Operations
Stack Operations
 
Microprogram Control
Microprogram Control Microprogram Control
Microprogram Control
 
Addressing modes ppt
Addressing modes pptAddressing modes ppt
Addressing modes ppt
 
Assembly 8086
Assembly 8086Assembly 8086
Assembly 8086
 
Assembly Language String Chapter
Assembly Language String Chapter Assembly Language String Chapter
Assembly Language String Chapter
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Cache coherence
Cache coherenceCache coherence
Cache coherence
 
Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 

Andere mochten auch

Assembly language programming
Assembly language programmingAssembly language programming
Assembly language programming
himhk
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
Mohd Arif
 
Assembler design options
Assembler design optionsAssembler design options
Assembler design options
Mohd Arif
 

Andere mochten auch (20)

Intro to assembly language
Intro to assembly languageIntro to assembly language
Intro to assembly language
 
Assembly Language Lecture 5
Assembly Language Lecture 5Assembly Language Lecture 5
Assembly Language Lecture 5
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
 
Assembly
AssemblyAssembly
Assembly
 
Assembly language programming
Assembly language programmingAssembly language programming
Assembly language programming
 
Assembly
AssemblyAssembly
Assembly
 
.NET Framework Projet with C#
.NET Framework Projet with C#.NET Framework Projet with C#
.NET Framework Projet with C#
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
 
Assembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI HiroakiAssembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI Hiroaki
 
Assembly language part I
Assembly language part IAssembly language part I
Assembly language part I
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
 
Assembler design options
Assembler design optionsAssembler design options
Assembler design options
 
Assembly Language -I
Assembly Language -IAssembly Language -I
Assembly Language -I
 
Chapt 01 basic concepts
Chapt 01   basic conceptsChapt 01   basic concepts
Chapt 01 basic concepts
 
Processor Basics
Processor BasicsProcessor Basics
Processor Basics
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Lec 01 basic concepts
Lec 01 basic conceptsLec 01 basic concepts
Lec 01 basic concepts
 
[ASM] Lab1
[ASM] Lab1[ASM] Lab1
[ASM] Lab1
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
 

Ähnlich wie Introduction to Assembly Language

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 

Ähnlich wie Introduction to Assembly Language (20)

Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout Introduction to source{d} Engine and source{d} Lookout
Introduction to source{d} Engine and source{d} Lookout
 
CompilersAndLibraries
CompilersAndLibrariesCompilersAndLibraries
CompilersAndLibraries
 
Going crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHPGoing crazy with Node.JS and CakePHP
Going crazy with Node.JS and CakePHP
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
WebAssembly. Neither Web Nor Assembly, All Revolutionary
WebAssembly. Neither Web Nor Assembly, All RevolutionaryWebAssembly. Neither Web Nor Assembly, All Revolutionary
WebAssembly. Neither Web Nor Assembly, All Revolutionary
 
String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?String Comparison Surprises: Did Postgres lose my data?
String Comparison Surprises: Did Postgres lose my data?
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
 
C Under Linux
C Under LinuxC Under Linux
C Under Linux
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
 
MLflow with R
MLflow with RMLflow with R
MLflow with R
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
Lca05
Lca05Lca05
Lca05
 
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
 
GDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしようGDG DevFest Kyoto 2014 これからのGoの話をしよう
GDG DevFest Kyoto 2014 これからのGoの話をしよう
 
Don't Be Afraid of Abstract Syntax Trees
Don't Be Afraid of Abstract Syntax TreesDon't Be Afraid of Abstract Syntax Trees
Don't Be Afraid of Abstract Syntax Trees
 
The true story_of_hello_world
The true story_of_hello_worldThe true story_of_hello_world
The true story_of_hello_world
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날루비가 얼랭에 빠진 날
루비가 얼랭에 빠진 날
 

Mehr von Motaz Saad

Open Source Business Models
Open Source Business ModelsOpen Source Business Models
Open Source Business Models
Motaz Saad
 
OS Lab: Introduction to Linux
OS Lab: Introduction to LinuxOS Lab: Introduction to Linux
OS Lab: Introduction to Linux
Motaz Saad
 

Mehr von Motaz Saad (15)

Cross Language Concept Mining
Cross Language Concept Mining Cross Language Concept Mining
Cross Language Concept Mining
 
Hewahi, saad 2006 - class outliers mining distance-based approach
Hewahi, saad   2006 - class outliers mining distance-based approachHewahi, saad   2006 - class outliers mining distance-based approach
Hewahi, saad 2006 - class outliers mining distance-based approach
 
Structured Vs, Object Oriented Analysis and Design
Structured Vs, Object Oriented Analysis and DesignStructured Vs, Object Oriented Analysis and Design
Structured Vs, Object Oriented Analysis and Design
 
Open Source Business Models
Open Source Business ModelsOpen Source Business Models
Open Source Business Models
 
OS Lab: Introduction to Linux
OS Lab: Introduction to LinuxOS Lab: Introduction to Linux
OS Lab: Introduction to Linux
 
Introduction to CLIPS Expert System
Introduction to CLIPS Expert SystemIntroduction to CLIPS Expert System
Introduction to CLIPS Expert System
 
The x86 Family
The x86 FamilyThe x86 Family
The x86 Family
 
Intel 64bit Architecture
Intel 64bit ArchitectureIntel 64bit Architecture
Intel 64bit Architecture
 
Assembly Language Lecture 3
Assembly Language Lecture 3Assembly Language Lecture 3
Assembly Language Lecture 3
 
Assembly Language Lecture 2
Assembly Language Lecture 2Assembly Language Lecture 2
Assembly Language Lecture 2
 
Class Outlier Mining
Class Outlier MiningClass Outlier Mining
Class Outlier Mining
 
Browsing The Source Code of Linux Packages
Browsing The Source Code of Linux PackagesBrowsing The Source Code of Linux Packages
Browsing The Source Code of Linux Packages
 
مقدمة في تكنواوجيا المعلومات
مقدمة في تكنواوجيا المعلوماتمقدمة في تكنواوجيا المعلومات
مقدمة في تكنواوجيا المعلومات
 
Data Mining and Business Intelligence Tools
Data Mining and Business Intelligence ToolsData Mining and Business Intelligence Tools
Data Mining and Business Intelligence Tools
 
Browsing Linux Kernel Source
Browsing Linux Kernel SourceBrowsing Linux Kernel Source
Browsing Linux Kernel Source
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

Introduction to Assembly Language

  • 1. Assembly Language Basic Concepts Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
  • 2.
  • 3. Computer Design Motaz K. Saad, Dept. of CS Programmer’s View Logic Designer’s View Instruction Set Design Machine Organization Machine Language Machine Implementations Computer Interface Hardware Components Compiler/System View Logic Designer’s View
  • 4. Computer Architecture Design I/O system Instr. Set Proc. Compiler Operating System Application Digital Design Instruction Set Architecture Datapath & Control Software Hardware Circuit Design Motaz K. Saad, Dept. of CS
  • 5.
  • 6. Below Your Program High-level language program in C void swap(int v[ ], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Motaz K. Saad, Dept. of CS swap: muli $2, $5, 4 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 Compiler Assembly language Program (for MIPS) 00000000101000010000000000011000 00000000100011100001100000100001 10001100011000100000000000000000 10001100111100100000000000000100 10101100111100100000000000000000 10101100011000100000000000000100 00000011111000000000000000001000 Assembler Binary machine language program (for MIPS)
  • 7.
  • 8.
  • 9. Trying it out on different architectures! #include <stdio.h> void swap ( int v [ ], int k ); void print_vector ( int v [ ]); int main( int argc , char * argv [ ]) { int v []={1,3,5,7,9,-1}; print_vector(v); swap(v,2); print_vector(v); } void swap ( int v [], int k ) { int temp ; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } void print_vector ( int v []) { int i ; for (i=0 ; v[i]>0 ; i++) printf(&quot; %d &quot;,v[i]); printf(&quot; &quot;); } Motaz K. Saad, Dept. of CS
  • 10. swap: # vars= 8, regs= 2/0, args= 0, extra= 8 .frame $fp,24,$31 .mask 0x50000000,-4 .fmask 0x00000000,0 .set noreorder .cpload $25 .set reorder subu $sp,$sp,24 .cprestore 0 sw $fp,20($sp) sw $28,16($sp) move $fp,$sp sw $4,24($fp) sw $5,28($fp) lw $2,28($fp) move $3,$2 sll $2,$3,2 lw $3,24($fp) addu $2,$2,$3 lw $3,0($2) sw $3,8($fp) lw $2,28($fp) move $3,$2 sll $2,$3,2 lw $3,24($fp) addu $2,$2,$3 lw $3,28($fp) move $4,$3 sll $3,$4,2 lw $4,24($fp) addu $3,$3,$4 addu $4,$3,4 # page 1 $ gcc -S swap.c [on a MIPS R12K machine] lw $3,0($4) sw $3,0($2) lw $2,28($fp) move $3,$2 sll $2,$3,2 lw $3,24($fp) addu $2,$2,$3 addu $3,$2,4 lw $2,8($fp) sw $2,0($3) # page 2 Motaz K. Saad, Dept. of CS
  • 11. swap: # vars= 0, regs= 0/0, args= 0, extra= 0 .frame $sp,0,$31 .mask 0x00000000,0 .fmask 0x00000000,0 .set noreorder .cpload $25 .set reorder sll $5,$5,2 addu $5,$5,$4 lw $2,4($5) lw $3,0($5) sw $2,0($5) .set noreorder .set nomacro j $31 sw $3,4($5) .set macro .set reorder .end swap .rdata .align 2 $ gcc -O3 -S swap.c [on a MIPS R12K machine] Motaz K. Saad, Dept. of CS
  • 12. swap: pushl %ebp movl %esp, %ebp subl $4, %esp movl 12(%ebp), %eax imull $4, %eax, %edx movl 8(%ebp), %eax movl (%eax,%edx), %eax movl %eax, -4(%ebp) movl 12(%ebp), %eax imull $4, %eax, %ecx movl 8(%ebp), %edx movl 12(%ebp), %eax imull $4, %eax, %eax addl 8(%ebp), %eax addl $4, %eax movl (%eax), %eax movl %eax, (%edx,%ecx) movl 12(%ebp), %eax imull $4, %eax, %eax addl 8(%ebp), %eax leal 4(%eax), %edx movl -4(%ebp), %eax movl %eax, (%edx) leave ret $ gcc -S swap.c [on a Pentium III machine] swap: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx pushl %ebx movl 12(%ebp), %eax movl (%edx,%eax,4), %ebx movl 4(%edx,%eax,4), %ecx movl %ecx, (%edx,%eax,4) movl %ebx, 4(%edx,%eax,4) movl (%esp), %ebx leave ret $ gcc -O3 -S swap.c [on a Pentium III machine] Motaz K. Saad, Dept. of CS
  • 13. swap: .prologue 2, 2 .vframe r2 mov r2 = r12 ;; .body st8 [r2] = r32 mov r14 = r2 ;; adds r14 = 8, r2 ;; st4 [r14] = r33 mov r14 = r2 adds r16 = 12, r2 ;; mov r14 = r2 ;; adds r14 = 8, r2 ;; ld4 r14 = [r14] ;; sxt4 r15 = r14 addl r14 = 4, r0 ;; setf.sig f6 = r15 setf.sig f7 = r14 ;; xma.l f6 = f6, f7, f0 ;; getf.sig r15 = f6 ld8 r14 = [r2] ;; add r14 = r15, r14 ;; ld4 r14 = [r14] ;; st4 [r16] = r14 mov r14 = r2 ;; adds r14 = 8, r2 ;; ld4 r14 = [r14] ;; sxt4 r15 = r14 addl r14 = 4, r0 ;; setf.sig f6 = r15 setf.sig f7 = r14 ;; xma.l f6 = f6, f7, f0 ;; getf.sig r15 = f6 ld8 r14 = [r2] ;; add r16 = r15, r14 mov r14 = r2 ;; #page 1 adds r14 = 8, r2 ;; $ gcc -S swap.c [on an Itanium I machine] ld4 r14 = [r14] ;; sxt4 r15 = r14 addl r14 = 4, r0 ;; setf.sig f6 = r15 setf.sig f7 = r14 ;; xma.l f6 = f6, f7, f0 ;; getf.sig r15 = f6 ld8 r14 = [r2] ;; add r14 = r15, r14 ;; adds r14 = 4, r14 ;; ld4 r14 = [r14] ;; st4 [r16] = r14 mov r14 = r2 ;; adds r14 = 8, r2 ;; ld4 r14 = [r14] ;; sxt4 r15 = r14 addl r14 = 4, r0 ;; setf.sig f6 = r15 setf.sig f7 = r14 ;; xma.l f6 = f6, f7, f0 ;; getf.sig r15 = f6 ld8 r14 = [r2] ;; add r14 = r15, r14 ;; adds r15 = 4, r14 mov r14 = r2 ;; adds r14 = 12, r2 ;; ld4 r14 = [r14] ;; st4 [r15] = r14 .restore sp mov r12 = r2 br.ret.sptk.many b0 .endp swap# .section .rodata .align 8 # page 2 Motaz K. Saad, Dept. of CS
  • 14. Converting Source into Executable Files Motaz K. Saad, Dept. of CS
  • 15. A More Complete Story Source file Motaz K. Saad, Dept. of CS Compiler Assembler file Object file Assembler Linker Source file Compiler Assembler file Object file Assembler Source file Compiler Assembler file Object file Assembler Program library Program library Executable file
  • 16. Converting Source into Executable Files Motaz K. Saad, Dept. of CS
  • 17. The Linker Motaz K. Saad, Dept. of CS
  • 18. Anatomy of an Object File Motaz K. Saad, Dept. of CS Size and position of other pieces. Machine Code Binary Data Representation. References that must change if the program is moved in memory. Associate addresses with external label. Unresolved references. Compilation information to allow mapping of addresses to source code.
  • 19.
  • 20. Comparing Assembly Language to High-Level Languages Motaz K. Saad, Dept. of CS
  • 21. Specific Machine Levels Motaz K. Saad, Dept. of CS