This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

subtraction with borrow

Hi all

How to write a pragram to subtraction immediate data from accumulator with borrow in C51?

Parents Reply Children
  • "I am trying a C code, the "xbuffer=(EX-X)+1;" is correct if original code as following:
    .
    .
    MOV A,EX
    SUBB A,X
    INC A
    MOV xbuffer,A"

    The assembler code behaves differently depending on the value of the carry flag, so I guess the answer is no.

    Are you trying to translate an assembler program into 'C'?

  • Yes, if it isn't correct. How to write a C code?

  • "Yes, if it isn't correct. How to write a C code?"

    You can't, or at least not meaningfully. While it is safe enough to read the carry flag in 'C' its value doesn't mean anything useful.

    Look at the bigger picture - figure out what the assembler program does and reproduce that in 'C'. Don't try to translate it one instruction at a time. Translate the algorithm instead.

  • Translate the algorithm instead.

    So anyone can for explain a simple program.

  • "So anyone can for explain a simple program."

    The subject of your question is a perfect example.
    The 8051 is an 8-bit processor, so all its instructions deal with only 8 bits at a time. Therefore, if you want to handle data larger than 8 bits in Assembler, you have to "manually" code it 8 bits at a time.

    The Subtract-with-Borrow (and, similarly, Add-with-Carry) instruction allows you to do subtraction on data larger than 8 bits by handling 8 bits at a time, and including a "borrow" from the previous 8-bit operation.

    In 'C', you don't mess about with all this detail - you just use the 16-bit data type (int) or 32-bit data type (long) built into the language!

    long a, b, c;
    
    a = b - c; // 32-bit subtraction in 'C' is just a simple use of the '-' operator!
    The whole point of high-level languages is that they allow you to concentrate on what you want to do, rather than how to achieve it with the available processor instructions!