How to use asm code in c language

This is a discussion on How to use asm code in c language within the C Programming forums, part of the General Programming Boards category; How to use asm code in c language. I am using turbo c++ complier...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    1

    Question How to use asm code in c language

    How to use asm code in c language. I am using turbo c++ complier

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    What have you tried?
    Have you read the manual?
    Have you searched the web?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Check out the asm keyword.

    You must have TASM.EXE in the same directory as all your other BIN files.

    Turbo C++ will see the asm keyword and will invoke TASM for you.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    23
    Ah,...inline assembly

    Well, this is tricky because every compiler seems to do it a little differently.

    As suggested, here is how you would do it in TurboC++

    Code:
    int main(int argc, char* argv[]){
         asm mov ax,4C00h
         asm int 21h
    }
    for a DOS program that only exits, returning exit code of 0

    or even
    Code:
    int main(int argc, char* argv[]){
         asm{
              mov ax,4C00h
              int 21h
         }
    }
    for Microsoft's compilers, same thing but you use _asm instead of asm.

    for gcc, it is completely different, yet again, and you have to even use the AT&T style syntax instead of INTEL syntax. (personally, I hate the AT&T style, but, eh). The most annoying thing about AT&T style is that the operands are reversed, so you would have the thing to move on the left and where to move it on the right (instead of the way INTEL does it). I think newer gcc can use Intel syntax as well but haven't tried it.

    anyhow, it is specific to compiler (and OS and processor, but you already knew that)

    oh yeah, and you probably don't want to mess with ebp or bp in your inline assembly as most C compilers use it for tracking where you are in your program (stacks, procedure calls, and such).

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    oh yeah, and you probably don't want to mess with ebp or bp in
    your inline assembly as most C compilers use it for tracking where
    you are in your program (stacks, procedure calls, and such).
    Well not exactly...but I won't bother explaining. However this doesn't mean you can't use the stack and should be afraid of ebp or esp. If you are going to use the stack in your asm function and you are going to call this function from C...set this framework up every time and you will be ok.


    push ebp ;save ebp
    mov ebp,esp

    ...
    ...//asm code here
    ...


    pop ebp ;restore ebp
    ret
    Last edited by VirtualAce; 05-07-2004 at 12:43 AM.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    23
    Quote Originally Posted by Bubba
    Well not exactly...but I won't bother explaining. However this doesn't mean you can't use the stack and should be afraid of ebp or esp. If you are going to use the stack in your asm function and you are going to call this function from C...set this framework up every time and you will be ok.


    push ebp ;save ebp
    mov ebp,esp

    ...
    ...//asm code here
    ...


    pop ebp ;restore ebp
    ret
    Well, what I meant was it uses ebp for pointing at the parameters passed to the function but also the return address is going to be on the stack somewhere. If you don't save the stack, when you return from function, you'll jump into the middle of some different bytecodes (probably not what you wanted) which hence will send you to a bad place in the program (aka, tracking where you are via the stack).

    But, yes, you are correct that you can be pretty safe by using the prolog and epilog code you gave there.

    If I am mistaken here, please help me figure out where I went wrong??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 02:16 AM
  2. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  3. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 02:20 AM
  4. Machine code, asm, poop
    By tim545666 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-02-2002, 01:27 AM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21