News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

insert and delete in Memory

Started by loki_dre, April 26, 2008, 08:21:20 AM

Previous topic - Next topic

loki_dre

Is it possible to insert or delete a byte in memory allocated using the following method:
        ;ALLOCATE AND LOCK OUR MEMORY
        invoke GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, MEMORYSIZE
        mov hMemory, eax
        invoke GlobalLock, hMemory
        mov pMemory, eax

Alternative methods are welcome.
I would like to avoid moving around chunks memory for just one byte.


donkey

Yes, you simply mov the byte to an address within the bounds of the allocated memory...

mov al, SomeNumber
mov edi, [pMemory]
mov edx, IndexInMemory

mov [edi+edx], al

Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

loki_dre

won't that overwrite a byte?.....i'd like to insert or delete a byte
ie.
"HELLO WORLD"
insert "-" at position [3] yields:
"HELL-O WORLD"
delete at position [2] yields:
"HEL-O WORLD"

evlncrn8

that involves having to shift memory around.. you need to copy the bytes after the insertion point +1 from where they should be etc..

Synfire

Slightly more code but it could be used many times over.. you could create a procedure which converts a string into a linked list, each node containing a single character. Then you could create a procedure to convert from the list back to ASCII/UNICODE as needed. With this, list you could insert, delete, and dynamically resize the "string" as needed. If you found time you could even write procedures which used the linked list itself for recieving user input and producing output directly. This is more of a "final solution" answer though. If you create such a project, you should do so seperate from your current project, then link it in. That way you can reuse it on other projects where you need to do a lot of string manipulation.

donkey

Quote from: loki_dre on April 26, 2008, 09:07:30 AM
won't that overwrite a byte?.....i'd like to insert or delete a byte
ie.
"HELLO WORLD"
insert "-" at position [3] yields:
"HELL-O WORLD"
delete at position [2] yields:
"HEL-O WORLD"


Sorry I assumed you would know that you had to shift the contents before the insertion and after a deletion, I just posted how to mov a byte using a base and index and left the rest to you.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

hutch--

If the memory allocation is not all that large, its probably simpler to allocate another buffer and write the first to the second skipping whatever data is required to be removed. If adding data, allocate the buffer enough larger, write the existing data up to the required insertion point, write in the inserted data then write the rest of the original buffer after it.

Just make sure you calculate the allocated size increase properly.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

Simplest approach is to be generous when heapallocating your memory (Windows doesn't really care about a megabyte more), and then to use RtlMoveMemory to insert a byte.