Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They are not similar, pointers are just addresses, they may seem mystical in c/c++ due to the abstractions but in assembly a pointer could be a register with an address stored in it that "points" to some data, EIP is a pointer that "points" to the current instruction during the programs execution, the whole idea is that it's "cheaper" to copy an address that points to data than the data itself.</p> <p>I think the confusion comes from the fact that c/c++ are strongly type languages and assembly is loosely typed.</p> <p>This <a href="http://en.wikipedia.org/wiki/Strong_and_weak_typing" rel="nofollow">wiki article</a> on Strong and weak typing explains it all, it has this to say about pointers..</p> <p><strong>Pointers</strong></p> <p><em>Some programming languages expose pointers as if they were numeric values, and allow users to perform arithmetic on them. These languages are sometimes referred to as "weakly typed", since pointer arithmetic can be used to bypass the language's type system.</em></p> <p>Even with this said if you read this <a href="http://www.cplusplus.com/doc/tutorial/pointers/" rel="nofollow">tutorial on pointers</a> it says;</p> <pre><code>a[5] = 0; // a [offset of 5] = 0 *(a+5) = 0; // pointed by (a+5) = 0 </code></pre> <p>these two expressions are equivalent which makes sense when you think about it.</p> <p><code>a</code> is just a pointer for an array in assembly you might have something roughly like this;</p> <pre><code>.data a db "some data" </code></pre> <p><code>.data</code> is a pointer to the address where the data lives <code>a:</code> is also a pointer to the address that begins right where the label a is in the program just before definition of the byte <code>'s'</code> in <code>"some data"</code> just like if the pointer a in c was;</p> <pre><code>char a[] = "some data"; // or char *a = "some data"; // and a is the start address </code></pre> <p>accessing them looks like;</p> <pre><code>a[5] == 'd' &amp;&amp; *(a+5) == 'd'; // true </code></pre> <p>pointing to a looks like;</p> <pre><code>char *b = a; </code></pre> <p>access looks like this in assembly;</p> <pre><code>mov al, byte[a+5] // calculates effective address or points to the 'd' cmp al, 'd' // if al == 'd' je some_appropriate_label // je(jump if equal) somewhere anywhere //(some_appropriate_label being an address or pointer to the begining of some appropriate code) </code></pre> <p>pointing to or getting the address in assembly looks something like;</p> <pre><code>mov ebx, a // moves the address that a represents into ebx mov bl, byte[ebx+5] // moves 'd' into bl </code></pre> <p>In assembly everything is pretty exposed.</p> <hr> <p>I felt like doing some extra investigation for you, i made this simple c program called test.c;</p> <pre><code>int main(){ char *pointer1 = "some data"; char *pointer2 = pointer1; } </code></pre> <p>and fed <code>gcc -S -masm=intel -fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm test.c</code> through <code>gcc</code> to get <code>test.s</code> the assembly equivalent of <code>test.c</code> this is the file;</p> <pre><code> .file "test.c" .intel_syntax noprefix .def ___main; .scl 2; .type 32; .endef .section .rdata,"dr" LC0: .ascii "some data\0" .text .globl _main .def _main; .scl 2; .type 32; .endef _main: push ebp mov ebp, esp and esp, -16 sub esp, 16 call ___main mov DWORD PTR [esp+12], OFFSET FLAT:LC0 mov eax, DWORD PTR [esp+12] mov DWORD PTR [esp+8], eax leave ret .ident "GCC: (rev2, Built by MinGW-builds project) 4.8.1" </code></pre> <p>notice these parts;</p> <pre><code>LC0: .ascii "some data\0" </code></pre> <p>and</p> <pre><code>call ___main mov DWORD PTR [esp+12], OFFSET FLAT:LC0 mov eax, DWORD PTR [esp+12] mov DWORD PTR [esp+8], eax </code></pre> <p>It looks like this program is using the stack to store it's pointers, <code>esp</code> is the stack pointer, it contains the address or pointer to the top of the stack at any time.</p> <p><strong>pointer1</strong></p> <pre><code>mov DWORD PTR [esp+12], OFFSET FLAT:LC0 // moves address where data lives into stack // this is where pointer1 lives </code></pre> <p><strong>pointer2</strong></p> <pre><code>mov eax, DWORD PTR [esp+12] // moves address/pointer into eax from stack mov DWORD PTR [esp+8], eax // moves address/pointer into pointer2 // esp+12 is the c pointer (think *(a+0) a[0] from c but instead of char 's' it's an address(dword), // LCO is the data that was moved into the pointer which is also an address // The second line is basically saying; // move the 4byte address to the topOfStack+8bytes </code></pre>
    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