访问结构成员 NASM 程序集

Accessing struc members NASM Assembly

来自 Python 和 Java 等面向对象的语言,为什么这段代码不能像我希望的那样工作?

我想访问 cat struc 变量 cat_name 并将其发送到 STDOUT 以在我的终端中打印。

catstruct.asm:

SECTION .bss
    struc cat
        cat_name: resb 8
    endstruc

SECTION .data
catStruc:
    istruc  cat
        at cat_name, db "Garfield" 
    iend


SECTION .text
GLOBAL  _start

_start:
    mov     edx, 8
    mov     ecx, cat_name
    mov     ebx, 1
    mov     eax, 4
    int     0x80

    mov     ebx, 0
    mov     eax, 1
    int     0x80

代码汇编时没有错误,但是,当我运行它时它不打印任何东西。怎么会?

cat_name只包含0,猫名从结构开始的偏移量,你需要

mov     ecx, catStruct+cat_name

引自手册

For example, to define a structure called mytype containing a longword, a word, a byte and a string of bytes, you might code

struc   mytype 

  mt_long:      resd    1 
  mt_word:      resw    1 
  mt_byte:      resb    1 
  mt_str:       resb    32 

endstruc

The above code defines six symbols: mt_long as 0 (the offset from the beginning of a mytype structure to the longword field), mt_word as 4, mt_byte as 6, mt_str as 7, mytype_size as 39, and mytype itself as zero.