PureBasic Forum
https://www.purebasic.fr/english/

bug? i cant make sense of this little one
https://www.purebasic.fr/english/viewtopic.php?f=4&t=64594
Page 1 of 1

Author:  Keya [ Fri Jan 15, 2016 7:12 am ]
Post subject:  bug? i cant make sense of this little one

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

Author:  wilbert [ Fri Jan 15, 2016 7:23 am ]
Post subject:  Re: bug? i cant make sense of this little one

I think this is a bug.
On OSX, 32 bits shows an IMA error, 64 bits works fine.

Author:  Keya [ Fri Jan 15, 2016 7:28 am ]
Post subject:  Re: bug? i cant make sense of this little one

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

Author:  wilbert [ Fri Jan 15, 2016 7:32 am ]
Post subject:  Re: bug? i cant make sense of this little one

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.

Author:  TassyJim [ Fri Jan 15, 2016 7:39 am ]
Post subject:  Re: bug? i cant make sense of this little one

I think the problem is wrong types.
Try
Code:
Define result.q


Still might be a bug...

Jim

Author:  Keya [ Fri Jan 15, 2016 8:00 am ]
Post subject:  Re: bug? i cant make sense of this little one

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

Author:  sys64802 [ Fri Jan 15, 2016 11:56 pm ]
Post subject:  Re: bug? i cant make sense of this little one

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

Author:  Demivec [ Sat Jan 16, 2016 2:16 am ]
Post subject:  Re: bug? i cant make sense of this little one

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.

Author:  sys64802 [ Sat Jan 16, 2016 2:42 am ]
Post subject:  Re: bug? i cant make sense of this little one

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.

Author:  Demivec [ Sat Jan 16, 2016 3:32 am ]
Post subject:  Re: bug? i cant make sense of this little one

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.

Author:  Keya [ Sat Jan 16, 2016 3:40 am ]
Post subject:  Re: bug? i cant make sense of this little one

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)

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/