機電之家資源網
單片機首頁|單片機基礎|單片機應用|單片機開發(fā)|單片機文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓信息
贊助商
SWI 軟中斷詳解
SWI 軟中斷詳解
 更新時間:2009-7-31 15:56:51  點擊數(shù):0
【字體: 字體顏色



 

SWI : 軟件中斷

(Software Interrupt)
  SWI{條件}  <24 位編號>
指令格式 這是一個簡單的設施,但可能是最常用的。多數(shù)操作系統(tǒng)設施是用 SWI 提供的。沒有 SWI 的 RISC OS 是不可想象的。

Nava Whiteford 解釋了 SWI 是如何工作的(最初在 Frobnicate issue 12½)...

 


我將試圖在本文中解釋 SWI 是如何工作的。

SWI 是什么?

SWI 表示 Software Interrupt。在 RISC OS  中使用 SWI 來訪問操作系統(tǒng)例程或第三方生產的模塊。許多應用使用模塊來給其他應用提供低層外部訪問。

SWI 的例子有:

  • 文件器 SWI,它輔助讀寫磁盤、設置屬性等。
  • 打印機驅動器 SWI,用來輔助使用打印并行端口。
  • FreeNet/Acorn TCP/IP 協(xié)議棧 SWI,用 TCP/IP 協(xié)議在 Internet 上發(fā)送和接收數(shù)據(jù)。

在以這種方式使用的時候,SWI 允許操作系統(tǒng)擁有一個模塊結構,這意味著用來建立完整的操作系統(tǒng)的所需的代碼可以被分割成許多小的部分(模塊)和一個模塊處理程序(handler)。

當 SWI 處理程序得到對特定的例程編號的一個請求的時候,它找到這個例程的位置并執(zhí)行它,并傳遞(有關的)任何數(shù)據(jù)。

它是如何工作的?

首先查看一下如何使用它。一個 SWI 指令(匯編語言)看起來如下:
   SWI &02
   SWI "OS_Write0"
這些指令實際上是相同的,將被匯編成相同的指令。唯一的不同是第二個指令使用一個字符串來表示 SWI 編號 &02。在使用采用了字符串編號的程序的時候,在執(zhí)行之前首先查找這個字符串。

在這里我們不想處理字符串,因為它不能給出它要進行什么的一個真實表示。它們通常用于增進一個程序的清晰程度,但不是實際執(zhí)行的指令。

讓我們再次看一下第一個指令:

   SWI &02
這是什么意思? 字面的意思是進入 SWI 處理程序并傳遞值 &02。在 RISC OS 中這意味著執(zhí)行編號是 &02 的例程。

它是如何這么作的? 它如何傳遞 SWI 編號和進入 SWI 處理程序?

如果你查看內存的開始 32 字節(jié)(位于 0-&1C)并反匯編它們(查開實際的 ARM 指令)你將見到如下:

 

地址       內容               反匯編00000000 : 0..å : E5000030 : STR     R0,[R0,#-48]00000004 : .óŸå : E59FF31C : LDR     PC,&0000032800000008 : .óŸå : E59FF31C : LDR     PC,&0000032C0000000C : .óŸå : E59FF31C : LDR     PC,&0000033000000010 : .óŸå : E59FF31C : LDR     PC,&0000033400000014 : .óŸå : E59FF31C : LDR     PC,&0000033800000018 : .óŸå : E59FF31C : LDR     PC,&0000033C0000001C :  2?ã : E3A0A632 : MOV     R10,#&3200000
讓我們仔細看一下。

除了第一個和最后一個指令之外(它們是特殊情況)你見到的都是把一個新值裝載到 PC (程序計數(shù)器)的指令,它們告訴計算機到哪里去執(zhí)行下一個指令。還展示了這個值是從內存中的一個地址接受來的。(你可以在 !Zap 主菜單上使用“Read Memory”選項去自己查看一下。)

這看起來好象與 SWI 沒多少關系,下面做進一步的說明。

一個 SWI 所做的一切就是把模式改變成超級用戶并設置 PC 來執(zhí)行在地址 &08 處的下一個指令! 把處理器轉換到超級用戶模式會切換掉兩個寄存器 r13 和 r14 并用 r13_svc 和 r14_svc 替換它們。

在進入超級用戶模式的時候,還把 r14_svc 設置為在這個 SWI 指令之后的地址。

這個實際上就象一個連接到地址 &08 的分支指令(BL &08),但帶有用于一些數(shù)據(jù)(SWI 編號)的空間。

象我說過的那樣,地址 &08 包含跳轉到另一個地址的一個指令,就是實際的 SWI 程序的地址!

此時你可能會想“稍等一會! 還有 SWI 編號呢?”。實際上處理器忽略這個值本身。SWI 處理程序使用傳遞來的 r14_svc 的值來獲取它。

下面是完成它的步驟(在存儲寄存器 r0-r12 之后):

  1. 它從 r14 中減去 4 來獲得 SWI 指令的地址。
  2. 把這個指令裝載到一個寄存器。
  3. 清除這個指令的高端 8 位,去掉了 OpCode 而只剩下的 SWI 編號。
  4. 使用這個值來找到要被執(zhí)行的代碼的例程的地址(使用查找表等)。
  5. 恢復寄存器 r0-r12。
  6. 使處理器離開超級用戶模式。
  7. 跳轉到這個例程的地址。
容易吧! ;)

下面是一個例子,來自 ARM610 datasheet:

0x08 B SupervisorEntryTable DCD ZeroRtn DCD ReadCRtn DCD WriteIRtn ...Zero   EQU 0ReadC  EQU 256WriteI EQU 512 ; SWI 包含需要的例程在位 8-23 中和數(shù)據(jù)(如果有的話)在位 0-7 中。; 假定 R13_svc 指向了一個合適的棧STMFD R13, {r0-r2 , R14} ; 保存工作寄存器和返回地址。LDR R0,[R14,#-4] ; 得到 SWI 指令。BIC R0,R0, #0xFF000000 ; 清除高端的 8 位。MOV R1, R0, LSR #8 ; 得到例程偏移量。ADR R2, EntryTable ; 得到入口表(EntryTable)的開始地址。LDR R15,[R2,R1,LSL #2] ; 分支到正確的例程WriteIRtn ; 寫 R0 中的位 0 - 7 中的字符。............. LDMFD R13, {r0-r2 , R15}^ ; 恢復工作空間,并返回、恢復處理器模式和標志。
這就是 SWI 指令的基本處理步驟。

來源:

The ARM610 datasheet by Advanced Risc Machines
The ARM RISC Chip - A programmers guide by van Someren Atack published by Addison Wesley

 


Return to assembler index
Copyright © 2000 Richard Murray



SWI : SoftWare Interrupt

  SWI<suffix>  <number>
This is a simple facility, but possibly the most used. Many Operating System facilities are provided by SWIs. It is impossible to imagine RISC OS without SWIs.

Nava Whiteford explains how SWIs work (originally in Frobnicate issue 12½)...


In this article I will attempt to delve into the working of SWIs (SoftWare Interrupts).

What is a SWI?

SWI stands for Software Interrupt. In RISC OS SWIs are used to access Operating System routines or modules produced by a 3rd party. Many applications use modules to provide low level external access for other applications.

Examples of SWIs are:

  • The Filer SWIs, which aid reading to and from disc, setting attributes etc.

  • The Printer Driver SWIs, used to well aid the use of the Parallel port for printing.

  • The SWIs FreeNet/Acorn TCP/IP stack SWIs used to transmit and receive data using the TCP/IP protocol usually used for sending data over the Internet.

When used in this way, SWIs allow the Operating System to have a modular structure, meaning that the code required to create a complete operating system can be split up into a number of small parts (modules) and a module handler.

When the SWI handler gets a request for a particular routine number it finds the position of the routine and executes it, passing any data.

So how does it work?

Well first lets look at how you use it. A SWI instruction (in assembly language) looks like this:
   SWI &02
or
   SWI "OS_Write0"
Both these instructions are in fact the same, and would therefore assemble to the same instruction. The only difference is that the second instruction uses a string to represent the SWI number which is &02. When a program written using the string is used, the string is first looked up before execution.

We're not going to deal with the strings here as they do not give a true representation of what it going on. They are often used to aid the clarity of a program, but are not the actual instructions that are executed.

Right lets take a look at the first instruction again:

   SWI &02
What does that mean? Well, literally it means enter the SWI handler and pass value &02. In RISC OS this means execute routine number &02.

So how does it do that, how does it passed the SWI number and enter the SWI handler?

If you look at a disassembly of the first 32 bytes of memory (locations 0-&1C) and disassemble them (look at the actual ARM instructions) you should see something like this:

Address  Contents            Disassembly00000000 : 0..å : E5000030 : STR     R0,[R0,#-48]00000004 : .óŸå : E59FF31C : LDR     PC,&0000032800000008 : .óŸå : E59FF31C : LDR     PC,&0000032C0000000C : .óŸå : E59FF31C : LDR     PC,&0000033000000010 : .óŸå : E59FF31C : LDR     PC,&0000033400000014 : .óŸå : E59FF31C : LDR     PC,&0000033800000018 : .óŸå : E59FF31C : LDR     PC,&0000033C0000001C : 2¦ ã : E3A0A632 : MOV     R10,#&3200000
So what? You may think, well take a closer look.

Excluding the first and last instructions (which are special cases) you can see that all the instruction load the PC (Program Counter), which tells the computer where to execute the next instruction from, with a new value. The value is taken from a address in memory which is also shown. (you can take a look at this for yourself using the "Read Memory" option on the !Zap main menu.)

Now, this may seem to bare little relation to SWIs but with the following information it should make more sense.

All a SWI does is change the Mode to Supervisor and set the PC to execute the next instruction at address &08! Putting the processor into Supervisor mode switches out 2 registers r13 and r14 and replaces these with r13_svc and r14_svc.

When entering Supervisor mode, r14_svc will also be set to the address after the SWI instruction.

This is really just like a Branch with Link to address &08 (BL &08) but with space for some data (the SWI number).

As I have said address &08 contains a instruction which jumps to another address, this is the address where the real SWI Handler is!

At this point you maybe thinking "Hang on a minute! What about the SWI number?". Well in fact the value itself is ignored by the processor. The SWI handler obtains it using the value of r14_svc that got passed.

This is how it does it (after storing the registers r0-r12):

  1. It subtracts 4 from r14 to obtain the address of the SWI instruction.
  2. Loads the instruction into a register.
  3. Clears the last 8 bits of the instruction, getting rid of the OpCode and giving just the SWI number.
  4. Uses this value to find to address of the routine of the code to be executing (using lookup tables etc.).
  5. Restore the registers r0-r12.
  6. Takes the processor out of Supervisor mode.
  7. Jumps to the address of the routine.
Easy! ;)

Here is some example code, from the ARM610 datasheet:

0x08 B SupervisorEntryTable DCD ZeroRtn DCD ReadCRtn DCD WriteIRtn ...Zero   EQU 0ReadC  EQU 256WriteI EQU 512 ; SWI has routine required in bits 8-23 and data; (if any) in bits 0-7.; Assumes R13_svc points to a suitable stackSTMFD R13, {r0-r2 , R14} ; Save work registers and return addressLDR R0,[R14,#-4] ; Get SWI instruction.BIC R0,R0, #0xFF000000 ; Clear top 8 bits.MOV R1, R0, LSR #8 ; Get routine offset.ADR R2, EntryTable ; Get start address of entry ; table.LDR R15,[R2,R1,LSL #2] ; Branch to appropriate routine.WriteIRtn ; Wnte with character in R0 bits 0 - 7.............. LDMFD R13, {r0-r2 , R15}^ ; Restore workspace and return, restoring ; processor mode and flags.
That's it, that's the basics of the SWI instruction.

Sources: The ARM610 datasheet by Advanced Risc Machines
The ARM RISC Chip - A programmers guide by van Someren Atack published by Addison Wesley

   軟中斷:
編程異常通常叫做軟中斷

軟中斷是通訊進程之間用來模擬硬中斷的 一種信號通訊方式。
中斷源發(fā)中斷請求或軟中斷信號后,CPU或接收進程在適當?shù)臅r機自動進行中斷處理或完成軟中斷信號對應的功能軟中斷是軟件實現(xiàn)的中斷,也就是程序運行時其他程序對它的中斷;而硬中斷是硬件實現(xiàn)的中斷,是程序運行時設備對它的中斷。 1.軟中斷發(fā)生的時間是由程序控制的,而硬中斷發(fā)生的時間是隨機的
2.軟中斷是由程序調用發(fā)生的,而硬中斷是由外設引發(fā)的
3.硬件中斷處理程序要確保它能快速地完成它的任務,這樣程序執(zhí)行時才不會等待較長時間
閱讀全文(219) | 回復(2) | 引用(0) 
回復:軟中斷
goodguy發(fā)表評論于2005-6-15 20:06:00

SWI 指令

個人主頁 | 引用 | 返回 
回復:軟中斷
goodguy發(fā)表評論于2005-6-15 19:47:00
 

SWI
instruction

  • 上一篇: ARM的開發(fā)步驟
  • 下一篇: 用QtE實現(xiàn)SBC一241OX上的LED控制
  • 發(fā)表評論   告訴好友   打印此文  收藏此頁  關閉窗口  返回頂部
    熱點文章
     
    推薦文章
     
    相關文章
    網友評論:(只顯示最新5條。)
    關于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機電之家 | 會員助手 | 免費鏈接

    點擊這里給我發(fā)消息66821730(技術支持)點擊這里給我發(fā)消息66821730(廣告投放) 點擊這里給我發(fā)消息41031197(編輯) 點擊這里給我發(fā)消息58733127(審核)
    本站提供的機電設備,機電供求等信息由機電企業(yè)自行提供,該企業(yè)負責信息內容的真實性、準確性和合法性。
    機電之家對此不承擔任何保證責任,有侵犯您利益的地方請聯(lián)系機電之家,機電之家將及時作出處理。
    Copyright 2007 機電之家 Inc All Rights Reserved.機電之家-由機電一體化網更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術支持

    主辦:杭州市高新區(qū)(濱江)機電一體化學會
    中國行業(yè)電子商務100強網站

    網站經營許可證:浙B2-20080178-1