[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppAsm

(C++) asm

Keyword enabling you to use assembler code.

Things inline Assembler CAN do: Things inline Assmbler CANNOT do: The code below makes your computer beep:
int main()
{
  asm
  {
    mov ax, 0x0E07
    xor bx,bx
    int 0x10
  }
  return 0;
}


Below some examples are given for C/C++ statements and how to achieve the same using assembler mnemonics.

ADD

  1. include <cassert>
int main() { int x = 1; int y = 2; int z = 0; asm { mov eax,x add eax,y mov z,eax } assert(z==3); return 0; }


JMP

Performs an unconditional jump, similar to the keyword goto.

  1. include <cassert>
int func1() { const int x = 1; const int y = 2; //z cannot be made const (which is a good thing) //doing so gives the error 'operand size mismatch' int z = 0; asm { jmp labelFunc1 //Jumps to labelFunc1 mov eax,x //Will be jumped over, stores the value of x in EAX jmp labelEnd //Will be jumped over, jumps to labelEnd labelFunc1: //Label mov eax,y //Store the value of y in EAX labelEnd: //Label mov z,eax //Store the value of EAX in z }; assert(z==y); return z; }


Jumps can only be performed to labels in the same assembler code block. When trying to jump from another function to the one above, an error will be given:

int func2()
{
  int z;
  asm
  {
    jmp labelFunc1; //ERROR! Undefined label 'labelFunc1'
    mov z, eax;
    labelFunc2:
  }
  return z;
}


MOV

  1. include <cassert>
int main() { char x = 1; assert(x==1); asm mov x,0 //Set the value of x to 0, equal to 'x = 0;' assert(x==0); return 0; }


'asm' links

  •  ?C

References



last edited (November 9, 2006) by bilderbikkel, Number of views: 5515, Current Rev: 11 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.