digitalmars.D.learn - sys_write in betterC doesn't write anything
- Basile B. (20/20) Feb 03 2018 compiles with -betterC -m32
- Adam D. Ruppe (8/10) Feb 03 2018 And that's the address of a D string, instead of the address of
- Basile B. (3/13) Feb 03 2018 Yeah, I forgot the ABI. It's working well
- Basile B. (21/42) Feb 03 2018 okay solved:
- Patrick Schluter (3/26) Feb 03 2018 [msg] contains the length of the string, so there's no need of
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
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 writeAnd 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
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:Yeah, I forgot the ABI. It's working well https://forum.dlang.org/post/edjipahtfphiqmxsiioq forum.dlang.org__gshared static msg = "betterC";That's a D string....mov ECX, msg ;//message to writeAnd 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
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
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:[msg] contains the length of the string, so there's no need of your len variable. Just saying.[...]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