www.digitalmars.com         C & C++   DMDScript  

D.gnu - printf with asm

reply Manfred Hansen <m.hansen kielnet.net> writes:
void main()
{
    int myint = 1234;
    const(char*) mystring = "This number -> %d <- should be 1234\n";

    asm
    {
            push    dword ptr myint     ; // pointer to the integer variable
declared in D
            push    dword ptr mystring  ; // pointer into the C-style string
declared in D
            call    printf              ; // call the printf function
    }
}

This programm run with dmd 2.007 but not with gdc based on version 2.005 .
My Compiler is gcc.4.1.2 under linux. 

The output:
manni manni-lx:~/dd/ass$ ./asm
This number -> 1234 <- should be 1234
Speicherzugriffsfehler (core dumped)

I believe the core dump has to with the line
const(char*) mystring ...

manni
Nov 05 2007
next sibling parent reply Manfred Hansen <m.hansen kielnet.net> writes:
Manfred Hansen wrote:

 
 void main()
 {
     int myint = 1234;
     const(char*) mystring = "This number -> %d <- should be 1234\n";
 
     asm
     {
             push    dword ptr myint     ; // pointer to the integer
             variable
 declared in D
             push    dword ptr mystring  ; // pointer into the C-style
             string
 declared in D
             call    printf              ; // call the printf function
     }
 }
 
 This programm run with dmd 2.007 but not with gdc based on version 2.005 .
 My Compiler is gcc.4.1.2 under linux.
 
 The output:
 manni manni-lx:~/dd/ass$ ./asm
 This number -> 1234 <- should be 1234
 Speicherzugriffsfehler (core dumped)
 
 I believe the core dump has to with the line
 const(char*) mystring ...
 
 manni
Hello, i make my programm a little bit smaller to get a segmentation fault. void main() { int myint = 1234; asm { push dword ptr myint; } } I have tested the programm under Debian/Sidux and Kubuntu feisty. Can someone reproduce the error, maybe the problem is not the gdc compiler, it's my operating system ? manni
Nov 07 2007
parent David Friedman <dvdfrdmn users.ess-eff.net> writes:
Manfred Hansen wrote:
 Manfred Hansen wrote:
 
 void main()
 {
     int myint = 1234;
     const(char*) mystring = "This number -> %d <- should be 1234\n";

     asm
     {
             push    dword ptr myint     ; // pointer to the integer
             variable
 declared in D
             push    dword ptr mystring  ; // pointer into the C-style
             string
 declared in D
             call    printf              ; // call the printf function
     }
 }

 This programm run with dmd 2.007 but not with gdc based on version 2.005 .
 My Compiler is gcc.4.1.2 under linux.

 The output:
 manni manni-lx:~/dd/ass$ ./asm
 This number -> 1234 <- should be 1234
 Speicherzugriffsfehler (core dumped)

 I believe the core dump has to with the line
 const(char*) mystring ...

 manni
Hello, i make my programm a little bit smaller to get a segmentation fault. void main() { int myint = 1234; asm { push dword ptr myint; } } I have tested the programm under Debian/Sidux and Kubuntu feisty. Can someone reproduce the error, maybe the problem is not the gdc compiler, it's my operating system ? manni
GCC puts special stack alignment code in the main function so you cannot leave the stack unbalanced. You could either restore the stack to the state it was in before the asm block or you could put the asm in another function. David
Nov 07 2007
prev sibling parent Charles Daffern <seejay.11 gmail.com> writes:
You're pushing the pointer to the pointer to the string:

const(char*) mystring = "This number -> %d <- should be 1234\n";
and
push    dword ptr mystring  ; // pointer into the C-style string

You declared a char* mystring.
The value of mystring is a pointer to the string's data.
You push the pointer of mystring.
Therefore, printf expects a pointer to a string
(ptr -> string data)
but gets a pointer to a pointer.
(ptr -> mystring -> string data)
To solve this, push mystring, not the pointer to mystring.
Feb 04 2008