bug? i cant make sense of this little one

Post bugreports for the Windows version here
User avatar
Keya
Addict
Addict
Posts: 1864
Joined: Thu Jun 04, 2015 7:10 am

bug? i cant make sense of this little one

Post by Keya »

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: Select all

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
Last edited by Keya on Fri Jan 15, 2016 7:24 am, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3736
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: bug? i cant make sense of this little one

Post by wilbert »

I think this is a bug.
On OSX, 32 bits shows an IMA error, 64 bits works fine.
macOS 10.15 Catalina, Windows 10
User avatar
Keya
Addict
Addict
Posts: 1864
Joined: Thu Jun 04, 2015 7:10 am

Re: bug? i cant make sense of this little one

Post by Keya »

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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3736
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: bug? i cant make sense of this little one

Post by wilbert »

Can you try this on Windows

Code: Select all

result = PeekA(?Lookup + Val(sNextChar)) + result
instead of

Code: Select all

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.15 Catalina, Windows 10
TassyJim
User
User
Posts: 93
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: bug? i cant make sense of this little one

Post by TassyJim »

I think the problem is wrong types.
Try

Code: Select all

Define result.q
Still might be a bug...

Jim
User avatar
Keya
Addict
Addict
Posts: 1864
Joined: Thu Jun 04, 2015 7:10 am

Re: bug? i cant make sense of this little one

Post by Keya »

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
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: bug? i cant make sense of this little one

Post by sys64802 »

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: Select all


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
User avatar
Demivec
Addict
Addict
Posts: 3818
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: bug? i cant make sense of this little one

Post by Demivec »

My results for Windows 8.1 v5.41 LTS x64 (ASCII/Unicode):

Code: Select all

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

Code: Select all

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

Code: Select all

Val=3
Peek=0
Peek=3
result=3
result=436F32
@Edit: removed incorrect observation.
Last edited by Demivec on Sat Jan 16, 2016 3:38 am, edited 1 time in total.
Image
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: bug? i cant make sense of this little one

Post by sys64802 »

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.
User avatar
Demivec
Addict
Addict
Posts: 3818
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: bug? i cant make sense of this little one

Post by Demivec »

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
User avatar
Keya
Addict
Addict
Posts: 1864
Joined: Thu Jun 04, 2015 7:10 am

Re: bug? i cant make sense of this little one

Post by Keya »

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)
Post Reply