Nasm - 按值和地址访问结构元素 [英] Nasm - access struct elements by value and by address

查看:60
本文介绍了Nasm - 按值和地址访问结构元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started to code in NASM assembly lately and my problem is that I don't know how I access struct elements the right way. I already searched for solutions on this site and on google but everywhere I look people say different things. My program is crashing and I have the feeling the problem lies in accessing the structs.

查看示例代码时:

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>

I dont know if you figured out but here is our code with some little modification that works. All instructions are correct except the last one mov ebx, [eax] which is expected caus you are trying to access content at address 0x1234567 resulting in 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主

这篇关于Nasm - 按值和地址访问结构元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆