Thread overview
sys_write in betterC doesn't write anything
Feb 03, 2018
Basile B.
Feb 03, 2018
Adam D. Ruppe
Feb 03, 2018
Basile B.
Feb 03, 2018
Basile B.
Feb 03, 2018
Patrick Schluter
February 03, 2018
compiles with -betterC -m32

module runnable;

__gshared static msg = "betterC";
__gshared static len = 7;

extern(C) int main(int argc, char** args)
{
    asm
    {
        naked;
        mov EDX, len ;//message length
        mov ECX, msg ;//message to write
        mov EBX, 1   ;//file descriptor (stdout)
        mov EAX, 4   ;//system call number (sys_write)
        int 0x80     ;//call kernel

        mov EBX, 0   ;//process' exit code
        mov EAX, 1   ;//system call number (sys_exit)
        int 0x80     ;//call kernel - this interrupt won't return
    }
}

nothing is printed. Could this work ?
February 03, 2018
On Saturday, 3 February 2018 at 15:30:10 UTC, Basile B. wrote:
> __gshared static msg = "betterC";

That's a D string....

>         mov ECX, msg ;//message to write

And that's the address of a D string, instead of the address of the characters.

You need to put the pointer in there. So either make msg a char*, or pass msg.ptr there. With inline asm the ptr word will cause weirdness so I tend to move the pointer and length into local variables first.
February 03, 2018
On Saturday, 3 February 2018 at 15:30:10 UTC, Basile B. wrote:
> compiles with -betterC -m32
>
> module runnable;
>
> __gshared static msg = "betterC";
> __gshared static len = 7;
>
> extern(C) int main(int argc, char** args)
> {
>     asm
>     {
>         naked;
>         mov EDX, len ;//message length
>         mov ECX, msg ;//message to write
>         mov EBX, 1   ;//file descriptor (stdout)
>         mov EAX, 4   ;//system call number (sys_write)
>         int 0x80     ;//call kernel
>
>         mov EBX, 0   ;//process' exit code
>         mov EAX, 1   ;//system call number (sys_exit)
>         int 0x80     ;//call kernel - this interrupt won't return
>     }
> }
>
> nothing is printed. Could this work ?

okay solved:



module runnable;

__gshared static msg = "betterC\n";
__gshared static len = 8;

extern(C) int main(int argc, char** args)
{
    asm
    {
        naked;
        mov EDX, len ;//message length
        mov ECX, [msg + 4] ;//message to write
        mov EBX, 1   ;//file descriptor (stdout)
        mov EAX, 4   ;//system call number (sys_write)
        int 0x80     ;//call kernel

        mov EBX, 0   ;//process' exit code
        mov EAX, 1   ;//system call number (sys_exit)
        int 0x80     ;//call kernel - this interrupt won't return
    }
}

the pointer to the string data is 4 bytes later...
February 03, 2018
On Saturday, 3 February 2018 at 15:38:02 UTC, Adam D. Ruppe wrote:
> On Saturday, 3 February 2018 at 15:30:10 UTC, Basile B. wrote:
>> __gshared static msg = "betterC";
>
> That's a D string....
>
>>         mov ECX, msg ;//message to write
>
> And that's the address of a D string, instead of the address of the characters.
>
> You need to put the pointer in there. So either make msg a char*, or pass msg.ptr there. With inline asm the ptr word will cause weirdness so I tend to move the pointer and length into local variables first.

Yeah, I forgot the ABI. It's working well

https://forum.dlang.org/post/edjipahtfphiqmxsiioq@forum.dlang.org
February 03, 2018
On Saturday, 3 February 2018 at 15:38:19 UTC, Basile B. wrote:
> On Saturday, 3 February 2018 at 15:30:10 UTC, Basile B. wrote:
>> [...]
>
> okay solved:
>
>
>
> module runnable;
>
> __gshared static msg = "betterC\n";
> __gshared static len = 8;
>
> extern(C) int main(int argc, char** args)
> {
>     asm
>     {
>         naked;
>         mov EDX, len ;//message length
>         mov ECX, [msg + 4] ;//message to write
>         mov EBX, 1   ;//file descriptor (stdout)
>         mov EAX, 4   ;//system call number (sys_write)
>         int 0x80     ;//call kernel
>
>         mov EBX, 0   ;//process' exit code
>         mov EAX, 1   ;//system call number (sys_exit)
>         int 0x80     ;//call kernel - this interrupt won't return
>     }
> }
>
> the pointer to the string data is 4 bytes later...

[msg] contains the length of the string, so there's no need of your len variable. Just saying.