News:

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

Create buffer divisible by 4

Started by Gunner, April 27, 2011, 12:22:56 AM

Previous topic - Next topic

Gunner

By the time I get to coding, I am tired after a long day so the answer is not coming to me...

How would I create a buffer that is divisible by 4?  Or would this be unnecessary?  I am thinking for alignment that buffers should be divisible by 4 right?

I have variable length strings that I get the lengths of and add em together and create a buffer with HeapAlloc, lets say at one time the length might be 49, is there an equation or some math I could use to figure out it should be made 52? if it is say 56, then we are good to go... etc...
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

        mov     edx,StringLength
        add     edx,4                  ;add 1 for terminator and 3 for mod function
        and     edx,-4                 ;=0FFFFFFFCh
;edx = buffer length


i prefer not to use EAX for these because you get shorter code with any other register
EAX does not have the shortened forms for signed byte add, sub, etc

of course, you can do it with an EQU if the length is known in advance

Gunner

Nice, just what I was looking for... Thanks!

The lengths aren't known before hand...
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

if you don't count the terminator...

X = (N + M - 1) and (- M)

X = result size
N = minimum size
M = alignment size

qWord

The memory returned by HeapAlloc() and GlobalAlloc() is allways 8 byte aligned.
FPU in a trice: SmplMath
It's that simple!

Antariy

Quote from: dedndave on April 27, 2011, 12:42:24 AM
i prefer not to use EAX for these because you get shorter code with any other register
EAX does not have the shortened forms for signed byte add, sub, etc

AFAIK, ML produces short opcode form for the short immediates, i.e., for ADD EAX,4 this would be db 83h,0C0h,04h, and 83h, 0E0h, 0FCh for the AND EAX,-4

dedndave

as usual, you're right, Alex   :P
another bad habit i have to break from the 8086 days

FORTRANS

Hi,

   It works the same for the 8086.  Abstracted from a TASM
reference.

83 /0 ib   ADD r/m16,imm8    Add a sign-extended immediate
                             byte to r/m word


Regards,

Steve N.

dedndave

huh ?
what the hell am i thinking - lol
i was pretty sure that
        ADD     AX,8
was 3 bytes long
and that
        ADD     DX,8
was 2
now you guys are just picking on me because i am getting old - lol

mineiro


        mov     edx,StringLength
        and      edx,03h
        jz   align_4
       not dl
       inc dl
       and dl,03h
        add       edx,StringLenght
        align_4:
;edx = buffer length



FORTRANS

Hi,

   Minor question.  Or brainfart?


       not dl
       inc dl

; Is that not the same result as...

        NEG     DL      ; except for carry flag?


Regards,

Steve

jj2007

Me I am so used to the memalign opcode :bg
include \masm32\include\masm32rt.inc

.code
Dummy db 123
txDave db "Hi, Dave!", 0
AppName db "Masm32:", 0

start: mov esi, offset txDave
memalign esi, 4
MsgBox 0, esi, addr AppName, MB_OK
exit

end start

dedndave

        mov     edx,StringLength
        and      edx,03h
        jz   align_4
       not dl
       inc dl
       and dl,03h
        add       edx,StringLenght
        align_4:
;edx = buffer length

       not dl
       inc dl

; Is that not the same result as...

        NEG     DL      ; except for carry flag?

even so - that is a lot of code - ouch !
i like...
       add     edx,3
       and     edx,-4

a lot better

dedndave

memalign macro from masm32   :bg
      memalign MACRO reg, number
        add reg, number - 1
        and reg, -number
      ENDM


at least i got one thing right, today
i feel like my mind is slipping farther and farther every day around you guys

well, i used to be pretty sharp   :lol
i was also good lookin, but i don't remember what that was like, either

mineiro

Yes, neg works, maybe I need sleep a bit hehehe.

align2 proc len:dword,multiple_of_2:dword
;to do: need check if multiple of 2 is so high to fit in this dword
push ecx
mov     edx,len
mov ecx,multiple_of_2
dec ecx
and      edx,ecx
neg edx
and edx,ecx
add edx,len
pop ecx
ret
align2 endp