ASM JUMP指令和C/C++中的指针之间的主要区别是什么?
what is the main difference between ASM JUMP instruction and the Pointer in C/C++
我是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
- 在 const 函数中通过引用和指针返回之间的区别
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- AVX 指令中寄存器和指针之间的客观差异
- 禁止指针和整数之间的比较C++
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 介于 [固定数组] 和 [带内存分配的指针] 之间的性能
- ISO C++禁止指针和整数 [-fpermissive] [c++] 之间的比较
- 类指针之间的 Qt 差异
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 指向 POD 类型的指针之间的静态转换与重新解释转换
- 在两个.cpp文件之间定义全局类/结构指针
- 按值和引用传递指针之间的区别?
- 在硬件SIMD矢量指针和相应类型之间进行"interpret_cast"是一种未定义的行为吗
- 在C++中的程序之间共享指针
- 指针和程序性能之间有什么关系吗?
- Qt 错误 iso c++ 禁止指针和整数之间的比较 -permissive
- 可以在指向不同模板类实例的指针之间进行强制转换吗
- C++17:unique_ptr<char[]> 和 shared_ptr<char[] 之间的指针存储差异>
- 错误:ISO C++禁止在指针和整数 [-fallowive] 之间进行比较