BBC BASIC for Windows
Programming >> Assembly Language Programming >> Bit 2 of OPT is useful after all!
http://bb4w.conforums.com/index.cgi?board=assembler&action=display&num=1244109089

Bit 2 of OPT is useful after all!
Post by admin on Jun 4th, 2009, 09:51am

The OPT pseudo-op controls various aspects of the assembly process in BBC BASIC. When bit 2 is set (i.e. when OPT has the values 4, 5, 6, 7, 12, 13, 14 or 15) the location at which the assembled code is stored in memory, and the address at which it is designed to run, can be different (O% and P% respectively).

This feature was provided in the original versions of BBC BASIC to allow code to be assembled which would subsequently be relocated, for example into a 'sideways' ROM or RAM. I have always considered it to have virtually no practical value in BBC BASIC for Windows, because almost invariably you will want to run the code in the location to which it has been assembled.

I'm pleased to say I was wrong. I have recently been experimenting with 'injecting code into another process' (in particular, to allow subclassing a window belonging to another process) and the above-mentioned feature of the assembler is ideally suited to that.

One first allocates a block of memory in the 'remote' process (VirtualAllocEx), the address of which is determined by Windows, and is likely not to be a valid address in one's own process. Next one assembles some code which is stored in the BASIC heap as normal. Then the assembled code is copied to the remote process's address space (WriteProcessMemory).

So for the 'injected' code to run successfully in the remote process it must be assembled to run at the allocated address but actually stored in BASIC's local heap. This is something which would be extremely difficult to achieve were it not for the facility provided by setting bit 2 of OPT.

Richard.
Re: Bit 2 of OPT is useful after all!
Post by Michael Hutton on Jun 5th, 2009, 08:09am

This is *very* interesting. I suppose it will be 'easy' now to get GWL_WNDPROC from that process for subclassing.

Michael