flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Separate new strucs from C

Author
Thread Post new topic Reply to topic
donn



Joined: 05 Mar 2010
Posts: 321
donn 21 Dec 2015, 17:10
Hi,

I'm continuing with an attempt to build a Linked List in asm. Also would like it accessible from C.

I'm able to use an asm function to load the List struc with default values. However, when I define a second List in C, and set a struc member, such as isBlankList, it sets the value for both Lists. Is there a way for separate Lists to be defined as needed, from C? What I have so far:


ASM struc definition, C header also has one without default values
Code:
        struc List{
                .firstItem dd ?
                .listSize dd ?
                .index dd ?
                .isBlankList dd 1 ; Used value of 50 when testing as a more unique value
        }
    


ASM newList function
Code:
newList:

        push ebp
        mov ebp, esp

        mov ecx, [ebp+8]
        mov edx, [ebp+12]
        
        push ebx esi edi

        mov eax, .newListLocal  ; Pass newly defined List address?

        pop edi esi ebx

        mov esp, ebp
        pop ebp

        retn 0

        .newListLocal:          ; This may be the problem. It seems to set default values for
                list List       ; one List, but with two, changes are mirrored to each.
    



C Lists being defined, and loaded with default values
Code:
        List *asmList = (List*)newList();  
        
        List *asmList2 = (List*)newList(); 


        asmList->isBlankList = 10; // Value of 10 used during testing as a more unique value.
                                   // After setting this, both asmList and asmList2 have 10 
                                   // for the isBlankList member. Beforehand, they had 1
                                   // or whatever was specified in ASM definition.
    


Thanks a lot


Description:
Download
Filename: List.inc
Filesize: 561 Bytes
Downloaded: 883 Time(s)

Post 21 Dec 2015, 17:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20518
Location: In your JS exploiting you and your system
revolution 21 Dec 2015, 17:59
The code above allocates memory in the code section statically at compile time. Instead you will have to allocate memory for the list on demand (i.e. dynamically). Perhaps use a "alloc" or "HeapAlloc" or something inside the newList function. Alternatively you can have C allocate the memory and pass the pointer to newList.


Last edited by revolution on 21 Dec 2015, 23:32; edited 1 time in total
Post 21 Dec 2015, 17:59
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 21 Dec 2015, 22:56
Thanks a lot. Will go the Kernel32 API route for now.
Post 21 Dec 2015, 22:56
View user's profile Send private message Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 21 Feb 2016, 19:09
Got it, solved for now:

Code:
newList:

        push ebp
        mov ebp, esp

        mov ecx, [ebp+8]
        mov edx, [ebp+12]
        
        push ebx esi edi


        call [GetProcessHeap]
        mov [hHeap], eax

        ;Size of List
        push 000001100b
        ;Set allocated to 0s
        push 000001000b
        push [hHeap]
        call [HeapAlloc] 
        mov [hAllocatedMemory], eax
        
        mov dword ebx, [hAllocatedMemory]

        ;Define a local List, members become initialized since we have a definition (shown below)
        list List

        ;Start moving over the local values. Since struc was used, we can reference them with labels. Also, they are initialized.
        mov dword edx, [list]
        mov dword [ebx], edx

        mov dword edx, [list.index]
        mov dword [ebx+4], edx  

        mov dword edx, [list.isBlankList]
        mov dword [ebx+8], edx  

        ;Return the handle
        mov eax, [hAllocatedMemory]
        
        pop edi esi ebx

        mov esp, ebp
        pop ebp

        retn 0
    


And the struc:

Code:
        struc List{
                .listSize dd 2
                .index dd 1
                .isBlankList dd 50
        }
    


Plenty new to learn going forward, like maybe using $ for sizing and how to mov the local struc memory values before they get wiped out. Rep and some of the strings instructions may solve that regardless of the struc layout, but want to investigate the options.

Thanks again.
Post 21 Feb 2016, 19:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.