[PB 5.60] Label arithmetic bug

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

[PB 5.60] Label arithmetic bug

Post by kenmo »

I thought this was posted before, but I don't see it.

Subtracting two label addresses, in argument to Round(), causes strange result.
Subtracting two pointer variables is OK.
You can remove the /4, issue remains.

PB 5.60 x86 Windows 7

Code: Select all

DataSection
  Start:
  Data.a $01, $02, $03, $04    ; 4 bytes (1 long)
  Finish:
EndDataSection

nBytes = (?Finish - ?Start)
;Debug nBytes ; OK (4)

nLongs = nBytes / 4
Debug nLongs ; OK (1)

nLongs = (?Finish - ?Start) / 4
Debug nLongs ; OK (1)

nLongs = Round(nBytes / 4, #PB_Round_Up)
Debug nLongs ; OK (1)

nLongs = Round((?Finish - ?Start) / 4, #PB_Round_Up) ; <------------------------
Debug nLongs ; not OK!

*Start  = ?Start
*Finish = ?Finish

nLongs = Round((*Finish - *Start) / 4, #PB_Round_Up)
Debug nLongs ; OK (1)
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: [PB 5.60] Label arithmetic bug

Post by User_Russian »

Not only Round

Code: Select all

DataSection
  Start:
  Data.a $01, $02, $03, $04    ; 4 bytes (1 long)
  Finish:
EndDataSection

nBytes = (?Finish - ?Start)
;Debug nBytes ; OK (4)

nLongs = nBytes / 4
Debug nLongs ; OK (1)

nLongs = (?Finish - ?Start) / 4
Debug nLongs ; OK (1)

nLongs = Round(nBytes / 4, #PB_Round_Up)
Debug nLongs ; OK (1)

nLongs = Round((?Finish - ?Start), #PB_Round_Up) ; <------------------------
Debug nLongs ; not OK!

nLongs = Int((?Finish - ?Start)) ; <------------------------
Debug nLongs ; not OK!

nLongs = Pow((?Finish - ?Start), 1) ; <------------------------
Debug nLongs ; not OK!

s.s = StrF((?Finish - ?Start)) ; <------------------------
Debug s ; not OK!
This is because of the conversion to a floating-point number.

Code: Select all

DataSection
  Start:
  Data.a $01, $02, $03, $04    ; 4 bytes (1 long)
  Finish:
EndDataSection

x.f=(?Finish - ?Start)
Debug x
Fred
Administrator
Administrator
Posts: 16685
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [PB 5.60] Label arithmetic bug

Post by Fred »

Yes, round() is for floating point, so it won't work with addresses.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [PB 5.60] Label arithmetic bug

Post by kenmo »

Fred wrote:Yes, round() is for floating point, so it won't work with addresses.
Hi Fred. Even if you don't call any float functions, it looks like a bug with address --> float arithmetic.

Code: Select all

DataSection
  Start:
  Data.b $01, $02, $03, $04    ; 4 bytes (1 long)
  Finish:
EndDataSection

 Start  = ?Start
 Finish = ?Finish
*Start  = ?Start
*Finish = ?Finish

Debug       ( Finish -  Start) * 1.5      ;  (int - int) * float
Debug 1.5 * ( Finish -  Start)            ;  float * (int - int)

Debug       (*Finish - *Start) * 1.5      ;  (int - int) * float
Debug 1.5 * (*Finish - *Start)            ;  float * (int - int)

Debug       (?Finish - ?Start) * 1.5      ;  (int - int) * float
Debug 1.5 * (?Finish - ?Start)            ;  float * (int - int)

This could cause strange memory bugs!

Code: Select all

DataSection
  Start:
  Data.b $01, $02, $03, $04    ; 4 bytes (1 long)
  Finish:
EndDataSection

BufferSize.i = 1.5 * (?Finish - ?Start)
Debug BufferSize

BufferSize.i = (?Finish - ?Start) * 1.5
Debug BufferSize

BufferSize.i = 1.5 * (?Finish - ?Start)
Debug BufferSize
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [PB 5.60] Label arithmetic bug

Post by skywalk »

This should be mentioned in the manual. Since type casting is dependent on algebraic order, it should be noted that pointers/labels are also type cast with unintended results.
The fix is assign pointers' integer values prior to using in algebra.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply