Pointers Nasm-按值和地址访问结构元素
最近我开始在NASM程序集中进行代码编写,我的问题是我不知道如何正确地访问结构元素。我已经在这个网站和谷歌上搜索了解决方案,但我看到的每一个地方,人们都在说不同的话。我的程序正在崩溃,我感觉问题在于访问结构 查看示例代码时:Pointers Nasm-按值和地址访问结构元素,pointers,assembly,struct,x86,nasm,Pointers,Assembly,Struct,X86,Nasm,最近我开始在NASM程序集中进行代码编写,我的问题是我不知道如何正确地访问结构元素。我已经在这个网站和谷歌上搜索了解决方案,但我看到的每一个地方,人们都在说不同的话。我的程序正在崩溃,我感觉问题在于访问结构 查看示例代码时: STRUC Test .normalValue RESD 1 .address RESD 1 ENDSTRUC TestStruct: istruc Test at Test.normalValue dd ffff0000h
STRUC Test
.normalValue RESD 1
.address RESD 1
ENDSTRUC
TestStruct:
istruc Test
at Test.normalValue dd ffff0000h
at Test.address dd 01234567h
iend
;Example:
mov eax, TestStruct ; moves pointer to first element to eax
mov eax, [TestStruct] ; moves content of the dereferenced pointer to eax (same as mov eax, ffff0000h)
mov eax, TestStruct
add eax, 4
mov ebx, eax ; moves pointer to the second element (4 because RESD 1)
mov eax, [TestStruct+4] ; moves content of the dereferenced pointer to eax (same as mov eax, 01234567h)
mov ebx, [eax] ; moves content at the address 01234567h to ebx
是这样吗
非常感谢您的帮助我不知道您是否了解,但这是我们的代码,稍作修改即可生效。除最后一条指令
mov ebx[eax]
外,所有指令均正确,这是由于您试图访问地址0x1234567
处的内容导致SIGSEGV
section .bss
struc Test
normalValue RESD 1
address RESD 1
endstruc
section .data
TestStruct:
istruc Test
at normalValue, dd 0xffff0000
at address, dd 0x01234567
iend
section .text
global _start
_start:
mov eax, TestStruct ; moves pointer to first element to eax
mov eax, [TestStruct] ; moves content of the dereferenced pointer to eax same as mov eax, ffff0000h
mov eax, TestStruct
add eax, 4
mov ebx, eax ; moves pointer to the second element 4 because RESD 1
mov eax, [TestStruct+4] ; moves content of the dereferenced pointer to eax same as mov eax, 01234567h
mov ebx, [eax] ; moves content at the address 01234567h to ebx
使用
nasm-f elf64 main.nasm-o main.o逐步编译、链接和运行;ld main.o-o main;gdb main
对我来说似乎是正确的,好吧。。。我想我大概已经找到了这次撞车的原因。好像我没有。无论如何,谢谢..您可以向我们展示不起作用的实际代码。mov-eax,TestStruct
/add-eax,4
起作用,但速度毫无意义。使用mov-eax,TestStruct+4
在组装+链接时进行添加。当所有输入都是集合时间常数时,通常避免运行时数学;没有编译器为您执行常量传播。当然,您也可以执行mov-ebx,TestStruct
/mov-eax,[ebx+4]
来假装您有一个运行时变量指针。