www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Hello Assembly!

reply "Taylor Hillegeist" <taylorh140 gmail.com> writes:
So i was playing around with the D inline assembly trying to make 
it say hello world on my windows setup...

void main(){
     asm
     {
         myhello:
         db "HELLO, WORLD$";
         mov EAX , myhello;
         mov AH, 0x09;
         int 0x21;
     }
}


I figure this should do it. but i'm running into problems. 
Anybody know why?
Aug 12 2015
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Wed, 12 Aug 2015 22:10:30 +0000, Taylor Hillegeist wrote:

 I figure this should do it. but i'm running into problems. Anybody know
 why?
Describe "problems"
Aug 12 2015
parent "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Wednesday, 12 August 2015 at 22:14:58 UTC, Justin Whear wrote:
 On Wed, 12 Aug 2015 22:10:30 +0000, Taylor Hillegeist wrote:

 I figure this should do it. but i'm running into problems. 
 Anybody know why?
Describe "problems"
object.Error (0): Access Violation ---------------- 0x00402028 0x38004023 0x6C0018FF 0x38004023 0xE50018FF 0xA1004022 0x010041E0 0x38000000 0x5C0018FF 0x38000001 0xD00018FF 0xF40100FD 0x780018FD 0x1E0018FF Thats pretty much it!
Aug 12 2015
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 12 August 2015 at 22:10:32 UTC, Taylor Hillegeist 
wrote:
 So i was playing around with the D inline assembly trying to 
 make it say hello world on my windows setup...
Have you ever written assembly for Windows before? Your code looks more like DOS (aside from the EAX, which would be overwriten by the AH mov anyway! In DOS, I think it was DX.) But DOS code won't work here anyway, since it was 16 bit and D makes 32 or 64 bit exes. The way you'd typically do it on Windows is to just call one of the win32 api functions, similarly to how you'd do it from C or regular D, just calling the functions manually.
Aug 12 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 12 August 2015 at 22:18:41 UTC, Adam D. Ruppe wrote:
 The way you'd typically do it on Windows is to just call one of 
 the win32 api functions, similarly to how you'd do it from C or 
 regular D, just calling the functions manually.
Here's an example: import core.sys.windows.windows; // make the names of C funcs available void main() { int written; // just let D handle the local var for us asm { // the goal is: // WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), myhello.ptr, myhello.length, &written, null); // so call GetStdHandle first push STD_OUTPUT_HANDLE; call GetStdHandle; // the return value is now in EAX mov EBX, EAX; // save it for later in EBX // we push arguments from right to left for the WriteConsoleA call.. push 0; // null mov EAX, written; // local vars in D are available too push EAX; // &written push 13; // length of "HELLO, WORLD\n" lea EAX, myhello; // the address of our string push EAX; // pointer push EBX; // our saved handle from before call WriteConsoleA; jmp past_hello; // need to jump past the string since it isn't actually executable code! myhello: db "HELLO, WORLD\n"; past_hello: nop; } } That should run successfully. Putting the string in a db like that isn't ideal either, you should probably just put it in an ordinary D variable too so the compiler can place it in the right place. Then you can also load it. Since D asm complains about the .ptr property thinking it means an instruction, I would do something like: string hello = "HELLO, WORLD\n"; auto myhello = hello.ptr; Then you can just `mov EAX, myhello;` and it will work.
Aug 12 2015
parent "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Wednesday, 12 August 2015 at 22:32:30 UTC, Adam D. Ruppe wrote:
 On Wednesday, 12 August 2015 at 22:18:41 UTC, Adam D. Ruppe 
 wrote:
 [...]
Here's an example: [...]
Wow, very cool thanks!
Aug 12 2015
prev sibling parent "Taylor Hillegeist" <taylorh140 gmail.com> writes:
On Wednesday, 12 August 2015 at 22:18:41 UTC, Adam D. Ruppe wrote:
 On Wednesday, 12 August 2015 at 22:10:32 UTC, Taylor Hillegeist 
 wrote:
 So i was playing around with the D inline assembly trying to 
 make it say hello world on my windows setup...
Have you ever written assembly for Windows before? Your code looks more like DOS (aside from the EAX, which would be overwriten by the AH mov anyway! In DOS, I think it was DX.) But DOS code won't work here anyway, since it was 16 bit and D makes 32 or 64 bit exes. The way you'd typically do it on Windows is to just call one of the win32 api functions, similarly to how you'd do it from C or regular D, just calling the functions manually.
Ahh, It probably is! I was looking for a minimal example. DOS != Windows CMD I was following the example on http://web.archive.org/web/20100529113659/http://home.comcast.net/~fbkotler/clueless.html It is werid working with asm on windows... RISC/asm is much more fimilar to me..
Aug 12 2015