www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sys_write in betterC doesn't write anything

reply Basile B. <b2.temp gmx.com> writes:
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 ?
Feb 03 2018
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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.
Feb 03 2018
parent Basile B. <b2.temp gmx.com> writes:
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
Feb 03 2018
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
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...
Feb 03 2018
parent Patrick Schluter <Patrick.Schluter bbox.fr> writes:
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.
Feb 03 2018