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

2024-02-21

我最近开始在 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 main

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Nasm - 按值和地址访问结构元素 的相关文章

  • 在 Intel x86 架构上使用非 AVX 指令移动 xmm 整数寄存器值

    我有以下问题 需要使用 AVX2 以外的任何工具来解决 我有 3 个值存储在 m128i 变量中 不需要第四个值 需要将这些值移动 4 3 5 我需要两个功能 一个用于按这些值进行右逻辑移位 另一个用于左逻辑移位 有谁知道使用 SSE AV
  • Android NDK 代码中的 SIGILL

    我在市场上有一个 NDK 应用程序 并获得了有关以下内容的本机崩溃报告 SIGILL信号 我使用 Google Breakpad 生成本机崩溃报告 以下是详细信息 我的应用程序是为armeabi v7a with霓虹灯支持 它在 NVIDI
  • 如何在函数内 malloc 结构体数组?代码以其他方式工作

    我正在尝试创建一个函数来创建可变大小的二维函数数组 我正在使用以下代码 它本身似乎工作得很好 typedef struct Starter Properties int TypeB int TypeF int TypeW Randomize
  • 从 DX:AX 寄存器转移到单个 32 位寄存器

    我在添加 16 位乘法的乘积时遇到问题 我想将一年 例如 2015 年 乘以 365 为此 我 mov dx 0 to clear the register mov ax cx cx holds the year such as 2015
  • ICC 中的 -O3 会扰乱内在函数,使用 -O1 或 -O2 或相应的手动汇编即可

    这是后续这个问题 http stackoverflow com questions 49791664 o2 in icc messes up assembler fine with o1 in icc and all optimizatio
  • 是否有适用于双打 (__m128d) 的 Move (_mm_move_ss) 和 Set (_mm_set_ss) 内在函数?

    多年来 我有几次看到 in 中的内在函数float参数被转换为 m128使用以下代码 m128 b mm move ss m mm set ss a 例如 void MyFunction float y m128 a mm move ss
  • 将数组显式衰减为指针

    最简洁 最惯用的方式是什么明确地将数组衰减为指针 例如 考虑您需要能够指导 SFINAE 或明确过载的情况 template
  • OOP 中的对象和结构有什么区别?

    对象与结构体有何区别 我们何时以及为何使用对象而不是结构体 数组与两者有何不同 何时以及为何使用数组而不是对象或结构 我想了解每个的用途 显然 您可以根据您的编程风格模糊这些区别 但通常结构是结构化的数据块 对象是可以执行某种任务的主权实体
  • 比“add esp, 4”更小的指令

    又是我 我的程序中有很多 add esp 4 我正在尝试减小它的大小 是否有任何更小的指令可以替代 add esp 4 pop edx 或者您不介意破坏的任何其他整数寄存器 这就是现代编译器实际上所做的 https stackoverflo
  • 指针的 Fortran 副本

    我有一个包含指针 p 的 var 类型 我需要将 var 复制到与 var 类型相同的另一个变量 var1 上 通过执行 var1 var 在引号中 因为我不知道这是否是正确的方法 请参见下文 在我的实现中 var 和 var1 被传递给一
  • 为什么C++中没有“NULL引用”?

    我正在阅读 C 常见问题解答 8 6 什么时候应该使用引用 什么时候应该使用指针 http www parashift com c faq lite refs vs ptrs html 特别是以下声明 可以时使用引用 必要时使用指针 上述情
  • intfmt: db "%d", 10, 0 在汇编中的含义

    我最近在我的一个汇编文件的顶部看到了这个 并意识到我在打印整数的过程中花了很长时间使用它 而没有真正意识到它最初来自哪里 在我的基本汇编模板中使用 或 10 0 是什么结尾的意思是 section data intfmt db d 10 0
  • 返回具有关联类型的特征

    struct A struct PropA struct B struct PropB trait AB type prop fn a self gt fn b self p Self prop gt impl AB for A type
  • 对结构体进行 typedef 对其自身有什么影响? [复制]

    这个问题在这里已经有答案了 我在 API 顶部看到过这样的代码 typedef struct SYSTEM SYSTEM 方式SYSTEM之前是未定义的 有谁知道这是做什么的 编译器认为什么SYSTEM这条线之后 感谢您的回答 我的问题是
  • 在 C++ 中检查空指针的首选方法是什么?

    选项A if NULL pSomethingColumn Yes we use Yoda conditions if NULL pSomethingColumn Or if pSomethingColumn if pSomethingCol
  • 为什么 LED 保持亮起而不是闪烁?

    这是使用 pic16f676 中的 TIMER0 中断使 LED 闪烁的 MPASM 代码 端口 A 的引脚 0 RA0 未切换至关闭位置 请帮忙 我是图片组装的新手 我想掌握图片 有没有高手帮我学习一下 我需要以 1 秒的间隔眨眼 代码是
  • 寻找一种简单的方法来重新初始化结构

    我有一个名为 CoolStruct 的结构 struct CoolStruct int id uint32 type uint32 subtype String name 我也有这些结构的向量 std vector
  • 汇编语言程序中连续两次相乘

    我正在使用 8086 模拟器以及 DOSBOX 和 MASM 我知道当我们将 8 位与 8 位相乘时 答案将是 16 位 al 8 bit ax 当我们将 16 位与 16 位相乘时 答案将是 32 位 ax 16 bit dx ax 但如
  • 测试 xmm/ymm 寄存器是否为零的更快方法?

    It s fortunate that PTEST does not affect the carry flag but only sets the rather awkward ZF also affects both CF and ZF
  • INT 13h 无法读取超出特定扇区的数据

    我正在为我的操作系统编写内核 在将磁盘扇区加载到内存时遇到问题 以下是从磁盘加载扇区的函数代码部分 mov ax 0x3000 mov es ax mov ax 0x0201 mov bx word ptr bp 6 bx 0x000 0x

随机推荐