Article View: rec.games.programmer
Article #2256Re: Writing code at run-time
From: Junaid A. Walker
Date: Tue, 07 Jun 1994 04:23
Date: Tue, 07 Jun 1994 04:23
77 lines
4270 bytes
4270 bytes
There's quite a bit of interest to this so far, just to reply to my own posting and add a little clarity; Well its that time of year.... How does one set up an area under Linux gcc/ MSDOS djgpp write some machine code instructions to this area, and then execute this code all at runtime. I am programming a sprite library that reads in a gif, compiles it to machine code that will copy the sprite onto the screen. (Xlib people listenning? Why waste time compiling bit maps at development time? And why distribute huge executables?) Obviously this is a massive speed optimization (x4) because you completely eliminate all loops - sort of the difference between an interpreted (normal) program that must scan some form of internal pixel map representation and translating it to a linear/planar raster, and a compiled program with everything pre-calculated on a case-by-case basis (a common appoach for the highest speed FFTs). For the curious, a few ideas; ***Linux: (this is currently what I'm using) 1) malloc the required # bytes 2) use mmap() to create a executable segment mapping of 1) 3) store a pointer to 1) in a function pointer, and invoke pointed to function (making sure function passing prolog is included in code). OR use a bit of inline assembler to make a direct call to register loaded with pointer in 1) thus avoiding having to set up the stack for C/C++ type parameter passing. The above is fairly portable because mmap() is available on most sane systems, and those that dont have mmap()....well we've always got the gdb internals to paw over. ***MSDOS djgpp: As above, but is the data segment executable? If not go32 hacking required. What about under VCPI/DPMI go32 interfaces? ***MSDOS real mode: Trivial, all segments are executable. But as you would expect a bit cludgy because all functions must be <64K. Probably along the lines of; void far (*fn_ptr)(void); int zero=0; /*force huge ptr normalization, watch out for*/ /*optimizer tho*/ fn_ptr=(char huge *)farmalloc(FUNC_SIZE) + zero; /*alloc required space and normalize ptr to get full*/ /*64K addressability*/ fn_ptr+=0x0004; /*lets dword align for 486's by rounding up */ /*to next dword*/ fn_ptr&=0xfffc; /*now round down by truncating last 2 bits*/ /*now load assembler opcodes beginning at fn_ptr - dont forget retf*/ /*now load registers with function paras eg _AX=50; ...*/ /*OR set up the stack and push args*/ (*fn_ptr)(); /*call the function*/ Finally thank you for any interest you might take, for i would be greateful if you would post a follow up *and* mail me a reply (some of these groups have far too large volume for me to read regularly). This way i might learn a thing or two, and i can post a summary of responses with the 'correct' solutions for others edification. Regards, Junaid 'Hat-trick' Walker
Technical Details
Message-ID:
<2t0sn7$m1r@harbinger.cc.monash.edu.au>Path:
arc1-ca.pugleaf.net!nntp.gmd.de!Germany.EU.net!EU.net!sunic!trane.uninett.no!ifi.uio.no!wabbit.cc.uow.edu.au!metro!inferno.mpx.com.au!news.cs.su.oz.au!harbinger.cc.monash.edu.au!barney.eng.monash.edu.au!junaidShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10f6aa,e6b2b5751c2b9056 X-Google-Attributes: gid10f6aa,public X-Google-Thread: fe306,e6b2b5751c2b9056 X-Google-Attributes: gidfe306,public X-Google-Thread: 1103af,e6b2b5751c2b9056 X-Google-Attributes: gid1103af,public X-Google-Thread: 102983,e6b2b5751c2b9056 X-Google-Attributes: gid102983,public X-Google-Thread: 102bfa,e6b2b5751c2b9056 X-Google-Attributes: gid102bfa,public X-Google-Thread: f9382,e6b2b5751c2b9056 X-Google-Attributes: gidf9382,public X-Google-Thread: 10a997,e6b2b5751c2b9056 X-Google-Attributes: gid10a997,public X-Google-ArrivalTime: 1994-06-06 22:17:21 PST Path: arc1-ca.pugleaf.net!nntp.gmd.de!Germany.EU.net!EU.net!sunic!trane.uninett.no!ifi.uio.no!wabbit.cc.uow.edu.au!metro!inferno.mpx.com.au!news.cs.su.oz.au!harbinger.cc.monash.edu.au!barney.eng.monash.edu.au!junaid From: junaid@barney.eng.monash.edu.au (Junaid A. Walker) Newsgroups: rec.games.programmer,alt.uu.comp.os.linux.questions,aus.computers.linux,comp.os.linux,comp.os.linux.misc,alt.msdos.programmer,comp.os.msdos.programmer Subject: Re: Writing code at run-time Date: 7 Jun 1994 04:23:03 GMT Organization: Monash University Lines: 78 Message-ID: <2t0sn7$m1r@harbinger.cc.monash.edu.au> NNTP-Posting-Host: barney.eng.monash.edu.au X-Newsreader: TIN [version 1.2 PL1]