erafraw
erafraw

Reputation: 61

what is the main difference between ASM JUMP instruction and the Pointer in C/C++

I am newbie in ASM and C/C++. Many site show me a picture that not only the Pointers are "point to" somewhere (address) they store; but also the Junm. Please someone tell me know "what is the main difference between ASM JUMP instruction and the Pointer in C/C++". Thanks guys.

Upvotes: 4

Views: 959

Answers (2)

James
James

Reputation: 1019

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.

I think the confusion comes from the fact that c/c++ are strongly type languages and assembly is loosely typed.

This wiki article on Strong and weak typing explains it all, it has this to say about pointers..

Pointers

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.

Even with this said if you read this tutorial on pointers it says;

a[5] = 0;       // a [offset of 5] = 0
*(a+5) = 0;     // pointed by (a+5) = 0

these two expressions are equivalent which makes sense when you think about it.

a is just a pointer for an array in assembly you might have something roughly like this;

.data

   a db  "some data"

.data is a pointer to the address where the data lives a: is also a pointer to the address that begins right where the label a is in the program just before definition of the byte 's' in "some data" just like if the pointer a in c was;

char a[] = "some data"; // or
char *a = "some data"; // and a is the start address

accessing them looks like;

a[5] == 'd' && *(a+5) == 'd'; // true

pointing to a looks like;

char *b = a;

access looks like this in assembly;

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)

pointing to or getting the address in assembly looks something like;

mov ebx, a // moves the address that a represents into ebx
mov bl, byte[ebx+5] // moves 'd' into bl

In assembly everything is pretty exposed.


I felt like doing some extra investigation for you, i made this simple c program called test.c;

int main(){
    char *pointer1 = "some data";
    char *pointer2 = pointer1;
}

and fed gcc -S -masm=intel -fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm test.c through gcc to get test.s the assembly equivalent of test.c this is the file;

    .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"

notice these parts;

LC0:
    .ascii "some data\0"

and

call    ___main
mov DWORD PTR [esp+12], OFFSET FLAT:LC0
mov eax, DWORD PTR [esp+12]
mov DWORD PTR [esp+8], eax

It looks like this program is using the stack to store it's pointers, esp is the stack pointer, it contains the address or pointer to the top of the stack at any time.

pointer1

mov DWORD PTR [esp+12], OFFSET FLAT:LC0 // moves address where data lives into stack
// this is where pointer1 lives

pointer2

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

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172618

Pointers are used to store the address of the variables.

ASM JUMP is used by processor to start executing code from the address.

I dont think that there is any relevant reason to differentiate between the two as they both are different concepts and are used for different reasons.

Upvotes: 6

Related Questions