flat assembler
Message board for the users of flat assembler.

 Index > Compiler Internals > BSR operator with negative constants
Author
CandyMan

Joined: 04 Sep 2009
Posts: 384
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 14 Dec 2022, 10:16
Code:
```A = bsr(-1)
processed: A=bsr(-1)
error: value out of range.    ```
Code:
```macro IsPower V
{
A = bsf(V)
B = bsr(V)
if A=B
display 'IsPowerOf2'
end if
}

V = -1

IsPower V    ```

_________________
smaller is better
14 Dec 2022, 10:16
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8132
Location: Kraków, Poland
Tomasz Grysztar 14 Dec 2022, 11:26
CandyMan wrote:
Code:
```A = bsr(-1)
processed: A=bsr(-1)
error: value out of range.    ```
What is the highest bit of -1 representation depends on how many bits you have. For the sake of universality, fasm gives results of calculations that are consistent with an unbounded number of bits (in the manual I also define this as computation on 2-adic numbers, see my video series for detailed explanation), and BSR of -1 is then meaningless, because infinite series of ones does not have the highest one.

If you know your maximum size, just mask it out before the calculation:
Code:
`A = bsr((-1) and 0FFFFFFFFFFFFFFFFh)    `

In case of fasmg, you can use mask of any length (making bsr(-1) larger an larger).

CandyMan wrote:
Code:
```macro IsPower V
{
A = bsf(V)
B = bsr(V)
if A=B
display 'IsPowerOf2'
end if
}

V = -1

IsPower V    ```
There are other options:
Code:
```macro IsPower V
{
if V=1 shl bsf(V)
display 'IsPowerOf2'
end if
}    ```
14 Dec 2022, 11:26
CandyMan

Joined: 04 Sep 2009
Posts: 384
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 14 Dec 2022, 11:49
I understood. Thanks for the explanation.
14 Dec 2022, 11:49
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum