NASM Forum > Programming with NASM
Structure Size member
munair:
Some NASM examples I found put 'size:' as a final member of a structure and use it to obtain the structure's size. Is this correct?
The following code is compiler generated and NASM has no trouble compiling it:
--- Code: ---struc _D27
_D27_fname: resd 1
_D27_sname: resd 1
_D27_age: resd 1
size:
endstruc
SECTION .text
global _start
global _end
_start:
mov eax, [_I0 + _D27_age]
_end:
mov ebx, 0
mov eax, 1
int 80h
SECTION .data
_I0: istruc _D27
at _D27_fname, dd 0
at _D27_sname, dd 0
at _D27_age, dd 0
iend
--- End code ---
If the 'size:' member is correct, can it also be prepended, or is the name 'size' default for NASM to be recognized?
Frank Kotler:
I THINK that Nasm defaults to "_D27_len" for the size of the structure. Check me on that, though.
Best,
Frank
munair:
That would be _D27_size, which doesn't require the 'size' member (_D27_len does not compile).
munair:
Hmm, it looks like I figured it out. The following code gives me the size:
--- Code: ---struc _D27
_D27_fname: resd 1
_D27_sname: resd 1
_D27_age: resd 1
endstruc
SECTION .text
global _start
global _end
_start:
mov eax, _D27_size
; printing eax outputs 12
_end:
mov ebx, 0
mov eax, 1
int 80h
--- End code ---
BTW, what happens here:
--- Code: ---mov eax, [_D27_size]
--- End code ---
That's what I tried first but it causes a segmentation fault when trying to print eax to the standard output device.
debs3759:
That would treat _D27_size as a pointer and would load eax with whatever is at the memory address it points to.
Navigation
[0] Message Index
[#] Next page
Go to full version