The asm construct - PowerPoint PPT Presentation

About This Presentation
Title:

The asm construct

Description:

The asm construct – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 9
Provided by: usfi
Learn more at: https://www.cs.usfca.edu
Category:
Tags: asm | construct | img

less

Transcript and Presenter's Notes

Title: The asm construct


1
The asm construct
  • An introduction to the GNU C/C compilers
    obscure syntax for doing inline assembly language

2
The asm construct
  • When using C/C for systems programs, we
    sometimes need to employ processor-specific
    instructions (e.g., to access CPU registers or
    the current stack area)
  • Because our high-level languages strive for
    portability across different hardware
    platforms, these languages dont provide direct
    access to CPU registers or stack

3
gcc/g extensions
  • The GNU compilers support an extension to the
    language which allows us to insert assembler code
    into our instruction-stream
  • Operands in registers or global variables can
    directly appear in assembly language, like this
    (as can immediate operands)
  • int count 4 // global variable
  • asm( movl count , eax )
  • asm( imull 5, eax, ecx )

4
Local variables
  • Variables defined as local to a function are more
    awkward to reference by name with the asm
    construct, because they reside on the stack and
    require the generation of offsets from the ebp
    register-contents
  • A special syntax is available for handling such
    situations in a manner that gcc/g can decipher

5
Template
  • The general construct-format is as follows
  • asm( instruction-template
  • output-operand
  • input-operand
  • clobber-list )

6
Example from switcher.cpp
  • void upon_signal( int signum )
  • unsigned long tos
  • asm(" movl ebp, 0 " "m" (tos) )
  • for (int i 0 i lt 22 i)
  • printf( "tosd08X \n", i, tosi )

7
Example from pgfaults.c
  • void load_IDTR( void img )
  • asm( lidt 0 m (img) )
  • Heres how we used this function
  • unsigned short newidtr 3
  • load_IDTR( newidtr )

8
How to see your results
  • You can ask the gcc compiler to stop after
    translating your C/C source-file into x86
    assembly language
  • gcc S myprog.cpp
  • Then you can view the output myprog.s by using
    the cat command (or an editor)
  • cat myprog.s more
Write a Comment
User Comments (0)
About PowerShow.com