Shop OBEX P1 Docs P2 Docs Learn Events
PASM: d,s field specified by negative number causes overflow and produces unexpcted op-code — Parallax Forums

PASM: d,s field specified by negative number causes overflow and produces unexpcted op-code

yisiguroyisiguro Posts: 52
edited 2019-03-28 11:39 in Propeller 1
Perhaps it is useless to discuss so much but I feel it is not natural behavior.

Code below is valid and does not cause any compile error,
con
  rate = 115200
obj
  t:"Parallax Serial Terminal"
pub main
  t.Start(rate)
  t.Hex(x,8) ' "A0FFFE00" expected
  t.NewLine
  t.Hex(y,8) ' "A0FC01FF" expected
  t.NewLine
  t.Hex(z,8) ' "A0FC0000" expected
  t.NewLine
  repeat
dat
  org 0
x
  mov -1,#0
y
  mov 0,#-1
z
  mov 0,#0

but results
FFFFFE00
FFFFFFFF
A0FC0000

I'm using PropellerTool PropellerIDE. This occurs when d and/or s field is specified by negative number ( decimal or not ).

(edit:)
Thanks for comments. I see this issue is caused with OpenSpin invoked by PropellerIDE.
I read source code of OpenSpin on github repo, and found lines related to this issue.

Actually, I've mistakenly believed that source operand specified by immediate value is sign-extended for some instructions, such as MOV, ADD, etc.
Of course sign is not extended, but filled by zero, I know now.

Comments

  • Strange that it compiles for you. I get errors when trying to assemble such instructions. What version of Propeller Tool are you using? (I have 1.3.2)
  • yisiguroyisiguro Posts: 52
    edited 2019-03-24 09:12
    Wuerfel_21 wrote: »
    Strange that it compiles for you. I get errors when trying to assemble such instructions. What version of Propeller Tool are you using? (I have 1.3.2)

    Sorry, I mistook. I'm using PropellerIDE, not PropellerTool.

    (edit)
    I was very careless that I did not test this issue on any other pasm compilers:

    PropellerTool (1.3.2) : raise error
    PropellerIDE (0.33.3) : pass but unexpected output
    Propellent (1.6) : raise error
  • Negative numbers in PASM are 2's complement 32-bit integers; S and D are 9-bit unsigned integers. Normal behavior if assigning an out of range number to S and D does not throw an error would be to take the least significant 9 bits. For -1, which is all bits 1, that would result in all 9 bits of S or D being 1, or a value of 511 or $1FF. And this is exactly what happened in your test.

    To get the bit pattern of a negative number, complement the bit pattern of the absolute value (all 1's to 0 and all 0's to 1) and then add 1. For example, if you do this with 1, you go from all zeroes except 1 in the lsb, to all 1's except 0 in the lsb, and when you add 1 you get all bits 1 for minus 1.
  • yisiguroyisiguro Posts: 52
    edited 2019-03-27 12:40
    localroger wrote: »
    For -1, which is all bits 1, that would result in all 9 bits of S or D being 1, or a value of 511 or $1FF. And this is exactly what happened in your test.

    What you say is exactly what I expected, but what happened is not: that produces wrong instruction "WAITVID" from any instruction.

    Anyway, that seems to be a bug of PropellerIDE, and I'll try newer version from github repo.

    Thanks.
  • Ah, PropIDE is probably not masking the out of range S or D value before adding or ORing it into the opcode.
  • yetiyeti Posts: 818
    edited 2019-03-25 15:01
    I got no PropellerIDE here. The 4 compilers which might be launched by PropellerIDE on Linux vote 3:1 that this code is not ok.
    $ cat main.spin 
    con
      rate = 115200
    obj
      t:"Parallax Serial Terminal"
    pub main
      t.Start(rate)
      t.Hex(x,8) ' "A0FFFE00" expected
      t.NewLine
      t.Hex(y,8) ' "A0FC01FF" expected
      t.NewLine
      t.Hex(z,8) ' "A0FC0000" expected
      t.NewLine
      repeat
    dat
      org 0
    x
      mov -1,#0
    y
      mov 0,#-1
    z
      mov 0,#0
    
    BSTC, HomeSpun and FastSpin complain about the operands:
    $ bstc -L /opt/parallax.spin.src/spin main.spin
    Brads Spin Tool Compiler v0.15.3 - Copyright 2008,2009 All rights reserved
    Compiled for i386 Linux at 08:17:46 on 2009/07/20
    Loading Object main
    Loading Object Parallax Serial Terminal
    
    main(17,7) Error : Parameter exceeds $1FF
      mov -1,#0
    ______^
    
    Compiled 285 Lines of Code in 0.009 Seconds
    
    $ fastspin -L /opt/parallax.spin.src/spin main.spin
    Propeller Spin/PASM Compiler 'FastSpin' (c) 2011-2019 Total Spectrum Software Inc.
    Version 3.9.24 Compiled on: Mar 24 2019
    main.spin
    |-Parallax Serial Terminal.spin
    main.spin(17) error: Destination operand too big for mov
    main.spin(19) error: Source operand too big for mov
    
    $ homespun -L /opt/parallax.spin.src/spin main.spin
    Homespun Spin Compiler 0.32 - Batang Build
    parsing main.spin
    parsing /opt/parallax.spin.src/spin/Parallax Serial Terminal.spin
    compiling main.spin
    Error: main.spin (17, 8): Destination register cannot exceed $1ff
      mov -1,#0
           ^
    
    OpenSpin does not.
    $ openspin -L /opt/parallax.spin.src/spin main.spin
    Propeller Spin/PASM Compiler 'OpenSpin' (c)2012-2018 Parallax Inc. DBA Parallax Semiconductor.
    Version 1.00.81 Compiled on Dec 18 2018 13:23:20
    Compiling...
    main.spin
    |-Parallax Serial Terminal.spin
    Done.
    Program size is 1176 bytes
    
    Maybe OpenSpin just should complain about this like the others already do?
  • Wuerfel_21Wuerfel_21 Posts: 5,053
    edited 2019-03-25 15:52
    Gotta love how all the spin compilers are a little bit garbo.
    PropTool is GUI only and has the least features, but is the reference compiler.
    OpenSpin has this funky bug and lacks some features (assembler listings, adding a #define from the command line).
    BSTC is closed source but has lots of features, but causes strange bugs sometimes.
    Fastspin uses a lot of memory.
    Homespun, I haven't used yet due to not having found a binary yet (and being too lazy to download a C# compiler).
  • RaymanRayman Posts: 14,641
    edited 2019-03-25 17:48
    It's really only a bug if the instruction changes to WAITVID, right?
    Does this happen with OpenSpin?

    Actually, I guess it is a bug ether way...
Sign In or Register to comment.