文章目錄
- QUESTION
- ANSWER
- MORE INFORMATION
- SEE ALSO
- FORUM THREADS
1. 關於如何在C語言中嵌入彙編命令:
轉載自:http://www.keil.com/support/docs/2308.htm
C51: GETTING INLINE ASSEMBLY TO WORK
Information in this article applies to:
QUESTION
How do I implement inline assembly in C51 using the ASM and
ENDASM directives. I have read some of the knowledgebase articles relating to this problem, but I still cannot get it to work.
ANSWER
The following example program, MAIN.C, demonstrates some simple inline assembly.
void main(void){ test();#pragma asm JMP $ ; endless loop#pragma endasm}
The following rules apply to C51 inline assembly:
- To use #pragma ASM/ENDASM, you must set the Generate Assembler SRC File andAssemble SRC File source file compile options in µVision by right-clicking your source file name in the Project Workspace and selectingOptions
for File.
- When using the Generate Assembler SRC File and Assemble SRC File options in µVision, if there are no other C modules in your project, you must manually include the C51 Run-Time Library (C51S.LIB, C51M.LIB, or C51L.LIB).
MORE INFORMATION
- Refer to ASM in the Cx51 User's Guide.
- Refer to Interfacing C to Assembler in the Cx51 User's Guide.
SEE ALSO
- C51: PROBLEMS WITH #PRAGMA ASM
- GENERAL: SRC DIRECTIVE
- BL51: WARNING L1 (UNRESOLVED EXTERNAL) USING INLINE ASSEMBLY
- C51: INLINE ASSEMBLY GENERATES TARGET OUT OF RANGE
- C51: IN-LINE ASM GIVES COMPILER WARNINGS
FORUM THREADS
The following Discussion Forum threads may provide information related to this topic.
Last Reviewed: Thursday, July 17, 2008
--轉載自:http://blog.21ic.com/user1/6088/archives/2010/68469.html
方法:(假設當前工程中只有一個C代碼檔案)
第1步:建立一個工程,添加C檔案,編譯通過
第2步:右擊Keil介面中Project Workspace裡的C檔案名稱,選擇‘Option
for…’
第3步:點擊‘Generate Assembler SRC File’‘Assemble
SRC File’兩個三態複選框使它們呈高亮並勾選,點擊確定
第4步:將KEIL安裝目錄下C51/LIB/C51S.LIB(如果是SMALL模式)檔案複製到工程目錄下
第5步:添加庫檔案C51S.LIB檔案到工程,即右擊‘Source
Group 1’,選擇‘Add Files to Group …’,在工程目錄下選擇C51S.LIB,點擊開啟
(因為有了第3步中的操作,如果在這裡不添加庫檔案,則編譯產生的HEX檔案不能被單片機正確執行,同時編譯時間會出現一個警告:
WARNING L1:UNRESOLVED EXTERNAL SYMBOL
SYMBOL: ?C_STARTUP
無法識別?C_STARTUP這個標號,這個標號是在庫檔案中定義
)
第6步:開啟工程目錄,可以看到新產生一個副檔名為.SRC的檔案,將其副檔名改成.A51
第7步:添加這個.A51檔案到工程(這時候工程就包含C51S.LIB庫檔案,不然編譯會產生第5步中說明的警告,且產生的HEX檔案不能被單片機正確運行)
第8步:重新編譯,產生HEX檔案,載入到單片機中,可以發現與C語言啟動並執行現象是一致的