gpt4 book ai didi

pointers - Nasm - 按值和地址访问结构元素

转载 作者:行者123 更新时间:2023-12-05 05:09:27 28 4
gpt4 key购买 nike

我最近开始使用 NASM 程序集编写代码,但我的问题是我不知道如何以正确的方式访问结构元素。我已经在这个网站和谷歌上搜索了解决方案,但我看到到处都有人说不同的话。我的程序崩溃了,我感觉问题出在访问结构上。

查看示例代码时:

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 主程序

关于pointers - Nasm - 按值和地址访问结构元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540758/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com