Thread: Mixed Language: C and Asm

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Mixed Language: C and Asm

    Hi,
    can someone show me a little program using mixed language, C and Assembly AT&T syntax.
    I can't understand how it works .
    Thanks
    You ended that sentence with a preposition...Bastard!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose the first question is going to be "what's wrong with the 38,756 examples you can find on the web?"

    But generally this is probably going to be rather system-specific, but if you're using gcc why not look at the HOWTO or the gcc manual?

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Hey,
    I will post some code I have.

    Code:
    // mlpc1.c
    // C language source file for mixed language programming example
    #include <stdio.h>
    
    
    void UpperCase(char *Str);
    
    int main() {
    	char UserString[20];
    	fputs("Enter a string: ",stdout);
    	fgets(UserString,19,stdin);
    	fputs("\nYou entered: ",stdout);
    	fputs(UserString,stdout);	
    	fputs("\nAfter call to UpperCase this becomes: ",stdout);
    	UpperCase(UserString);
    	fputs(UserString,stdout);	
    	fputs("\n",stdout);
    }
    Code:
    # mpla1.s
    # assembly language source file for mixed language programming example
    
    	.data
    
    	.text
    	
    	.global UpperCase
    
    UpperCase:
    	push %ebp
    	mov %esp,%ebp
    	push %esi
    	push %eax
    	mov 8(%ebp),%esi	# make esi point to the string
    UCLoop:
    	movb (%esi),%al
    	cmp $0,%al
    	je UCExit
    	andb $0xdf,%al
    	movb %al,(%esi)
    	inc %esi
    	jmp UCLoop
    	
    UCExit:
    	pop %eax
    	pop %esi
    	pop %ebp
    	ret
    	.end
    I don't get how it works...
    You ended that sentence with a preposition...Bastard!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what is difficult about this? This is exactly the same as you writing a C program in two .c files, except you wrote one of your .c files in assembler.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    in the Uppercase function.
    i don't get why the register
    %esi or %eax was pushed
    I have just written a simple function to add 2 numbers. And didn't have to push %eax. I could use them directly since they are "global", right?
    So when do we push registers?
    I know push means returning push a variable to the stack, but I don't really get a pop.

    When you pop something off the stack, the esp is subtracted by 4 bytes.
    but that content you popped off, is it saved somewhere? What is the difference between it and ret?
    You ended that sentence with a preposition...Bastard!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    in the Uppercase function.
    i don't get why the register
    %esi or %eax was pushed
    I have just written a simple function to add 2 numbers. And didn't have to push %eax. I could use them directly since they are "global", right?
    So when do we push registers?
    I know push means returning push a variable to the stack, but I don't really get a pop.

    When you pop something off the stack, the esp is subtracted by 4 bytes.
    but that content you popped off, is it saved somewhere? What is the difference between it and ret?
    Push stores a register value to the stack so that it can be restored later.
    Pop gets the value from the stack and places it back in the register.

    Of course you have to be careful with this... if you push A B C you need to pop C B A to restore the registers correctly. Also pushing something you do not later pop will cause a misalignment between stack and data...

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    so basically if it the data we have in the register is to be used later, we only push.
    Ok, I will go try and do some experimenting on this.
    Thanks.
    You ended that sentence with a preposition...Bastard!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Eman View Post
    so basically if it the data we have in the register is to be used later, we only push.
    Ok, I will go try and do some experimenting on this.
    Thanks.
    Did you read the part about using Push and Pop with caution and in matched pairs?

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by CommonTater View Post
    Did you read the part about using Push and Pop with caution and in matched pairs?
    yeah that does that have something to do with the cdecl format?
    the parameters are popped off in the order in which they are pushed?

    One thing that is confusing me, when do we push to the stack?
    Before the function is called, or after?
    Like in
    UpperCase
    ebp is pushed to the stack. Does that make it a local?
    You ended that sentence with a preposition...Bastard!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    yeah that does that have something to do with the cdecl format?
    the parameters are popped off in the order in which they are pushed?

    One thing that is confusing me, when do we push to the stack?
    Before the function is called, or after?
    Like in
    UpperCase
    ebp is pushed to the stack. Does that make it a local?
    I'm pretty sure you've been linked to this page before. Have you ever tried actually reading that page? (Especially the part where they tell you why you have to push ebp, and why you should push any registers you intend to use in the function.) Start at "calling a __cdecl function".

  11. #11
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    read it.
    cool thanks. Hopefully, I won't be back (doubt it)
    You ended that sentence with a preposition...Bastard!

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    em, so
    andb 0xdf, %cl
    ands 223 with the lower case char.
    How can we know what figure to use? Because if this was in the exam I would have failed it instantly. I just played with anding, and it does indeed convert it to upper.
    if it was upper to lower, how would i do it? What would be my mask?
    Is there an easy of figuring it out?
    Thanks
    You ended that sentence with a preposition...Bastard!

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    Uppercase E = 69  = 0b01000101
    Lowercase e = 101 = 0b01100101

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    so i have to or it by 0x20
    E | 0x20..
    that wouldn't work for everything
    You ended that sentence with a preposition...Bastard!

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    so i have to or it by 0x20
    E | 0x20..
    that wouldn't work for everything
    Why not?

Popular pages Recent additions subscribe to a feed