ASM JUMP指令和C/C++中的指针之间的主要区别是什么?

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

本文关键字:之间 指针 区别 是什么 指令 JUMP C++ ASM      更新时间:2023-10-16

我是ASM和C/C++的新手。许多网站向我展示了一张图片,不仅指针"指向"它们存储的某个地方(地址(;还有君姆。请有人告诉我"ASM JUMP指令和C/C++中的指针之间的主要区别是什么"。谢谢大家。

指针用于存储变量的地址。

处理器使用 ASM JUMP 从地址开始执行代码。

我认为没有任何相关的理由来区分两者,因为它们都是不同的概念并且用于不同的原因。

它们并不相似,指针只是地址,由于抽象,它们在 c/c++ 中可能看起来很神秘,但在汇编中,指针可能是一个寄存器,其中存储了一个地址,"指向"某些数据,EIP 是在程序执行期间"指向"当前指令的指针,整个想法是复制指向数据的地址比复制数据本身"更便宜"。

我认为混淆来自这样一个事实,即c/c ++是强类型语言,汇编是松散类型的。

这篇关于强类型和弱类型的维基文章解释了这一切,它有关于指针的说法。

指针

某些编程语言公开指针,就好像它们是数值一样,并允许用户对它们执行算术运算。这些语言有时被称为"弱类型",因为指针算法可用于绕过语言的类型系统。

即使这样说,如果你在指针上阅读本教程,它说;

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

这两个表达式是等价的,当你考虑它时,这是有意义的。

a只是程序集中数组的指针,您可能大致像这样;

.data
   a db  "some data"

.data是指向数据所在的地址的指针 a: 也是一个指向地址的指针,该地址在程序中标签 a 所在的位置开始,就在定义字节之前"some data"'s'就像 c 中的指针 a 一样;

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

访问它们看起来像;

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

指着一个样子;

char *b = a;

访问在程序集中如下所示;

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)

指向或获取程序集中的地址如下所示;

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

组装中,一切都暴露在外。


我想为你做一些额外的调查,我做了这个简单的 c 程序,叫做 test.c;

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

并通过gcc gcc -S -masm=intel -fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm test.c馈送,以获得test.s相当于test.c这是文件的程序集;

    .file   "test.c"
    .intel_syntax noprefix
    .def    ___main;    .scl    2;  .type   32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "some data"
    .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"

请注意这些部分;

LC0:
    .ascii "some data"

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

看起来这个程序正在使用堆栈来存储它的指针,esp堆栈指针,它随时包含指向堆栈顶部的地址或指针。

指针1

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

指针2

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