Thread: ASM With C Programming

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    ASM With C Programming

    i have started to write a program that asks the user to enter a number to change the colour of the text and then again for the background.

    however i am stuck on how to use C with ASM.

    here is the code i have so far.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void ClearScreen(void);
    void BGcolour(void);
    
    void main()
    {
    ClearScreen();
    BGcolour();
    }
    
    
    void BGcolour()
    {
    int number;
    
    printf ("Please enter a number");
    scanf ("%d", number);
    
    
              asm{mov ah,02
                  mov bh,0 //sets where the writing is. bigger the number the more it shifts accross the screen.
                  mov dx,0
                  int 10h
                  mov ah,9
                  mov cx,2000
                  mov bl,4eh//first number is background colour, second is text colour
                  mov al,' '
                  int 10h
                  }
    
    printf ("Hello this is the new text colour");
    }
    
    
    
    void ClearScreen(void)
    {
    
            asm{mov ah,6
                mov al,0
                mov bh,1
                mov ch,0
                mov cl,0
                mov dh,24
                mov dl,79
                int 10h
                }
    }
    sorry if this is in the wrong section

    can anyone help?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What compiler and OS?
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    Oh dear.

    > scanf ("%d", number);
    I wonder what random memory location that trashed.
    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.

  4. #4
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    sorry bout the void main just used it. im using turbo c compiler i think

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I see you have already found the invention called inline assembly (which makes me cry) If you want to use system calls, we really need to know what OS you are using. Here's an snippet of calling a function MyFunc that takes two parameters, adds them, and returns the anwer:
    Code:
    /*C code*/
    _cdecl int MyFunc(int a, int b)
    {
         return a+b;
    }
    The _cdecl ensures parameters passed on the stack from right to left order and that the function cleans up the stack. This C code produces this assembly code:
    Code:
    :_MyFunc
    pushl %ebp
    movl %ebp, %esp
    movl %eax, [%ebp + 8]
    movl %edx, [%ebp + 12]
    addl %eax, %edx
    popl %ebp
    ret
    Then to call this in assembly to calculate 1+2 you would go:
    Code:
    pushl $2
    pushl $3
    call _MyFunc
    addl $8, %esp
    Then your anwer is in eax. As for inline assembly, check out the link. This is one of the most advanced subjects in C and really makes your code confusing. I try to avoid it whenever I can. But if you want to take it on, be my guest.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    :_MyFunc
    pushl %ebp
    movl %ebp, %esp
    movl %eax, [%ebp + 8]
    movl %edx, [%ebp + 12]
    addl %eax, %edx
    popl %ebp
    retThen to call this in assembly to calculate 1+2 you would go:

    Code:
    pushl $2
    pushl $3
    call _MyFunc
    addl $8, %esp
    None of that will work for MSVC, Borland, or Intel compilers be it inline or pure asm files. That is AT&T syntax and most popular compilers use Intel syntax, not AT&T. The only one I know of that uses AT&T is DJGPP and maybe Watcom although I've not heard about it in a very long time. I don't know what Dev-C uses since I stay as far away as possible from that compiler.

    For MSVC you must use __asm not just asm. There are lots of rules concerning assembly language in your compiler and therefore the help docs should explain everything you need to know about inline assembly short of explaining assembly language in general.

    Be aware though that since these compilers use Intel syntax and do not allow a register frag list, you really have no clue as to the state of the registers upon entry into an inline asm block.
    Consult your compiler help for more info.

    As to the parameter order on the stack, this is relative to the call type of the function. Since this type can be changed with keywords in C, you must check the function prototype to understand which order the params will be in.

    I don't recommend using assembly unless you are needing a highly optimized code section that just cannot be matched in C. This doesn't appear to be one of those instances.

    Back in the DOS days and old compilers assembly made a big difference but in recent times this difference is fading and nearly disappearing. Be cautious and profile the code.
    Last edited by VirtualAce; 10-25-2006 at 11:02 PM.

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I use a lot of linux when I am doing my assembly programming and in linux (as well as gas) you use AT&T syntax. Regardless, the two are not that hard to convert. Take of the l's at the end of instructions, remove the $ and %, readjust the memory addressing...Also, _cdecl guarentees right-to-left order of parameters for functions.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There are a number of more signficant differences between Intel and AT&T and converting them is not as simple as you make it sound.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, but for the code I showed, there is not that much more you need to do. I've worked in assembly for many years and I understand the differences between AT&T and Intel syntax. But for the purposes of this discussion, we don't need to get into the many little things that ARE important. All I was trying to do was illustrate the way to call a C function. And I'm not trying to "make it sound" as though converting is easy.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    sorry bout the void main just used it. im using turbo c compiler i think
    Isn't it a stoneage compiler?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, it was used by the druids to build stonehenge
    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.

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You may want to upgrade to Dev-C++ 4.9.9.2 (beta, but stable) or Code::Blocks. I personally like Dev-C++ better as it is more "clean", but it also hasn't been updated in a year or so.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

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, 01:16 AM
  2. Asm + C++
    By JaWiB in forum C++ Programming
    Replies: 17
    Last Post: 06-26-2003, 03:13 PM
  3. Understanding ASM
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 07-12-2002, 07:39 PM
  4. asm mov
    By manu in forum C++ Programming
    Replies: 1
    Last Post: 12-15-2001, 12:59 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM