这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » C中如何嵌入汇编语句?

共2条 1/1 1 跳转至

C中如何嵌入汇编语句?

院士
2006-09-17 18:14:16     打赏
C中如何嵌入汇编语句?



关键词: 如何     嵌入     汇编     语句    

院士
2006-12-22 22:43:00     打赏
2楼
问 我用的是TKS-52S仿真器,用的是随机软件,但我想在C语言中嵌入汇编语句,参考Keil C书籍,用
#pragma asm

#pragma endasm

发现不行.
请教周先生,如何解决? 1: 这样!在C程序中
{
  ...
  __asm {
      NOP
      ...自己填代码
  }

} 2: 试了,还是不行 3: 这样试试看!!1加库函数

{
  ...
  __asm {
      NOP("中间自己填,可参照资料")
      ......
  }

4: 楼上的那个是ads中的用法,搂主换个编译器吧 5: 才注意到楼主用的是51仿真器,不好意思!好象Keil是不支持内联混合编程的! 6: 供参考!QUESTION
I have a problem with following in-line assembly code that I created from an old, existing assembly function:

unsigned char PutChar_I2C(unsigned char ch) {
#pragma asm
      SETB    MDE
      MOV     A,R7     ; get ch in ACC
      .
      .
      .
      MOV     R7,#1    ; return value 1
      CLR     MCO
#pragma endasm
}

This code generated these warnings:

warning C280: 'ch': unreferenced local variable
warning C173: missing return-expression

The compiler generates this warning despite the fact I used the variable 'ch' (passed in R7). Also, why is there a warning for a missing return-expression since I set the return value in R7?

ANSWER
There are two solutions to your problem. Since the complete function is just assembly, you should leave it in a separate assembler module and just add the proper segment naming conventions. Then you may call the function from C as shown below:

Assembler Module:

?PR?_PutChar_I2C?FILE   SEGMENT CODE
RSEG ?PR?_PutChar_I2C?FILE
PUBLIC _PutChar_I2C
_PutChar_I2C:
      SETB    MDE
      MOV     A,R7     ; get ch in ACC
      .
      .
      .
      MOV     R7,#1    ; return value 1
      CLR     MCO
      RET

Function Call from C:

extern unsigned char PutChar_I2C(unsigned char ch);

void main (void)  {
  val = PutChar_I2C (1);
}

Another solution is to add dummy statements to the C source file as shown below:

unsigned char PutChar_I2C(unsigned char ch) {

// dummy assignment to avoid warning
// 'unreferenced local variable'
  ch = ch;
#pragma asm
      SETB    MDE
      MOV     A,R7     ; get ch in ACC
      .
      .
      .
      MOV     R7,#1    ; return 1
      CLR     MCO
#pragma endasm

// dummy return to avoid warning
// 'missing return-expression'
  return (ch);
}

The C51 compiler does not generate warnings. However, carefully review the generated code for these statements, and confirm that the variable 'ch' is correctly assigned to the parameter passing register R7 for the return value.

7: 几个链接!http://www.keil.com/support/docs/2308.htm

http://www.keil.com/support/docs/2622.htm

http://www.keil.com/support/docs/1671.htm

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]