Archive: Sections.nsh error??


Sections.nsh error??
Should the Sections.nsh macro below:

; Set one or more BITS in SECTION's flags.
!macro SetSectionFlag SECTION BITS
Push $R0
SectionGetFlags "${SECTION}" $R0
IntOp $R0 $R0 | "${BITS}"
SectionSetFlags "${SECTION}" $R0
Pop $R0
!macroend

Actually be written as:
; Set one or more BITS in SECTION's flags.
!macro SetSectionFlag SECTION BITS
Push $R0
SectionGetFlags "${SECTION}" $R0
IntOp $R0 $R0 & "${BITS}"
SectionSetFlags "${SECTION}" $R0
Pop $R0
!macroend

Notice the "&" in the second version...as opposed to the "|" in the first version.


Sections.nsh is correct when it uses the '|' operator to set bits in a register.

For simplicity I'll assume registers are 4-bit values (bit 3, bit 2, bit 1, and bit 0).

Suppose the register $9 has bits 0 and 2 set, and you want to set another bit without affecting bits 0 and 2.

The binary representation of $9 is 0101 (bits 0 and 2 set, equivalent to integer value 5)

Assume register $8 has only bit 1 set in it, so it has binary value 0010 (equivalent to integer value 2).

The instruction

IntOp $9 $9 | $8

will "or" the binary values 0101 and 0010 together and give the result 0111 (integer 7). This is the correct answer.

The instruction

IntOp $9 $9 & $8

will "and" the binary values 0101 and 0010 together and give the result 0000 (integer 0). This is the wrong answer.

The '|' (BINARY OR) and '&' (BINARY AND) operators work like this:

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

I hope this makes things clearer ;)


I understand your examples...and they make sense to me. However, if you want to use the SECTION_OFF variable, then you must have an "&" instead of "|". In other words, you need this to happen (using your example above):

0 & 0 = 0
0 & 1 = 0
0 & 0 = 0
0 & 1 = 0

However, I looked in Sections.nsh a little closer and there is this macro below...which is exactly what I need:
!macro UnselectSection SECTION
Push $0
SectionGetFlags "${SECTION}" $0
IntOp $0 $0 & ${SECTION_OFF}
SectionSetFlags "${SECTION}" $0
Pop $0
!macroend

Thanks for your clear and concise reply! That helped a lot!


You may want to check the ^ XOR operator (I needed it in one of my scripts)