I am experiencing difficulty when attempting to use the C library function strcpy within an external NASM function designed to create a linked list of items. The function is designed to malloc nodes until a specific number of nodes is reached and control returns to the calling function. The node itself contains a nested structure which is also malloc()ed and contains two string/arrays of chars.


The strange thing is that the first invokation of strcpy works just as it should but the second instance, which has exactly the same except for the fact that it copies a different string to a different data member in the structure, I receive an "error: invalid operand type" error even though the syntax appears exactly the same. Does anyone have any suggestions


** I tried to only post the code that seemed necessary but will post more if required. I think this should suffice since the linked list functionality works fine and the name and address strings do take in information from the keyboard correctly. Its just copying the one addressString to the malloced struct.




section .bss

nameString                RESB 80
addressString            RESB 80

STRUC linkedList
  prev:            RESD    1   
  next:            RESD    1   
  m_empNode:        RESD    1   

ENDSTRUC ; end of struc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

STRUC employee
  m_empName:          RESB    80
  m_empAddress:        RESB    80

ENDSTRUC ; end of struc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; break into code after initialization of list and retrieving input data

    ; restore ECX register pointing to malloced EmployeeNode
      POP      ECX          ; ECX =

      ; restore EBX register pointing to malloced ListNode
      POP      EBX

      ; if the headNode points to a null value, perform insert at the head
      CMP      dword , NULL

      ; otherwise, jump to insert at tail process
      JNE      insertTail


insertHead:

    ; copy name string to stuNode
      PUSH    dword nameString
      PUSH    dword ECX + m_empName
      CALL    strcpy
      ADD      ESP, 8

      POP      EDX

    ;; copy address string to employee node       
      PUSH  dword addressString
      PUSH  dword EDX + m_empAddress <-----;;;;; THIS INSTRUCTION PRODUCES error: invalid operand type
      CALL  strcpy                                                ;;;;; even though strcpy above it has a similar line and works fine
      ADD  ESP, 8


      ; put address of new node as headNode
      MOV      dword , EBX     

      ; insert is complete
      JMP      insertComplete


insertTail:

      MOV      EDX, ECX

      PUSH    EDX

      PUSH    dword nameString
      PUSH    dword ECX + m_empName
      CALL    strcpy
      ADD      ESP, 8

      POP      EDX

      PUSH    dword addressString
      PUSH    dword EDX + m_empAddress<-----;;;;; THIS INSTRUCTION PRODUCES error: invalid operand type
      CALL  strcpy                                                ;;;;; even though strcpy above it has a similar line and works fine
      ADD      ESP, 8
[\CODE]
Posted on 2006-10-23 20:30:48 by droors
Hm, I find it weird that NASM allows "PUSH dword ECX + m_empName" - that's not a valid x86 adressing mode. If you add square brackets to indicate a memory reference, then it's valid enough, but not what you want.

What you'll want is probably something like "lea eax, " along with a "push eax". Also remember that any external function call is free  to trash EAX,ECX,EDX so you should either preserve those across external calls, or (usually smarter) used EBX,ESI,EDI,EBP instead.

Same goes for code of your own, if you're writing a callback: you're free to trash EAX,ECX,EDX but you must preserve the other four registers.
Posted on 2006-10-24 01:26:16 by f0dder
f0dder:

Thank you very much for your assistance and sorry for any inconvenience.  Your suggestion seems to have fixed the problem.


Posted on 2006-10-24 04:40:26 by droors
Don't say sorry - we're here to help. Glad to have been of assistance :)
Posted on 2006-10-24 05:21:44 by f0dder
 PUSH     dword ECX + m_empName

what does this assemble to? push ?
Posted on 2006-10-24 06:34:36 by vid