Showing posts with label Compiler. Show all posts
Showing posts with label Compiler. Show all posts

Sunday, October 10, 2010

Interrupt Service Routines (ISRs) and its declaring in C language

About Interrupt Service Routines (ISRs) & Interrupt
對於Interrupt Service Routines(ISRs)各家有各種版本的解釋,但其實內容大同小異,例如,微軟在Windows CE 5.0的線上文件中對於ISRs有著這樣的說法,
An interrupt service routine (ISR) is a software routine that hardware invokes in response to an interrupt. ISRs examine an interrupt and determine how to handle it. ...... An ISR must perform very fast to avoid slowing down the operation of the device and the operation of all lower priority ISRs. [1]
由上面的最後一句話,我們可以知道ISRs的執行必須是快速的、有效率的,否則可能影響整個系統的運作。另外在Raj Kamal [2]的書上,對於ISR有這麼一段定義:
ISR is also called device driver in case of the devices and called exception or signal or trap handler in case of software interrupts.

Raj Kamal把ISR分成軟體及硬體的面相來說明。的確, Interrupt在我們修OS時,恐龍本OS [3]將Interrupt分成三類:
  1. External Interrupt: 由CPU外的週邊硬體所發出, 如: I/O complete...
  2. Internal Interrupt: CPU執行指令時發生, 如: Overflow, divide by zero...
  3. Software Interrupt: User Program在執行期間需要某項服務(如I/O request)而對OS發出Interrupt
當OS收到Interrupt,會保存目前process的執行狀態且暫停process,而去執行所對應的ISR。 


Declaring ISRs using C language
在C語言中,對於如何使用ISR,並沒有制定一個標準,因此不同的compiler會有不同的宣告方式,底下舉出各種compiler的宣告方式:
  • Borland C
/* Borland C */
void interrupt interrupt_handler(void)
{
    /* do something */
}
  • Watcom C/C++
/* Watcom C/C++ */
void _interrupt interrupt_handler(void)
{
    /* do something */
}
  • IAR
/* IAR declares an ISR like so: */
#pragma vector=TIMER0_OVF_vect
__interrupt void MotorPWMBottom()
{
    // code
}
  • AVR GCC
/* AVR GCC declares an ISR like so: */
ISR(PCINT1_vect)
{
  //code
}

其中, AVR GCC是以macro的方式呈現, 因此需要include底下這個header file
#include <avr/interrupt.h>

An ISR example
在筆者之前一篇文章「How to program the Pololu 3pi Robot using C language - "Motor Control"」中提到, ATmega328p有兩個8-bit timers: Timer0與Timer2, 它們被用來控制電流, 因此時間的控制是很重要的, 假如某個timer發生了overflow就應該做出一些中斷措施, 底下的例子就是每當程式執行中, timer2發生overflow時, ISR的呼叫情形

// this ISR is called every time timer2 overflows
ISR(TIMER2_OVF_vect)
{
 us_times_10 += 1024;
 if (us_times_10 >= 10000)
 {
  timer0_millis++;
  us_times_10 -= 10000;
 }
}

Conclusion
本文收集了不同書本、網站對於Interrupt Service Routines的介紹, 並複習了OS課程中, 「恐龍本」所分類的幾種Interrupt情況, 分成了軟體及硬體兩個面相來描述。另外也介紹四種不同的編譯器對於ISR的使用方式, 最後舉出一個例子, 來介紹「AVR」單晶片在這方面的應用。


Reference
  1. http://msdn.microsoft.com/en-us/library/ms892408.aspx
  2. Embedded Systems: Architecture, Programming and Design, 2/e, by Raj Kamal, Tata McGraw-Hill, 2006
  3. Silberschatz, Galvin, and Gagne, “Operating System Concept,” Sixth Edition, John Wiley & Sons, Inc., 2002.
  4. http://wiki.osdev.org/Interrupt_Service_Routines
  5. AVR-Libc user manual (v1.6.6)

Saturday, September 4, 2010

[C++] 函式,如何傳遞一個C++物件

Introduction
不論在C或C++中,當函式要傳遞一個「傳回值」時,通常可以透過「暫存器」來傳遞,也就是:函式把將要回傳的值,暫存在某個暫存器中,接下來呼叫方就可以去讀取該暫存器,也就達到傳遞的效果,以上作法的前提是,該「暫存器」容納得下該「傳回值」;然而,在C++中,當函式的「傳回值」不再是一般小尺寸的型態,而是大尺寸的物件時,會發生什麼事?編譯器會如何來處理這種情況?本篇文章即要討論這個情況。



Return Value — 「object」
當傳回值是個C++的「物件」,且程式沒有設定傳回值最佳化時(Return Value Optimization),則需要透過兩次的物件複製來達到傳遞目的:
  1. 將該物件複製到堆疊上的臨時空間
  2. 將存放在臨時空間中的物件複製到儲存函式回傳值的物件上
然而,根據不同的compiler及不同的calling convention,甚至不同的平台,在傳遞物件上皆會有不同的實作方式。

接下來在實驗的地方,我將比較在「VC9」(WITH Windows XP)及「g++」(WITH Ubuntu 10.04 64bit)這兩種不同的compiler及不同的平台下,執行物件回傳的異同,以及Return Value Optimization的設定對於執行結果的影響。




Experiment
在實驗一跟實驗二的地方,我將在VC9及g++底下分別執行測試的程式碼,並將執行結果的截圖貼上來。而由於前面兩個實驗產生了不同的執行結果,我將在short summary的地方進一步分析原因,並透過實驗三來做驗證
 
  • Source Code

#include <iostream>

using namespace std;

struct cpp_obj
{
 cpp_obj()
 {
  cout << "ctor\n";
 }
 cpp_obj(const cpp_obj& c)
 {
  cout << "copy ctor\n";
 }
 cpp_obj& operator=(const cpp_obj& rhs)
 {
  cout << "operator=\n";
  return *this;
 }
 ~cpp_obj()
 {
  cout << "dtor\n";
 }
};

cpp_obj return_test()
{
 cpp_obj b;
    cout << "before return\n";
 return b;
}

int main()
{
 cpp_obj n;
 n = return_test();
}

  • Exp.1
          實驗一是在windows XP下使用VC9,底下是執行結果的截圖



  • Exp.2
          實驗二是在Ubuntu下使用g++,底下是執行結果的截圖




  • short summary of Exp.1 & Exp.2
在實驗一當中,我們發現物件被複製了兩次,分別是
  1. 呼叫「copy constructor」來將物件複製到堆疊上的臨時空間
  2. 呼叫「operator=」來將存放在臨時空間中的物件複製到儲存函式回傳值的物件上
而實驗二,我們發現減少了copy constructor的呼叫,原因是因為g++ compiler在執行程式時自動做了最佳化(Return Value Optimization),將物件直接建構在臨時空間上,因此物件少了一次的複製。


因此,在實驗三,我們要做的就是將VC9的程式最佳化打開,我們可以預料得到的是: 最佳化打開後,物件勢必會減少一次的複製。


  • Exp.3
實驗三是在windows XP下使用VC9,且打開程式的最佳化(Return Value Optimization),底下是執行結果的截圖




Conclusion
本篇文章介紹了函式是如何傳遞一個C++物件,且透過實驗來說明不同的compiler會有不同的設定,根據不同的compiler及不同的calling convention,甚至不同的平台,在傳遞物件上皆會有不同的實作方式。

筆者是在VC9(WITH Windows XP)及g++(WITH Ubuntu 10.04 64bit)這兩種compiler下編譯程式的,而我們發現Return Value Optimization提升了程式執行時的效能,深深影響著實驗的結果,更多討論可以詳見Reference 2。

回傳一個物件是一件非常繁雜的工作,也因此,我們應該盡量避免回傳「物件」。


Reference 
  1. 程式設計師的自我修養 — 連結、載入、程式庫,俞甲子、石凡、潘愛民 
  2. MSDN, http://msdn.microsoft.com/en-us/library/ms364057%28VS.80%29.aspx