SlideShare a Scribd company logo
Assembly Language Basic Concepts Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
Introduction Computer Architecture includes the design of: Instruction Set Architecture  ( programmer's view) Machine Organization  ( logic designer’s view ). 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 x86 (including Intel IA-32) HP/Compaq Alpha AXP Sun SPARC Sun UltraSPARC Motorola 68000 PowerPC PowerPC64 ARM Hitachi SuperH IBM S/390 and zSeries MIPS HP PA-RISC Intel IA-64 AMD x86-64 H8/300 V850 and CRIS. 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 ! #include <stdio.h> int main() { printf(&quot;Hello World!\n&quot;); return 0; } gcc –S file.c Compile only; do not assemble or link Motaz K. Saad, Dept. of CS
gcc – S  hello.c .file &quot;cprog.c&quot; .section .rodata .LC0: .string &quot;Hello World!&quot; .text .globl main .type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $4, %esp movl $.LC0, (%esp) call puts movl $0, %eax addl $4, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret .size main, .-main .ident &quot;GCC: (GNU) 4.1.0 (SUSE Linux)&quot; .section .note.GNU-stack,&quot;&quot;,@progbits 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; \t%d  &quot;,v[i]); printf(&quot; \n &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? When you don’t have the tools to program in higher level: New embedded processors Compilers that check deadlines for real time system do not exist yet When the tools fail: Compilers still generate sub-optimal code When you are building the tools: Compiler designer/builders must know assembly well 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

More Related Content

What's hot (20)

PPTX
Signed Addition And Subtraction
Keyur Vadodariya
 
PPTX
CFG to CNF
Zain Ul Abiden
 
PPTX
Assembly Language and microprocessor
Khaled Sany
 
PDF
Solution manual of assembly language programming and organization of the ibm ...
Tayeen Ahmed
 
PPTX
Life cycle of a computer program
Abhay Kumar
 
PPTX
simple problem to convert NFA with epsilon to without epsilon
kanikkk
 
PDF
Assembly Langauge Chap 1
warda aziz
 
PDF
assembly language programming and organization of IBM PC" by YTHA YU
Education
 
PPTX
Java(Polymorphism)
harsh kothari
 
PPTX
Cpu scheduling in operating System.
Ravi Kumar Patel
 
PPT
Types of instructions
ihsanjamil
 
PDF
Chapter 6 Flow control Instructions
warda aziz
 
PPT
Stack & queue
Siddique Ibrahim
 
PPTX
Assignment problem branch and bound.pptx
KrishnaVardhan50
 
PPTX
Assembly language
Arafat Hossan
 
PPTX
Merge sort algorithm
Shubham Dwivedi
 
PDF
Assembly Language Programming By Ytha Yu, Charles Marut Chap 10 ( Arrays and ...
Bilal Amjad
 
PPTX
ADDRESSING MODES
Sadaf Rasheed
 
PDF
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
PPTX
8086 Instruction set
karthiga selvaraju
 
Signed Addition And Subtraction
Keyur Vadodariya
 
CFG to CNF
Zain Ul Abiden
 
Assembly Language and microprocessor
Khaled Sany
 
Solution manual of assembly language programming and organization of the ibm ...
Tayeen Ahmed
 
Life cycle of a computer program
Abhay Kumar
 
simple problem to convert NFA with epsilon to without epsilon
kanikkk
 
Assembly Langauge Chap 1
warda aziz
 
assembly language programming and organization of IBM PC" by YTHA YU
Education
 
Java(Polymorphism)
harsh kothari
 
Cpu scheduling in operating System.
Ravi Kumar Patel
 
Types of instructions
ihsanjamil
 
Chapter 6 Flow control Instructions
warda aziz
 
Stack & queue
Siddique Ibrahim
 
Assignment problem branch and bound.pptx
KrishnaVardhan50
 
Assembly language
Arafat Hossan
 
Merge sort algorithm
Shubham Dwivedi
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 10 ( Arrays and ...
Bilal Amjad
 
ADDRESSING MODES
Sadaf Rasheed
 
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
8086 Instruction set
karthiga selvaraju
 

Viewers also liked (20)

PPTX
Intro to assembly language
United International University
 
PPT
Assembly Language Lecture 5
Motaz Saad
 
PPT
Assembly Language Lecture 1
Motaz Saad
 
PPT
Assembly
manidangi
 
PPTX
Assembly language programming
himhk
 
PDF
Assembly
nick333y
 
PPT
.NET Framework Projet with C#
eclumson
 
PPTX
Assembly fundamentals
Syed Zaid Irshad
 
PDF
Assembly Language Tanka - SAKAI Hiroaki
asmtanka
 
PDF
Assembly language part I
Mohammed A. Imran
 
PPT
Assembler design option
Mohd Arif
 
PPT
Assembler design options
Mohd Arif
 
PPTX
Assembly Language -I
Devika Rangnekar
 
PPT
Chapt 01 basic concepts
bushrakainat214
 
PPTX
Processor Basics
Education Front
 
PPTX
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Bilal Amjad
 
PPT
Chapter 1
Ashhad Kamal
 
PPT
Lec 01 basic concepts
Abdul Khan
 
PPTX
[ASM] Lab1
Nora Youssef
 
PPT
Assembly Language Lecture 4
Motaz Saad
 
Intro to assembly language
United International University
 
Assembly Language Lecture 5
Motaz Saad
 
Assembly Language Lecture 1
Motaz Saad
 
Assembly
manidangi
 
Assembly language programming
himhk
 
Assembly
nick333y
 
.NET Framework Projet with C#
eclumson
 
Assembly fundamentals
Syed Zaid Irshad
 
Assembly Language Tanka - SAKAI Hiroaki
asmtanka
 
Assembly language part I
Mohammed A. Imran
 
Assembler design option
Mohd Arif
 
Assembler design options
Mohd Arif
 
Assembly Language -I
Devika Rangnekar
 
Chapt 01 basic concepts
bushrakainat214
 
Processor Basics
Education Front
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Bilal Amjad
 
Chapter 1
Ashhad Kamal
 
Lec 01 basic concepts
Abdul Khan
 
[ASM] Lab1
Nora Youssef
 
Assembly Language Lecture 4
Motaz Saad
 
Ad

Similar to Introduction to Assembly Language (20)

PDF
Introduction to source{d} Engine and source{d} Lookout
source{d}
 
ODP
CompilersAndLibraries
Staffan Tjernström
 
PPT
Going crazy with Node.JS and CakePHP
Mariano Iglesias
 
PDF
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
RootedCON
 
PDF
WebAssembly. Neither Web Nor Assembly, All Revolutionary
C4Media
 
PDF
String Comparison Surprises: Did Postgres lose my data?
Jeremy Schneider
 
ODP
How Xslate Works
Goro Fuji
 
ODP
C Under Linux
mohan43u
 
PPTX
C to perl binding
Shmuel Fomberg
 
PDF
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
PDF
MLflow with R
Databricks
 
PDF
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
Functional Thursday
 
PDF
Lca05
Sateesh Patil
 
PDF
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
Joel Porquet
 
PDF
GDG DevFest Kyoto 2014 これからのGoの話をしよう
Satoshi Noda
 
PPTX
Don't Be Afraid of Abstract Syntax Trees
Jamund Ferguson
 
PDF
The true story_of_hello_world
fantasy zheng
 
PDF
Refactoring to Macros with Clojure
Dmitry Buzdin
 
PDF
Presto anatomy
Dongmin Yu
 
PPT
루비가 얼랭에 빠진 날
Sukjoon Kim
 
Introduction to source{d} Engine and source{d} Lookout
source{d}
 
CompilersAndLibraries
Staffan Tjernström
 
Going crazy with Node.JS and CakePHP
Mariano Iglesias
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
RootedCON
 
WebAssembly. Neither Web Nor Assembly, All Revolutionary
C4Media
 
String Comparison Surprises: Did Postgres lose my data?
Jeremy Schneider
 
How Xslate Works
Goro Fuji
 
C Under Linux
mohan43u
 
C to perl binding
Shmuel Fomberg
 
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
MLflow with R
Databricks
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
Functional Thursday
 
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
Joel Porquet
 
GDG DevFest Kyoto 2014 これからのGoの話をしよう
Satoshi Noda
 
Don't Be Afraid of Abstract Syntax Trees
Jamund Ferguson
 
The true story_of_hello_world
fantasy zheng
 
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Presto anatomy
Dongmin Yu
 
루비가 얼랭에 빠진 날
Sukjoon Kim
 
Ad

More from Motaz Saad (15)

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

Recently uploaded (20)

PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Top Managed Service Providers in Los Angeles
Captain IT
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Français Patch Tuesday - Juillet
Ivanti
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 

Introduction to Assembly Language

  • 1. Assembly Language Basic Concepts Motaz K. Saad Spring 2007 Motaz K. Saad, Dept. of CS
  • 2. Introduction Computer Architecture includes the design of: Instruction Set Architecture ( programmer's view) Machine Organization ( logic designer’s view ). Motaz K. Saad, Dept. of CS
  • 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. Architectures x86 (including Intel IA-32) HP/Compaq Alpha AXP Sun SPARC Sun UltraSPARC Motorola 68000 PowerPC PowerPC64 ARM Hitachi SuperH IBM S/390 and zSeries MIPS HP PA-RISC Intel IA-64 AMD x86-64 H8/300 V850 and CRIS. Motaz K. Saad, Dept. of CS
  • 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. Try it out ! #include <stdio.h> int main() { printf(&quot;Hello World!\n&quot;); return 0; } gcc –S file.c Compile only; do not assemble or link Motaz K. Saad, Dept. of CS
  • 8. gcc – S hello.c .file &quot;cprog.c&quot; .section .rodata .LC0: .string &quot;Hello World!&quot; .text .globl main .type main, @function main: leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $4, %esp movl $.LC0, (%esp) call puts movl $0, %eax addl $4, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret .size main, .-main .ident &quot;GCC: (GNU) 4.1.0 (SUSE Linux)&quot; .section .note.GNU-stack,&quot;&quot;,@progbits Motaz K. Saad, Dept. of CS
  • 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; \t%d &quot;,v[i]); printf(&quot; \n &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. When to use Assembly Language? When you don’t have the tools to program in higher level: New embedded processors Compilers that check deadlines for real time system do not exist yet When the tools fail: Compilers still generate sub-optimal code When you are building the tools: Compiler designer/builders must know assembly well Motaz K. Saad, Dept. of CS
  • 20. Comparing Assembly Language to High-Level Languages Motaz K. Saad, Dept. of CS
  • 21. Specific Machine Levels Motaz K. Saad, Dept. of CS