How to prevent variable going below zero?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    I like Tumbleweed's code. Shorter and more to the point.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    With a few proviso's mikes way beats it hands down


    Code:
    
    IF Z<100 THEN I=I-5
    asm
         BTFSS STATUS,C
         CLRF   _I
     ENDASM
    
    if i attempts to underflow its set to 0
    Warning I'm not a teacher

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    I like it how a simple logic problem CuriousOne had, lead to another topic with negative numbers and STATUS register!

    Quite interesting!

    Ioannis

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    anyone familiar with assembly programming will be well aware of the status reg and its use. what surprised me is that
    the value of the status reg was/is preserved even after the completion of the transaction. because the result is transferred from the
    working regs back into the result variable during the process i expected the status to be no longer representative of the calculation.
    surprised yet again !
    Warning I'm not a teacher

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    You are absolutely right! I do not trust that this may be the case for any PIC though.

    Ioannis

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    I do not trust that this may be the case for any PIC though.
    its a quick easy test to check, it works on all chips that i tried just make sure the compiler dosn't compress the subtract into a decrement
    i var can be byte or word

    Code:
    
    j=8 
    i=6 
    while j 
    status=1
     i=i-2  ;subtractant cannot be 1 
    s=status 
    debug 13,10,"status ",bin8 s ,"  i ",    dec i 
    j=j-1 
    wend
    
    Warning I'm not a teacher

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Well, thanks for checking out. Wow!

    Ioannis

Similar Threads

  1. Assigning a string variable to another string variable
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd June 2013, 17:55
  2. Variable within Variable
    By munromh in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th January 2009, 14:44
  3. Electrical Current Does Prevent Corrosion?
    By T.Jackson in forum Off Topic
    Replies: 13
    Last Post: - 18th March 2008, 13:13
  4. One variable made up of another variable
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st September 2007, 00:18
  5. 2 variable
    By isaac in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th May 2004, 21:59

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts