Author Topic: Dynamic allocations and structurs  (Read 12305 times)

Offline azagaros

  • Jr. Member
  • *
  • Posts: 14
Dynamic allocations and structurs
« on: June 06, 2018, 06:28:12 PM »
Puzzling over math..

Code: [Select]
mov r11, [pPtrCont + ptrCont.m_pOutputFile]
mov r15, errorHaveOutput
cmp r11, 0
jne .error
; Allocate a ile type
xor rax, rax ; this does not matter because we have procees operator of the argument
mov rdi, nfiletypeSize
call malloc
mov r15, 3
cmp rax, 0
je .error
; we have the block in rax
mov [pPtrCont + ptrCont.m_pOutputFile], rax  ; store the pointer ::::syntax error here... for pointer math...


How do I do it other wise... I am just dealing with integers.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Dynamic allocations and structurs
« Reply #1 on: June 06, 2018, 07:25:39 PM »
I think you may need to show us how "pPtrCont", "ptrCont", etc. are defined. I suspect you may need a "+" in there instead of a ".".
Do I understand that you get a syntax error on the last line but not on the first line? That seems very strange!

Best,
Frank


Offline azagaros

  • Jr. Member
  • *
  • Posts: 14
Re: Dynamic allocations and structurs
« Reply #2 on: June 06, 2018, 08:19:04 PM »
All I want is to map the structure on the data the pointer points to.
Unless I have done this wrong it is relative position of the pointer plus the offset of the structure position
Code: [Select]
pPtrCont resb 8 ; pointer to the container.

struc ptrCont ;;; this stuff is possessor stuff and should not create code and is good for math
.m_pFileListHead         resb 8
.m_pOutputFile resb 8
.m_pIncDirListHead resb 8
endstruc
nPtrContSize equ 24

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Dynamic allocations and structurs
« Reply #3 on: June 06, 2018, 09:08:30 PM »
Hmmmm... Nasm's "struc" is just a "typedef". It does not reserve any memory. I think you're going to want to create an "instance" of your structure.

Code: [Select]
;pPtrCont resb 8 ; pointer to the container.

struc ptrCont ;;; this stuff is possessor stuff and should not create code and is good for math
  .m_pFileListHead         resb 8
  .m_pOutputFile resb 8
  .m_pIncDirListHead resb 8
endstruc
nPtrContSize equ 24
; Nasm calculates this as prtCont_size - should be the same

section .bss
; an instance of your structure
    my_ptrCont resb ptrCont_size ; or nPtrContSize - uninitiallized

section .data
; address of your instance
    pPrtCont dq my_ptrCont

section .text
; maybe need to adjust stack?

mov r11, [pPtrCont + ptrCont .m_pOutputFile]
mov r15, errorHaveOutput
cmp r11, 0
jne .error
; Allocate a ile type
xor rax, rax ; this does not matter because we have procees operator of the argument
mov rdi, nfiletypeSize
call malloc
mov r15, 3
cmp rax, 0
je .error
; we have the block in rax
mov [pPtrCont + ptrCont .m_pOutputFile], rax  ; store the pointer ::::syntax error here... for pointer math...

That's untested - always a bad idea. I'll try it if I get to it...

Nasm's syntax for "struc" is similar to C's... but not quite the same. Potentially confusing!

Best,
Frank


Offline azagaros

  • Jr. Member
  • *
  • Posts: 14
Re: Dynamic allocations and structurs
« Reply #4 on: June 07, 2018, 01:48:40 AM »
That does not look dynamic.  The statement -- "uninitialized" is vague, especially in the section of data.  This thing is not to take up memory space at the start of the program, the entire intent of dynamic allocations. 

There is a malloc for it in the code. If it is like a typedef in C, I can essentially do defStruc *mptrstruct = NULL;  and you would have to do the void* malloc(nsize) to get the memory allocation and that structure with mptrstuct = malloc(sizeof(mptstruct)).  Any time is use the pointer I have access to the structure map.  Assembly should be no different though it is done with the processor with that structure.  It contributes to the pointer math relative address plus offset.  In my code
Code: [Select]
section .bss
pPtrCont resb 8 ;null pointer once set to zero, non-zero it is location in memory.

section .text
;after the call to malloc, somewhere, to put on the heap
      mov rax, [ [pPtrCont] + ptrCont.m_pOutputFile]  ;with in the type def for byte in the block of data at byte 9 with the 8 bytes that make it up, where ever that thing is is memory
; the compiler is arguing with me again in its "limitation" because that is a syntax error: expecting a comma or eol.
; one pass it will assemble and another it won't.  I had not seen the size member talked about in the docs
; all it is pointer math, with convenient concepts. It is math with addresses.
; the work around looks like
      mov rax, [pPtrCont]
      add rax, 8
      mov [rax], mydata

Shawn
« Last Edit: June 07, 2018, 02:09:00 AM by azagaros »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Dynamic allocations and structurs
« Reply #5 on: June 07, 2018, 03:36:20 AM »
Hi Shawn,
I'm not sure I understand what you're trying to do...
Code: [Select]
;after the call to malloc, somewhere, to put on the heap
      mov rax, [ [pPtrCont] + ptrCont.m_pOutputFile]  ;with in the type def for byte in the block of data at byte 9 with the 8 bytes that make it up, where ever that thing is is memory
You call malloc... it returns a memory address in rax (no?). Then you overwrite rax with... well. a syntax error - nested square brackets aren't going to work.

Originally you showed me:
Code: [Select]
mov rdi, nfiletypeSize
call malloc
I don't know what "nfiletypesize" is supposed to be (neither does Nasm). Are you trying to malloc memory for an instance of your structure?
Code: [Select]
mov rdi, ptrCont_size ; or your nPtrContSize - 24 in any case
call malloc
mov [pPtrCont], rax
(these CamelCaps are drivin' me ape!) I (mis?)understood that you were only malloc-ing memory for one element of your structure.

Your workaround...
Code: [Select]
mov rax, [pPtrCont]
      add rax, 8
      mov [rax], mydata
"should" be about the same as...
Code: [Select]
mov [pPtrCont + ptrCont.m_pOutputfile], mydata
... if I understand what you're trying to do... which I may not...

Try the docs again
https://www.nasm.us/docs/2.14rc0/nasmdoc4.html#section-4.11.10
but it may not help.

Best,
Frank