How to prevent variable going below zero?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Quote Originally Posted by richard View Post
    mike would you like to provide some pbp code that demonstrates use of status register following pbp addition or subtraction


    the status reg here is not in any way influenced by the subtraction result

    i = byte

    Code:
    j=8
    i=6
    while j
    status=1
    i=i-1
    s=status
    debug 13,10,"status ",bin8 s ,"  i ",    dec i
    j=j-1
    wend
    result

    status 00011001 i 5
    status 00011001 i 4
    status 00011001 i 3
    status 00011001 i 2
    status 00011001 i 1
    status 00011101 i 0
    status 00011001 i 255
    status 00011001 i 254
    You don't state what PIC this is for, so I randomly grabbed the data sheet for a PIC16F1824; STATUS Register 3-1, page 24.

    STATUS <7:5> = Unimplemented, read as 0
    STATUS.4 = TO: Time-Out bit
    STATUS.3 = PD: Power-Down bit
    STATUS.2 = Z: Zero bit
    STATUS.1 = DC: Digit Carry
    STATUS.0 = C: Carry

    You can see when you reached zero the STATUS.Z reflected that. However, you alter the STATUS Register in your code. About the only thing you can change is the C bit. Delete the "status=1" line and try again.

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


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?


    You can see when you reached zero the STATUS.Z reflected that. However, you alter the STATUS Register in your code. About the only thing you can change is the C bit. Delete the "status=1" line and try again.
    your assertion is
    If subtracting and want to see if it rolled from 0 to 255, "STATUS.C = 0". Look in your data sheet for the STATUS Register and how it works.

    You don't state what PIC this is for, so I randomly grabbed the data sheet for a PIC16F1824; STATUS Register 3-1, page 24.
    if your assertion was correct it would not matter but its a12f1840

    i repeat
    mike would you like to provide some pbp code that demonstrates use of status register following pbp addition or subtraction
    for any chip
    Warning I'm not a teacher

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,392


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    a look at the lst file yields the reasons .
    the status reg is not useful when you add or subtract a const 1
    if you use a number greater than 1 or another var then the code does a proper addition/subtraction rather than inc/dec and sets status reg correctly

    caveat emptor
    Warning I'm not a teacher

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