Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason you are getting the bus error is because you are calling <code>ret</code> within your assembly code. <code>ret</code> causes the program control to transfer to the return address at the top of the stack, which you manipulate through use of <code>push</code> and <code>pop</code>. I'd highly suggest looking up what <code>ret</code> does in the Intel Instruction Set Reference.</p> <p>Below is code that I compiled, and runs successfully, on an iMac running Mac OS X 10.6.7.</p> <pre><code>#include &lt;stdio.h&gt; /*__declspec(naked)*/ void doStuff(unsigned long int val, unsigned long int flags, unsigned char *result) { __asm { push eax push ebx push ecx mov eax, dword ptr[ebp + 8] //val mov ebx, dword ptr[ebp + 12] //flags mov ecx, dword ptr[ebp + 16] //result and eax, ebx mov [ecx], eax pop ecx pop ebx pop eax } } int main(int argc, char *argv[]) { unsigned long val = 0xAA00A1F2; unsigned long flags = 0x00100002; unsigned char result = 0x0; doStuff(val, flags, &amp;result); printf("Result is: %2Xh\n", result); return 0; } </code></pre> <p>Notable changes are:</p> <ol> <li>Removal of <code>ret</code> in the inline assembly</li> <li>Use of register <code>ebp</code> instead of <code>esp</code> to reference the parameters to <code>doStuff</code></li> <li>Changing <code>flags</code> to be <code>0x00100002</code></li> </ol> <p>Change (1) fixes the bus error, (2) made referencing the parameters a little more consistent, and (3) is just a quick way to make sure the function is working as expected.</p> <p>Lastly, I highly recommend you become familiar with the GNU Debugger, GDB, if you haven't already. You can find more info about it at the project page <a href="http://www.gnu.org/software/gdb/" rel="nofollow">http://www.gnu.org/software/gdb/</a> as well as information about the Mac implementation and tutorial at <a href="http://developer.apple.com/library/mac/#documentation/DeveloperTools/gdb/gdb/gdb_toc.html" rel="nofollow">http://developer.apple.com/library/mac/#documentation/DeveloperTools/gdb/gdb/gdb_toc.html</a> .</p> <p><strong>EDIT:</strong> Added basic info/link to GDB, </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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