Author Topic: struc based on another struc (inheritance)  (Read 1301 times)

Offline Kazu

  • Jr. Member
  • *
  • Posts: 3
struc based on another struc (inheritance)
« on: April 15, 2017, 11:42:15 PM »
Hi,
I've got a struc, call it A, and I want to base 3 other struc's on A.
So for instance B will have all the elements and labels of A, plus one more.
C will have all the elements and labels of A, plus 2 more.
D will have all the elements and labels of A, plus 3 more.
I don't want to have to separately define B, C, and D repeating all of A's elements and labels.
Is there such a thing as saying
struc A
 .label1 resw 1
 .label2 resw 1
 .label3 resb 1
endstruc
struc B : A
   .foo resb 1
endstruc
Etc...
Such that I can refer to:
   mov rsi, (address of a B struc)
   movsx rax, byte [rsi + B.label3]

If NASM doesn't have this, does YASM?
« Last Edit: April 15, 2017, 11:46:08 PM by Kazu »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2364
  • Country: us
Re: struc based on another struc (inheritance)
« Reply #1 on: April 16, 2017, 08:40:45 AM »
Hi Kazu,
Welcome to the Forum.

Dunno what Yasm's got these days. This may not be exactly what you want.
Code: [Select]
; inheritance?
; nasm -f elf32 myprog.asm
; ld -o myprog muprog.o -m elf_i386

struc A
    .label1 resw 1
    .label2 resw 1
    .label3 resb 1
endstruc

struc B
    .A resb A_size
    .foo resd 1
endstruc

struc C
    .A resb A_size
    .foo resd 1
    .bar resd 1
endstruc

struc D
    .A resb A_size
    .foo resd 1
    .bar resd 1
    .baz resd 1
endstruc

section .bss
    ; uninitialized, but allocated
    my_D resb D_size ; not used

section .data
    my_B istruc B
at B.A, dw 1000
        dw 1000
db 42
at B.foo, dd 100

section .text
    global _start
    _start:
    mov esi, my_B
    movzx ebx, byte [esi + B.A + A.label3]
    mov eax, 1
    int 80h
; "echo $?" to see exitcode

You might also want to look into the structures that the NASMX package provides.

Best,
Frank


Offline Kazu

  • Jr. Member
  • *
  • Posts: 3
Re: struc based on another struc (inheritance)
« Reply #2 on: April 17, 2017, 03:55:27 PM »
Hi Frank,
My point was that I'd like to have B also get A's symbols. Is that possible?
Can the macro for struc be updated to allow that?
Thanks.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2364
  • Country: us
Re: struc based on another struc (inheritance)
« Reply #3 on: April 17, 2017, 05:20:50 PM »
B does have A's symbols - A.label1, A.label2, and A.label3. But they're in B. Of course, we have to "say" they're in B. Or do you want Nasm to just "know" that? (unlikely!) The value of "A.B" is zero - you could leave it out if you wanted to. It would be less clear, IMHO.

Not likely the macro(s) built into Nasm are going to be changed. You can write your own macros, of course, if you can find anything that works more like what you want. Again, I refer you to the NASMX package.

Does Yasm do what you want?

Best,
Frank


Offline Kazu

  • Jr. Member
  • *
  • Posts: 3
Re: struc based on another struc (inheritance)
« Reply #4 on: April 17, 2017, 11:22:20 PM »
Does Yasm do what you want?

Yasm just tried to imitate Nasm AFAIK.