Keil C51 和標準 C 主要的差異
Keil 的 C51 是以 ANSI C90 為其設計基礎, 即便是如此, 它和標準 C 語言 (ANSI C90) 之間還是有幾個滿大的差異:
- Keil C51 因應 8051 的特性多了 bit, sbit, sfr, sfr16 等四種資料型態. 一般我們只會用到 bit, 其他三個是定義 CPU 的特殊功能暫存器用的.
- Keil C51 在定義變數時多了儲存空間修飾字, 用來修改變數使用記憶體空間. 詳細可參考我的這一篇貼文: "C 語言:Keil C51 Program Tips"
- 多了定義中斷服務常式 ISR 的方法及進入 ISR 時切換暫存器區段 (register bank) 的語法, 例如: static void UART0_ISR(void) interrupt 4 using 2
- 標準函數庫沒有依標準來實作, 如: printf()
- 在 Keil C51 中, 函數預設是 non-reentrant, 這點是最嚴重的差異. 我們在使用時要有所警覺才好, 才不致於掉到陷阱裡而不自知.
什麼是 reentrant
在此先來了解一下 reentrant 的定義, Wiki 網站上給 reentrancy 下的定義是
In computing, a computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.