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

C file that also has assembly code

hi,
I'm writting code (8051) that has both C and assambly in the same file.
for example:
void main()
{
long temp;
#pragma asm
mov a, #0; //assambly
#pragma endasm
temp = a*1000; //C
}

problems:
1. a (acc) is not recognizable in the
C part.what is the BEST thing to do?
2. if I'll continue this proram and add
another asm part will the last value
of a,for example zero, and other
reg/ptr will be saved?

Parents
  • 1. a (acc) is not recognizable in the C part. what is the BEST thing to do?

    'C' uses the CPU registers (as opposed to the SFRs) for its own purposes;
    It has no idea that you've left a wanted value in A, so you can't rely on being able to use it!

    You'd need your assembler to copy the value to a 'C' variable.

    2. if I'll continue this proram and add another asm part will the last value of a,for example zero, and other reg/ptr will be saved?

    No. #pragma asm ... endasm simply pastes assembler source directly into the assembler source generated by the compiler. See p12 in the C51 User's Guide:

    "This source text can be thought of as in-line assembly. However, it is output to the source file generated only when using the SRC directive. The source text is not assembled and output to the object file."

    If you want values saved, you'll have to do it yourself!

    Note that #pragma asm ... endasm requires the SRC directive, so you can see exactly what's happening by examining the assembly source generated by the compiler!

    BTW, your code sample would look better within &ltpre&gt and &lt/pre&gt HTML Tags. (which also gives a pale background tint on my system).
    See "Tips for Posting Messages"
    http://www.keil.com/forum/tips.asp

    Check formatting using the 'Preview' button before posting.

Reply
  • 1. a (acc) is not recognizable in the C part. what is the BEST thing to do?

    'C' uses the CPU registers (as opposed to the SFRs) for its own purposes;
    It has no idea that you've left a wanted value in A, so you can't rely on being able to use it!

    You'd need your assembler to copy the value to a 'C' variable.

    2. if I'll continue this proram and add another asm part will the last value of a,for example zero, and other reg/ptr will be saved?

    No. #pragma asm ... endasm simply pastes assembler source directly into the assembler source generated by the compiler. See p12 in the C51 User's Guide:

    "This source text can be thought of as in-line assembly. However, it is output to the source file generated only when using the SRC directive. The source text is not assembled and output to the object file."

    If you want values saved, you'll have to do it yourself!

    Note that #pragma asm ... endasm requires the SRC directive, so you can see exactly what's happening by examining the assembly source generated by the compiler!

    BTW, your code sample would look better within &ltpre&gt and &lt/pre&gt HTML Tags. (which also gives a pale background tint on my system).
    See "Tips for Posting Messages"
    http://www.keil.com/forum/tips.asp

    Check formatting using the 'Preview' button before posting.

Children
No data