It is currently Mon Dec 17, 2018 10:44 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 7:12 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
I can't make any sense of this, hopefully im just having a Brain Moment (probably) and its not a bug!

This demo is supposed to take a single digit string such as "3", then return the value of that third byte stored in the DataSection under a label called "Lookup:", very simple.

Everything seems fine except if I use "result = result + value", where result was 0 to start with, the resulting value isn't the value!?

Code:
Define result.i, sNextChar.s

sNextChar = "3":  result = 0

Debug "Val=" + Str(Val(sNextChar))                        ;Correctly returns 3
Debug("Peek=" + Str( PeekA(?Lookup)))                     ;Correctly returns 0 (Lookup value index 0)
Debug("Peek=" + Str( PeekA(?Lookup + Val(sNextChar)) ))   ;Correctly returns 3 (Lookup value index 3)

;THIS WORKS:
result = PeekA(?Lookup + Val(sNextChar))
Debug("result=" + Hex(result))                            ;Correctly returns 3

;THIS FAILS, why!? the logic is essentially the same
result = 0
result = result + PeekA(?Lookup + Val(sNextChar))         ;(change Val(sNextChar) to 3 and it works, even though Val(sNextChar) correctly returns 3)
Debug("result=" + Hex(result))                            ;result is now 0x43353E   !??!?  (Expected: 3). And im only adding a single byte (PeekA) to result.i, which was 0 ... but somehow going from 0 to a strange address

DataSection 
  Lookup:
  ! db 0,1,2,3,4,5             ;eg if sNextChar = "3" we get ?Lookup+Val("3")= the fourth value which is 3
EndDataSection

5.41LTS Win32 ascii+unicode

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Last edited by Keya on Fri Jan 15, 2016 7:24 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 7:23 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3268
Location: Netherlands
I think this is a bug.
On OSX, 32 bits shows an IMA error, 64 bits works fine.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 7:28 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
wilbert thankyou very much for your prompt test!! it does seem like a bug to me, but i think im still having a Brain Moment also :D
At least the workaround is easy, but if it is a bug it seems like one of those nasty subtle ones that are tricky to find without a bit of QA! hmm

ps. Fred you can rename the thread topic to something a little less vague if you want - i couldnt figure out enough about it to accurately title it heehee

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 7:32 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3268
Location: Netherlands
Can you try this on Windows
Code:
result = PeekA(?Lookup + Val(sNextChar)) + result
instead of
Code:
result = result + PeekA(?Lookup + Val(sNextChar))         ;(change Val(sNextChar) to 3 and it works, even though Val(sNextChar) correctly returns 3)

As strange as it may seem, on OSX this makes a difference.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 7:39 am 
Offline
User
User

Joined: Sun Jun 16, 2013 6:27 am
Posts: 79
Location: Tasmania (Australia)
I think the problem is wrong types.
Try
Code:
Define result.q


Still might be a bug...

Jim


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 8:00 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
wilbert wrote:
Can you try this on Windows...

wilbert, cunning thinking, yes that changed order one worked! how odd

TassyJim, yes changing it to quad fixes it also, nice catch! still seems like a bug though

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Fri Jan 15, 2016 11:56 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Sep 12, 2015 6:55 pm
Posts: 105
Looks like a compiler bug, see below the code generated.

Changing result.i to quad forces some other code generation to cope with data larger than 32 bits, so the compiler does not rely on the ebx register anymore and so the bug does not happen.

Code:

Define result.i, sNextChar.s

sNextChar = "3":  result = 0

Debug "Val=" + Str(Val(sNextChar))                        ;Correctly returns 3
Debug("Peek=" + Str( PeekA(?Lookup)))                     ;Correctly returns 0 (Lookup value index 0)
Debug("Peek=" + Str( PeekA(?Lookup + Val(sNextChar)) ))   ;Correctly returns 3 (Lookup value index 3)

;THIS WORKS:
result = PeekA(?Lookup + Val(sNextChar))
Debug("result=" + Hex(result))                            ;Correctly returns 3


;THIS FAILS, why!? the logic is essentially the same
result = 0

; result = result + PeekA(?Lookup + Val(sNextChar))       

!  MOV    ebx,dword [v_result] ; ebx contains the value of result
!  MOV    ebp,l_lookup
!  MOV    edi,ebp
!  PUSH   dword [v_sNextChar]
!  CALL  _PB_Val_UNICODE@4
!  MOV    ecx,ebx
!  SAR    ecx,31
!  PUSH   ecx
!  PUSH   ebx
!  MOV    ecx,edi
!  SAR    ecx,31
!  PUSH   ecx
!  PUSH   edi
!  PUSH   edx
!  POP    edx
!  ADD    [esp],eax
!  ADC    [esp+4],edx
!  MOV    ebx,[esp] ; ebx is overwritten with the memory address ?Lookup + Val(sNextChar)
!  ADD    esp,8
!  MOV    eax,ebx
!  PUSH   eax
!  CALL  _PB_PeekA@4

; !  MOV    ebx,dword [v_result] ; brutal reload of result into ebx, just to test this

!  ADD    ebx,eax ; without the line above, this sum the above memory address and 3
!  MOV    dword [v_result],ebx


Debug("result=" + Hex(result))                            ;result is now 0x43353E   !??!?  (Expected: 3). And im only adding a single byte (PeekA) to result.i, which was 0 ... but somehow going from 0 to a strange address

DataSection
  Lookup:
  ! db 0,1,2,3,4,5             ;eg if sNextChar = "3" we get ?Lookup+Val("3")= the fourth value which is 3
EndDataSection


Tested on Windows 7


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Sat Jan 16, 2016 2:16 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3510
Location: Utah, USA
My results for Windows 8.1 v5.41 LTS x64 (ASCII/Unicode):
Code:
Val=3
Peek=0
Peek=3
result=3
result=3


My results for Windows 8.1 v5.41 LTS x32 (Unicode):
Code:
Val=3
Peek=0
Peek=3
result=3
result=4334E6


My results for Windows 8.1 v5.41 LTS x32 (ASCII):
Code:
Val=3
Peek=0
Peek=3
result=3
result=436F32


@Edit: removed incorrect observation.

_________________
Image


Last edited by Demivec on Sat Jan 16, 2016 3:38 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Sat Jan 16, 2016 2:42 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Sep 12, 2015 6:55 pm
Posts: 105
Why you say that ?
Did you see my post above ?
That's the code generated by the compiler, and the problem has nothing to do with Hex()
result already contains the wrong value.


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Sat Jan 16, 2016 3:32 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3510
Location: Utah, USA
sys64802 wrote:
Why you say that ?
Did you see my post above ?
That's the code generated by the compiler, and the problem has nothing to do with Hex()
result already contains the wrong value.

No, I did not see your post at the time I responded.

My statement was incorrect. :oops: I've edited my former post.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: bug? i cant make sense of this little one
PostPosted: Sat Jan 16, 2016 3:40 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1673
Demivec thanks for testing but I dont think this has anything to do with Hex() - if you change Hex() to Str() (so no Hex at all) in my first demo the result is still buggy
[edit] (as youve just noticed while i was typing heehee) :)

sys64802, nice detective work! :)

ps can a moderator please move this thread to Windows - Bugs (i wasnt sure it was a bug when i first posted and didnt want to wrongly assume so! but it seems confirmed now)

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye