flat assembler
Message board for the users of flat assembler.
Index
> Windows > align by 4 
Author 

quirck
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. 

22 Jul 2018, 07:48 

Ali.Z
perfect, so perfect thanks man.
edit: i misunderstood, but now i do thats why edited. _________________ Asm For Wise Humans Last edited by Ali.Z on 22 Jul 2018, 09:18; edited 2 times in total 

22 Jul 2018, 09:10 

quirck
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? 

22 Jul 2018, 09:14 

Ali.Z
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 

22 Jul 2018, 09:19 

revolution
You can use constants directly in x86 code.
Code: add eax,3 and eax,4 

22 Jul 2018, 10:10 

Ali.Z
yeah, but its part of my asm style.
i like to play with registers more than ... yeah thanks. 

22 Jul 2018, 10:47 

< Last Thread  Next Thread > 
Forum Rules:

Copyright © 19992020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.