How to reference variables inside an "asm"?

This is a discussion on How to reference variables inside an "asm"? within the C Programming forums, part of the General Programming Boards category; I've got -masm=intel on so I can use good ol' Intel syntax. I need to pop a value off the ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    How to reference variables inside an "asm"?

    I've got -masm=intel on so I can use good ol' Intel syntax. I need to pop a value off the stack into my unsigned short "cur_val", how do I reference "cur_val"?
    Code:
    struct bigint_t init_bigint_t(unsigned long count, ...)
    {
        struct bigint_t new_bigint;
        for(;count > 0;count--)
        {
            block cur_val;
            asm("pop word cur_val\n\t"); /* Error: junk `cur_val' after expression */
    
        }
    }
    Although it's Intel syntax, it's not Fasm dialect, I'm a little confused.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well that depends entirely on your compiler.
    There is NO standard for what asm does, so you should at least start with the manual.
    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
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I'm using GCC, so I guess that would mean I'm really using GAS in Intel mode().

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Yeah, I can use Google to. The hard part is finding documentation with Intel syntax turned on. It really does matther.
    Intel(Fasm):
    Code:
    lea eax, dword ptr [esi + ebx * 4 - 0x10]
    See how nice and neat that is, with it's continuity and no stupid "%"s.

    AT&T:
    Code:
    leal -0x10(%esi, %ebx, 4), %eax
    Just seeing it... Ewww... I just threw up a little... AT&T syntax is sickening.

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    struct bigint_t init_bigint_t(unsigned long count, ...)
    {
        struct bigint_t new_bigint;
        for(;count > 0;count--)
        {
            block cur_val asm("_cur_val");
            asm volatile
            (
            "pop word ptr [cur_val]#"
            );
        }
    }
    I'm getting cur_val undefined, why? Am I supposed to use some mangled form of "cur_val"?

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    struct bigint_t init_bigint_t(unsigned long count, ...)
    {
        va_list blocks;
        struct bigint_t new_bigint;
        new_bigint.count = count;
        va_start(blocks, count);
        new_bigint.root = (struct bigint_node*)malloc(sizeof(struct bigint_node)); /* Alloc space for root */
        struct bigint_node *cur_node = new_bigint.root; /* Set current node to root */
        for(;count > 0;count--)
        {
            cur_node->val = va_arg(blocks, block); /* Get block */
            cur_node->next = (struct bigint_node*)malloc(0); /* Allocate next node */
            cur_node = cur_node->next; /* Set current node to next node(the node just allocated) */
        }
        va_end(blocks);
        return new_bigint;
    }
    I went ahead and just did it with stdarg instead. Wasn't as hard as I was expecting either.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    malloc(0) is asking for trouble...

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Why does the syntax matter?

    What matters is getting the constraint strings right. The actual instruction between the first pair of quotes doesn't really matter.

    This "works" (compiles without complaint)
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    
    void foo ( unsigned long count, ...)
    {
        for(;count > 0;count--)
        {
            int cur_val;
            asm("pop %0\n" : :"m"(cur_val) );
            printf("Value=%x\n", cur_val);
        }
    }
    
    int main()
    {
        foo(3,22,44,66);
        return 0;
    }
    $ gcc -masm=intel foo.c
    $ ./a.out 
    Value=0
    Value=8048500
    Value=8048500
    It doesn't work because the top of the stack are not parameters, but the return address (and other things).
    The variadic parameters would be accessible though ebp, not esp.

    The generated code around the asm looks like this
    Code:
    .globl foo
    	.type	foo, @function
    foo:
    	push	ebp
    	mov	ebp, esp
    	sub	esp, 24
    	jmp	.L2
    .L3:
    #APP
    # 9 "foo.c" 1
    	pop DWORD PTR [ebp-4]
    
    # 0 "" 2
    #NO_APP
    	mov	eax, DWORD PTR [ebp-4]
    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.

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    The malloc(0) thing was just an accident, it was supposed to be "malloc(sizeof(struct bigint_node))".

    I'm not sure what I was thinking wanting to pop a param off the stack on my 64 bit box(Not that it would work in cdecl, as you've pointed out.). I guess I forgot that they use a weird register based calling convention.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How are variables stored in memory.
    By ineedmunchies in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2010, 05:49 PM
  2. Replies: 6
    Last Post: 12-13-2007, 07:20 PM
  3. Gnarly Linking Error HELP!!!!
    By brooksbp in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2007, 01:00 AM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  5. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM

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