我在ASM和C/C++方面是新手。许多网站向我展示了一张图片,不仅指针“指向”它们存储的某个地方(地址),而且还有跳转。请问有人能告诉我“ASM跳转指令和C/C++中指针的主要区别是什么”。谢谢大家。
我在ASM和C/C++方面是新手。许多网站向我展示了一张图片,不仅指针“指向”它们存储的某个地方(地址),而且还有跳转。请问有人能告诉我“ASM跳转指令和C/C++中指针的主要区别是什么”。谢谢大家。
指针用于存储变量的地址。
处理器使用 ASM JUMP 来从地址开始执行代码。
我认为没有任何相关的理由来区分这两个概念,因为它们都是不同的概念,用于不同的原因。
a[5] = 0; // a [offset of 5] = 0
*(a+5) = 0; // pointed by (a+5) = 0
a
只是指向一个数组的指针,你可能会有类似于以下的东西;.data
a db "some data"
.data
是指向数据所在地址的指针。a:
也是一个指针,指向程序中定义字节's'
之前标签a
所在的地址,就像c语言中的指针一样。
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
int main(){
char *pointer1 = "some data";
char *pointer2 = pointer1;
}
运行gcc
命令将test.c
文件转换为汇编语言,命令为:gcc -S -masm=intel -fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm test.c
,生成的汇编文件是test.s
。
.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"
LC0:
.ascii "some data\0"
并且
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
gdb(即GNU调试器)
。我使用的语言是Windows:MASM,NASM
和Linux:NASM
。 - James