flat assembler
Message board for the users of flat assembler.

 Index > Main > shr 4 problem,
Author
lazer1

Joined: 24 Jan 2006
Posts: 185
lazer1 06 Jul 2006, 15:05
(I thought I had posted on this but I probably clicked preview and
not submit)

if I compile the following:

Code:
```MEMDEST         equ     20000h

.f      dw      (MEMDEST and 0ffffh), (MEMDEST and (not 0ffffh)) shr 4
```

the binary is just 4 0's 00 00 00 00

shouldnt the above compile to 00 00 00 20 ?
06 Jul 2006, 15:05
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 06 Jul 2006, 15:59
The problem here comes from the fact that when the size of calculated number is word (which is because of DW here), the "not 0FFFFh" is interpreted as 0.

One of the reasons for this is to make things like "DW not 0FFFFh" or "DD not 0FFFFh" work (first makes 0000, second makes FFFF0000) independently on what is the assembler's internal computations size.

Thus NOT inside the expression aimed at 16-bit result negates the 16 bits only (unless the number is already larger than 16 bits, but that's another story). To avoid problem you should do it like:
Code:
`dw (MEMDEST and 0ffffh), (MEMDEST and 0f0000h) shr 4    `
06 Jul 2006, 15:59
lazer1

Joined: 24 Jan 2006
Posts: 185
lazer1 06 Jul 2006, 16:34
Tomasz Grysztar wrote:
The problem here comes from the fact that when the size of calculated number is word (which is because of DW here), the "not 0FFFFh" is interpreted as 0.

One of the reasons for this is to make things like "DW not 0FFFFh" or "DD not 0FFFFh" work (first makes 0000, second makes FFFF0000) independently on what is the assembler's internal computations size.

Thus NOT inside the expression aimed at 16-bit result negates the 16 bits only (unless the number is already larger than 16 bits, but that's another story). To avoid problem you should do it like:
Code:
`dw (MEMDEST and 0ffffh), (MEMDEST and 0f0000h) shr 4    `

what are the main scenarios I need to be careful with for this?

is it just not + shr or are there other places?

I have to check through my code as I often use "not" and "shr",

what happens with say "not 00ffffh" ?
06 Jul 2006, 16:34
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 06 Jul 2006, 16:37
It's only NOT actually, since it's the only operation that may be able to set some bits higher that all of the set bits in the operands (and for this reason has to be limited to the specified amount of bits).
With the other logical operations, like AND or XOR you wouldn't notice a difference whether it was done this way or not, because if the values fit in 16 bits, after ANDing or XORing they also fit in 16 bits.

The whole point here is that when you do a NOT operation, you need to have somehow specified what amount of bits you want to negate.
"dw not 1", "dd not 1" and "dq not 1" all declare different values, as NOT operates on different sizes. In the same way "a=word not 1", "a=dword not 1" and "a=qword not 1" all give different values to "a".

(One more important thing I forgot to document...)

The SHR is not at all related here, it's the "not 0ffffh" in the 16-bit context that gives 0, and thus "MEMDEST and 0" is also just 0. When the shift comes, there's nothing to shift already.

"not 00ffffh" is exactly the same as "not 0ffffh", after going through the parser there's no difference.
06 Jul 2006, 16:37
 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