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

How can generate the JC instruction with the Keil C compiler

HI,Dear all,

I did'nt want to use the SRC and ASM,How can Gen the JC instruction in keil?such as
following.
I used three byte,but used the union struct must define a double word var,You know,The Keil will gen a assemble code more waste the CPU time.So I have following code,to finish a add with 3-byte,but the keil compiler is not smart.

CY = 0;
V_0 += 0x07;
if(CY)
{
CY = 0;
V_1 += 1;
if(CY)
{
CY = 0;
V_2 += 1;
}
}
**********************************
; CY = 0;
; SOURCE LINE # 1254
CLR CY
; V_0 += 0x07;
; SOURCE LINE # 1255
MOV A,#07H
ADD A,V_0
MOV V_0,A
;
; if(CY)
; SOURCE LINE # 1257
JNB CY,?C0017
; {
; SOURCE LINE # 1258
; CY = 0;
; SOURCE LINE # 1259
CLR CY
; V_1 += 1;
; SOURCE LINE # 1260
INC V_1
;
; if(CY)
; SOURCE LINE # 1262
JNB CY,?C0017
; {
; SOURCE LINE # 1263
; CY = 0;
; SOURCE LINE # 1264
CLR CY
; V += 1;
; SOURCE LINE # 1265
INC V_2
; }

Parents
  • "Yes,but you know,if you will send a mass data to a device,the long var will waste the CPU much time do the null work,"

    What on earth are you talking about?!

    If you need to handle 32-bit numbers, then using the compiler's built-in support will be more efficient than trying to code it yourself at the 'C' source level!

    Of course, if you use 32-bit data types when you don't need to, then you will obviously be wasting a lot of code space & CPU time!

Reply
  • "Yes,but you know,if you will send a mass data to a device,the long var will waste the CPU much time do the null work,"

    What on earth are you talking about?!

    If you need to handle 32-bit numbers, then using the compiler's built-in support will be more efficient than trying to code it yourself at the 'C' source level!

    Of course, if you use 32-bit data types when you don't need to, then you will obviously be wasting a lot of code space & CPU time!

Children
  • If you need to handle 32-bit numbers

    Well, look at Paddy's example a bit closer, and you'll find that what it really needs is a 17-bit number (or an exploitable carry-bit) for his loop of exactly 65536 iterations. But C only offers integer types with either 16 or 32 bits. So yes, he does need to handle 32-bit numbers, if he wants to keep within the limits of C, but it'll still be wasteful, because it updates a value almost twice as wide as it really has to be. A 24-bit C integer type supported by the compiler could help, here, but it doesn't exist.

    In assembly, this could be done by checking the carry bit after decrementing the hibyte of a 16-bit number, but in C, that's not available.

    To work around this, one can use an accepting loop construct and exploit unsigned roll-over:

       unsigned char i = 0;
       do {
          /* do something */
          i ++
       } while (i != 0);
    

    Or use break:

       unsigned char i = 0xff;
       while (1) {
          /* do something */
          if (i == 0)
              break;
          i--;
       }
    

    Both of these loops will have 256 iterations, but without the need for a 9-bit data type or access to the carry flag.