Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code has two problems:</p> <p><strong>One</strong>: the indirect memory reference should be:</p> <pre><code>mov %0, ds : [ebx] </code></pre> <p>That is, with the <code>ds</code> out of the brackets.</p> <p><strong>Two</strong>: You are using memory constraint (<code>=m</code>) to refer to the <code>mem</code> variable. That is not possible because it is a local variable, and as such does not have a fixed address. Moreover, a single instruction cannot have both origin and destination in memory, you have to use a register. The easiest way would be to indicate <code>=g</code> that basically means <em>whatever</em>, but in your case it is not possible because <code>esp</code> cannot be moved directly to memory. You have to use <code>=r</code>.</p> <p><strong>Three</strong>: (?) You are clobbering the <code>ebx</code> register, so you should declare it as such, or else do not use it that way. That will not prevent compilation, but will make your code to behave erratically.</p> <p>In short:</p> <pre><code> unsigned int mem = 0; __asm volatile ( "mov ebx, esp\n\t" "mov %0, ds : [ebx]\n\t" : "=r"(mem) :: "ebx" ); </code></pre> <p>Or better not to force to use <code>ebx</code>, let instead the compiler decide:</p> <pre><code> unsigned int mem = 0, temp; __asm volatile ( "mov %1, esp\n\t" "mov %0, ds : [%1]\n\t" : "=r"(mem) : "r"(temp) ); </code></pre> <p>BTW, you don't need the <code>volatile</code> keyword in this code. That is used to avoid the assembler to be optimized away even if the output is not needed. If you write the code for the side-effect, add <code>volatile</code>, but if you write the code to get an output, do not add <code>volatile</code>. That way, if the optimizing compiler determines that the output is not needed, it will remove the whole block.</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