Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A comment wasn't enough space. Joe_Muc is correct. You should not stuff code into memory obtained by <code>malloc</code> or <code>new</code>. You will run into problems if you change the page properties of pages that Windows allocates.</p> <p>This isn't a problem becuase using VirtualAlloc() and the related WIn32 APIs is every easy: call <a href="http://msdn.microsoft.com/en-us/library/aa366887.aspx" rel="noreferrer">VirtualAlloc(</a>) and set the <code>flProtect</code> to <code>[PAGE_EXECUTE_READWRITE][2]</code></p> <p>Note, you should probably do three allocations, one guard page, the pages you need for your code, then another guard page. This will give you a little protection from bad code.</p> <p>Also wrap calls to your generated code with <a href="http://msdn.microsoft.com/en-us/library/ms680657(VS.85).aspx" rel="noreferrer">structured exception handling</a>.</p> <p>Next, the Windows X86 ABI (calling conventions) are not well documented (I know, I've looked). There is some info <a href="http://support.microsoft.com/kb/100832" rel="noreferrer">here</a>, <a href="http://msdn.microsoft.com/en-us/library/aa235439(VS.60).aspx" rel="noreferrer">here</a>, <a href="http://www.agner.org/optimize/calling_conventions.pdf" rel="noreferrer">here</a> The best way to see how things work is to look at code generated by the compiler. This is easy to do with the <a href="http://msdn.microsoft.com/en-us/library/367y26c6(VS.80).aspx" rel="noreferrer"><code>\FA</code> switches</a> ( there are four of them).</p> <p>You can find the 64-bit calling conventions <a href="http://msdn.microsoft.com/en-us/library/ms794533.aspx" rel="noreferrer">here</a>.</p> <p>Also, you can still obtain Microsoft's Macro Assembler <a href="http://www.microsoft.com/downloads/details.aspx?familyid=7A1C9DA0-0510-44A2-B042-7EF370530C64&amp;displaylang=en" rel="noreferrer">MASM here</a>. I recommend writing your machine code in MASM and look at its output, then have your machine code generator do similar things. </p> <p><a href="http://www.intel.com/products/processor/manuals/index.htm" rel="noreferrer">Intel's</a> and <a href="http://www.amd.com/us-en/Processors/TechnicalResources/0,,30_182_739_15343,00.html" rel="noreferrer">AMD's</a> processor manuals are good references - get them if you don't have them.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload