1 GCC Inline Assembly (asm) Example 1 : assignment System oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/spSession 14 – 26 May 2015 Ref: B. Hirsbrunner:

Slides:



Advertisements
Similar presentations
CPU Structure and Function
Advertisements

Flow of Control Instruction/Control structure Looping structure Looping structure Branching structure Branching structure For assembly language program.
Lecture 5: MIPS Instruction Set
Computer Organization and Architecture
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Introduction to X86 assembly by Istvan Haller
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
Flow Control Instructions
Assembler When a source program is a assembly language and the target program is a numerical machine language then the translator is called as a assembler.
INSTRUCTION SET OF MICROPROCESSOR 8085
Computer Science 210 Computer Organization The Instruction Execution Cycle.
High-Level Language Interface Chapter 17 S. Dandamudi.
Ithaca College 1 Machine-Level Programming X: inline Assembly Comp 21000: Introduction to Computer Systems & Assembly Lang On-Line resources* * See see.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
5. Assembly Language. Basics of AL Program data Pseudo-ops Array Program structures Data, stack, code segments.
1 ICS 51 Introductory Computer Organization Fall 2009.
Other Processors. Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
80C51 Block Diagram 1. 80C51 Memory Memory The data width is 8 bits Registers are 8 bits Addresses are 8 bits – i.e. addresses for only 256.
Seminar On 8085 microprocessor
Assembly language programming
Computer Architecture CST 250
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Microprocessor and Assembly Language
Chapter 13 Inline Code.
ELE2MIC Friday Lecture Venue Change
Other Processors.
ECE 382 Lesson 4 Lesson Outline Readings
Chapter 13 Inline Code.
Computer Architecture
Microprocessor and Assembly Language
Assembly Language Programming Part 2
CS-401 Assembly Language Programming
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Computer Science 210 Computer Organization
Symbolic Instruction and Addressing
The Processor and Machine Language
BIC 10503: COMPUTER ARCHITECTURE
Computer Science 210 Computer Organization
Design of the Control Unit for One-cycle Instruction Execution
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Conditional Jumps and Time Delays
Introduction to Micro Controllers & Embedded System Design
Symbolic Instruction and Addressing
Flow Control Instructions
University of Gujrat Department of Computer Science
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
Some Assembly (Part 2) set.html.
CSC 497/583 Advanced Topics in Computer Security
CS501 Advanced Computer Architecture
Computer Organization and Assembly Language
Introduction to Assembly
Instruction execution and ALU
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
An Introduction to the ARM CORTEX M0+ Instructions
Low level Programming.
The ‘asm’ construct An introduction to the GNU C/C++ compiler’s obscure syntax for doing inline assembly language.
Presentation transcript:

1 GCC Inline Assembly (asm) Example 1 : assignment System oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/spSession 14 – 26 May 2015 Ref: B. Hirsbrunner: " ASM Memento ", © UniFr 2015, diuf.unifr.ch/pai/sp > Tutorials in bold italic: pure assembly code : op-code op1, op2 main() { // assignment: m = n; int n=10, m; asm( "movl %1, %0" // move %1 (n) to %0 (m), // in long-word (32-bit) memory reference : "=r" (m) // output list: m is accessible via %0 in // write-only mode : "r" (n) // input list: n is accessible via %1 ); }

2 GCC Inline Assembly (asm) Example 2 : loop main() { // while(++counter != max){}; int counter=0, max=100; asm( "loop: \n\t" // declare the label 'loop' "incl %0 \n\t" // increment %0 (counter), // in long (32-bit) memory reference "cmp %0, %1 \n\t" // compare %0 (counter) and %1 (max), and // set the status register to 'equal' or // 'not equal' "JNE loop \n\t" // jump to label 'loop' if status // register is set to 'not equal' : "+m" (counter) // output operand: counter is accessed in // read and written mode via %0 : "r" (max) // input operand: max is accessible in // read mode via %1 ); } in bold italic: pure assembly code

3 Some ASM Instructions and AT&T Syntax Some basic asm instructions dec argdecrement the value of arg by one inc argincrement the value of arg by one mov src, destmove the value from src to dest cmp op1, op2compare the values and set the status register to ‘equal’ or ‘not equal’ jmp labeljump to ‘label’ jne labelJNE (Jump Not Equal) : jump to ‘label’ if the status register is set to 'not equal Some advanced asm instructions xchg op1, op2atomic operation (i.e. no interruption by scheduler): exchange the two values op1 and op2 locknext instruction is locked (assert LOCK# signal for duration of the next instruction, i.e. add atomicity to the next instruction) cliClear interrupt flag (i.e. clear the IF flag in the EFLAGS register); maskable external interrupts are disabled stiSet interrupt flag; external maskable interrupts are enabled at the end of the next instruction. (This behavior allows external interrupts to be disabled at the beginning of a procedure and enabled again at the end of the procedure.)

4 Extended ASM Inline Register Operand Constraint "r"gcc may keep the variable in any of the available General Purpose Registers (GPRs). "m"gcc may keep the variable in memory, and helps gcc about the right mode : b, w, l, q. The extended inline format allows to specify the input, output and clobbered registers: asm(assembler template : list of the output register operands : list of the input register operands : list of clobbered registers ); Constraint Modifiers "="Means that the operand is write-only by the assembler template. "+"Means that the operand is both read and written by the assembler template.