flat assembler
Message board for the users of flat assembler.

flat assembler > Windows > align by 4

Author
Thread Post new topic Reply to topic
Ali.A



Joined: 08 Jan 2018
Posts: 113
Code:
_write.file:
                        mov edx,esi ; esi = buffer that contains data to write
                        push edx
                        mov eax,edi ; edi = number of chars in that buffer
                        call _file.align
                        call _strlen
                        ... ; some code for winapi writefile    


Code:
_file.align:
                        xor edx,edx
                        mov ecx,04 ; divisor
                        ; EAX = 96h
                        div ecx
                        add edi,edx ; edx = reminder (2) edi = 96h
                        ret ; file is perfectly aligned the result is 98h
                            


however, when edi and/or eax are 91h .. edx contain 1 after DIV
and when "add edi,edx" the result is 92h which is not aligned by 4

i also tried using:
Code:
@@:
shr eax,3 ; when eax=96 the final result is 2 which is great 96h + 2 = 98h
; but if eax = another number i dont get what i want
cmp eax,04
ja @B    


excuse me guys, im poor man and bad in math.

btw i know i could sub by 4, but im looking for faster execution.

_________________
Asm For Wise Humans
Post 22 Jul 2018, 04:54
View user's profile Send private message Reply with quote
quirck



Joined: 18 Aug 2013
Posts: 5
Note that everything aligned by 4 has two least significant bits clear. This leads us to using AND.
Code:
and edi, -4
    

But clearing the low bits rounds the number down, and we presumably want to round up to the nearest multiple of 4. If the original value was already aligned, there is no problem. But if it wasn't, the result needs to be increased by 4. This can be achieved by adding 3 before AND:
Code:
add edi, 3
and edi, -4
    

The addition will produce a carry into bit 2 if and only if the original value was not aligned. And with bits 0 and 1 clear, the carry into bit 2 is effectively the desired increase by 4.

It may help to manually study some cases.
If edi = 0, after addition it becomes 3, after AND the result is 0.
If edi = 1, after addition it becomes 4, after AND the result is 4.
If edi = 2, after addition it becomes 5, after AND the result is 4.
If edi = 3, after addition it becomes 6, after AND the result is 4.
If edi = 4, after addition it becomes 7, after AND the result is 4.
If edi = 5, after addition it becomes 8, after AND the result is 8.
And so on.
Post 22 Jul 2018, 07:48
View user's profile Send private message Reply with quote
Ali.A



Joined: 08 Jan 2018
Posts: 113
perfect, so perfect thanks man.

edit: i misunderstood, but now i do thats why edited.

_________________
Asm For Wise Humans


Last edited by Ali.A on 22 Jul 2018, 09:18; edited 2 times in total
Post 22 Jul 2018, 09:10
View user's profile Send private message Reply with quote
quirck



Joined: 18 Aug 2013
Posts: 5
What result do you want to get for edi = 96h? 98h? With shr you'll get 4Ch.
Or is your intention not just rounding up but computing something else?
Post 22 Jul 2018, 09:14
View user's profile Send private message Reply with quote
Ali.A



Joined: 08 Jan 2018
Posts: 113
no its my bad, for not understanding well enough .. now i have tested your code and it works perfectly.

thanks again.

im very happy, whatever value in eax will be aligned by 4 when i call this function.
Code:
_file.align:
                        mov ecx,03
                        add eax,ecx
                        not ecx
                        and eax,ecx
                        ret     

_________________
Asm For Wise Humans
Post 22 Jul 2018, 09:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15912
Location: SDSS J140821.67+025733.2
You can use constants directly in x86 code.
Code:
add eax,3
and eax,-4    
Post 22 Jul 2018, 10:10
View user's profile Send private message Visit poster's website Reply with quote
Ali.A



Joined: 08 Jan 2018
Posts: 113
yeah, but its part of my asm style.
i like to play with registers more than ... yeah

thanks.
Post 22 Jul 2018, 10:47
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-2018, Tomasz Grysztar.

Powered by rwasa.