標籤

2009年4月2日 星期四

MASM note: asume

Since the ds register can be changed at run time (using an instruction like mov ds,ax), any segment can be a data segment.

When you specify a segment in your program, not only must you tell the CPU that a segment is a data segment, but you must also tell the assembler where and when that segment is a data (or code/stack/extra/F/G) segment.

Note that this directive does not modify any of the segment registers, it simply tells the assembler to assume the segment registers are pointing at certain segments.

assume directive modifies the assembler's behavior from the point MASM encounters it until another assume directive changes the stated assumption.


DSEG1 segment para public 'DATA'
var1 word ?
DSEG1 ends

DSEG2 segment para public 'DATA'
var2 word ?
DSEG2 ends

CSEG segment para public 'CODE'
assume CS:CSEG, DS:DSEG1, ES:DSEG2
mov ax, seg DSEG1
mov ds, ax
mov ax, seg DSEG2
mov es, ax

mov var1, 0
mov var2, 0
.
.
.
assume DS:DSEG2
mov ax, seg DSEG2
mov ds, ax
mov var2, 0
.
.
.
CSEG ends
end


The 80x86 microprocessor doesn't know about segments declared within your program, it can only access data in segments pointed at by the cs, ds, es, ss, fs.

When the assembler encounters an instruction of the form mov var1,0, the first thing it does is determine var1's segment. It then compares this segment against the list of assumptions the assembler makes for the segment registers. If you didn't declare var1 in one of these segments, then the assembler generates an error claiming that the program cannot access that variable. If the symbol (var1 in our example) appears in one of the currently assumed segments, then the assembler checks to see if it is the data segment. If so, then the instruction is assembled as described in the appendices. If the symbol appears in a segment other than the one that the assembler assumes ds points at, then the assembler emits a segment override prefix byte, specifying the actual segment that contains the data.

In the example program above, MASM would assemble mov VAR1,0 without a segment prefix byte. MASM would assemble the first occurrence of the mov VAR2,0 instruction with an es: segment prefix byte since the assembler assumes es, rather than ds, is pointing at segment DSEG2. MASM would assemble the second occurrence of this instruction without the es: segment prefix byte since the assembler, at that point in the source file, assumes that ds points at DSEG2. Keep in mind that it is very easy to confuse the assembler.


CSEG segment para public 'CODE'
assume CS:CSEG, DS:DSEG1, ES:DSEG2
mov ax, seg DSEG1
mov ds, ax
.
.
.
jmp SkipFixDS

assume DS:DSEG2

FixDS: mov ax, seg DSEG2
mov ds, ax
SkipFixDS:
.
.
.
CSEG ends
end


Notice that this program jumps around the code that loads the ds register with the segment value for DSEG2. This means that at label SkipFixDS the ds register contains a pointer to DSEG1, not DSEG2. However, the assembler isn't bright enough to realize this problem, so it blindly assumes that ds points at DSEG2 rather than DSEG1. This is a disaster waiting to happen. Because the assembler assumes you're accessing variables in DSEG2 while the ds register actually points at DSEG1, such accesses will reference memory locations in DSEG1 at the same offset as the variables accessed in DSEG2. This will scramble the data in DSEG1 (or cause your program to read incorrect values for the variables assumed to be in segment DSEG2).

For beginning programmers, the best solution to the problem is to avoid using multiple (data) segments within your programs as much as possible. Save the multiple segment accesses for the day when you're prepared to deal with problems like this. As a beginning assembly language programmer, simply use one code segment, one data segment, and one stack segment and leave the segment registers pointing at each of these segments while your program is executing. The assume directive is quite complex and can get you into a considerable amount of trouble if you misuse it. Better not to bother with fancy uses of assume until you are quite comfortable with the whole idea of assembly language programming and segmentation on the 80x86.

The nothing reserved word tells the assembler that you haven't the slightest idea where a segment register is pointing. It also tells the assembler that you're not going to access any data relative to that segment register unless you explicitly provide a segment prefix to an address. A common programming convention is to place assume directives before all procedures in a program. Since segment pointers to declared segments in a program rarely change except at procedure entry and exit, this is the ideal place to put assume directives:


assume ds:P1Dseg, cs:cseg, es:nothing
Procedure1 proc near
push ds ;Preserve DS
push ax ;Preserve AX
mov ax, P1Dseg ;Get pointer to P1Dseg into the
mov ds, ax ; ds register.
.
.
.
pop ax ;Restore ax's value.
pop ds ;Restore ds' value.
ret
Procedure1 endp


The only problem with this code is that MASM still assumes that ds points at P1Dseg when it encounters code after Procedure1. The best solution is to put a second assume directive after the endp directive to tell MASM it doesn't know anything about the value in the ds register:


.
.
.
ret
Procedure1 endp
assume ds:nothing


Although the next statement in the program will probably be yet another assume directive giving the assembler some new assumptions about ds (at the beginning of the procedure that follows the one above), it's still a good idea to adopt this convention. If you fail to put an assume directive before the next procedure in your source file, the assume ds:nothing statement above will keep the assembler from assuming you can access variables in P1Dseg.

Segment override prefixes always override any assumptions made by the assembler. mov ax, cs:var1 always loads the ax register with the word at offset var1 within the current code segment, regardless of where you've defined var1. The main purpose behind the segment override prefixes is handling indirect references. If you have an instruction of the form mov ax,[bx] the assembler assumes that bx points into the data segment. If you really need to access data in a different segment you can use a segment override, thusly, mov ax, es:[bx].

In general, if you are going to use multiple data segments within your program, you should use full segment:offset names for your variables. E.g., mov ax, DSEG1:I and mov bx,DSEG2:J. This does not eliminate the need to load the segment registers or make proper use of the assume directive, but it will make your program easier to read and help MASM locate possible errors in your program.

The assume directive is actually quite useful for other things besides just setting the default segment. You'll see some more uses for this directive a little later in this chapter.

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_8/CH08-3.html#HEADING3-98

2009年3月13日 星期五

符合SMBIOS規範的電腦系統訊息獲取方法

符合SMBIOS規範的電腦系統訊息獲取方法

作者:ramble(如需轉載請註明作者)

SMBIOS (System Management BIOS, SMBIOS),它是一種定出主機板及系統廠商如何以標準的格式顯示產品管理資訊的規格。

SMBIOS 及 DMI(Desktop Management Interface)規格兩者皆是由 Desktop Management Task Force (DMTF) 所草擬的,它是一個由業界所領導,實行技術規格以確認開放性標準的組織。

對於符合SMBIOS規範的電腦,可以通過訪問SMBIOS的結構獲得系統訊息,共有兩種辦法可以訪問:
  1. 通過即插即用功能接口訪問SMBIOS結構,這個在SMBIOS2.0標準裡定義了,從SMBIOS2.1開始這個訪問方法不再被推薦使用。
  2. 基於表結構的方法,表內容是tableentry point的數據,這個訪問方法從SMBIOS2.1以後開始被使用,從2.1開始,以後的版本都推薦使用這種訪問方式。在2.1版本中允許支援這兩種方法中的任意一種和兩種都支援,但在2.2已經以後的版本,必須支援方法2。

鑑於市場上計算機已經均支援SMBIOS2.3標準,所以只考慮方法2,基於表結構的訪問方式。

基於表結構訪問SMBIOS的過程是先找到EntryPoint Structure(EPS)表,然後通過EntryPoint Structure(EPS)表的數據找到SMBIOS數據表。

訪問SMBIOSEPS表的操作過程如下:

1.從物理內存0xF0000-0xFFFFF間尋找關鍵字「_SM_」

2.找到後再向後16個字節,看後面5個BYTE是否是關鍵字「_DMI_」,如果是,EPS表即找到。

註:按照SMBIOS規範説明,找到關鍵字」_SM_」後就可以確定此處就是EPS表結構,但我在實際操作中發現有為數不少的電腦的指定64K內存中有不只一個「_SM_」,所以不能只用找到」_SM_」來確定,需要繼續判斷16個字節後是否是「_DMI_」。

SMBIOSEPS表結構如下:

位置

名稱

長度

描述

00H

關鍵字

4 byte

固定是「_SM_」

04H

Check Sum

1 byte

用於檢查數據

05H

表結構長度

1 byte

Entry Point Structure表的長度

06H

Major版本號碼

1 byte

用於判斷SMBIOS版本

07H

Minor版本號碼

1 byte

用於判斷SMBIOS版本

08H

表結構大小

2 byte

用於即插即用接口方法獲得數據表結構長度

0AH

EPS修正

1 byte

0B-0FH

格式區域

5 byte

存放解釋EPS修正的訊息

10H

關鍵字

5 byte

固定為「_DMI_」

15H

Check Sum

1 byte

Intermediate Entry Point Structure(IEPS)的Check Sum

16H

數據表長度

2 byte

SMBIOS數據表長度

18H

數據表位址

4 byte

SMBIOS數據表的真實內存位置

1CH

數據表結構數目

2 byte

SMBIOS數據表的結構數目

1EH

SMBIOS BCD修正

1 byte


通過EPS表結構中的12H以及14H處,得出數據表長度和數據表地址,即可通過地址訪問結構表。從EPS表中的1CH處可得知結構表結構的總數,其中TYPE0結構就是BIOSinformation,TYPE1結構就是SYSTEMInformation。

每個結構的頭部是相同的,格式如下:

位置

名稱

長度

描述

00H

Type number

1 byte

結構的type number

01H

長度

1 byte

本結的長度,就此type number的結構而言

02H

Handle

2 byte

用於獲得SMBIOS結構,使用方法未知


每個結構都分為格式區域和字符串區域,格式區域就是一些本結構的信息,字符串區域是緊隨在格式區域後的一個區域。結構01H處標識的結構長度僅是格式區域的長度,字符串區域的長度是不固定的。

下面以TYPE0(BIOSinformation)為例説明格式區域和字符串區域的關係

位置

名稱

長度

描述

00H

Type number

1 byte

結構的type number,此處是0

01H

長度

1 byte

Type 0格式區域的長度,一般為14H,也有13H

02H

Handle

2 byte

一般為0000H

04H

BIOS廠商訊息

1 byte

此處是BIOS賣方的訊息,可能是OEM廠商名,一般為01H,代表緊隨格式區域後的字串區域的第一個字串

05H

BIOS版本

1 byte

BIOS版本號,一般為02H,代表字串區域的第二個字串

06H

BIOS開始位址段

2 byte

用於計算常駐BIOS鏡像大小的計算,方法為(10000H-BIOS開始位址段)*16

08H

BIOS發佈日期

1 byte

一般為04H,表示字串區第三個字串

09H

BIOS ROM Size

1 byte

計算方法為(n+1)*64K,n為此處讀出的數值

0AH

BIOS特徵

8 byte

BIOS的功能支援特徵,如PCI,PCMCIA,FLASH等等

12H

BIOS特徵擴展

不定



緊隨TYPE0(BIOSinformation)結構區域之後的就是TYPE0(BIOSinformation)字符串區域,如下所示:

db『System BIOS Vendor Name',0 ; 字符串以零結尾,第一個字符串:賣方訊息

db『4.04』,0 ; 第二個:BIOS版本

db『00/00/0000』,0 ; 第三個:發佈日期

db0 ; 以0為整個字符區域的結尾,所以要找下一個TYPE,只要在字符區域找到連續的0000H即可

註:當有EPS表中得到結構表的開始地址後,可以直接按結構來尋找相應的TYPE號,找到後直接讀取就是該TYPE對應的結構的格式區域訊息,然後向後移動結構區域長度(結構區域長度由該結構的01H處讀出)個BYTE,即是該TYPE機構的字符串區域。

由上面介紹可知,獲得BIOS訊息的辦法就是:

  1. 通過EPS表的12H和14H數據找到TYPE結構表,然後找到TYPE0的內存地址。(不一定是首個)
  2. 由TYPE0結構區域中得出相應BIOS訊息是否存在(存在則是上面所述的 01H,02H,03H依次排布,不存在則是相應的位置上為00H)。
  3. 如存在訊息,則從字符串區域中讀取對應BIOS訊息。

獲得SYSTEM信息方法同上,只是TYPE結構區域有所不同,請參照SMBIOSReference Specification

在Linux環境裡可以使用dmidecode這支程式去dump SMBIOS的資料

dmidecode主要功能:
桌面管理介面提供標準化的電腦硬體描述,包括硬體的特性,比如 BIOS 的序號、與硬體連接線。dmidecode 提供由 BIOS 輸出 DMI 的資料,通常被其他硬體偵測程式當作後端工具使用。


轉載來源:
http://cupidsd.bokee.com/4052760.html
其它參考資料:
System Management BIOS (SMBIOS) Specification
Desktop Management Interface (DMI) Standards
一篇世新的碩論-SMBIOS的研究與實作 寫得算蠻完整的唷
CodeGuru SMBIOS Demystified Windows VC++6.0

2009年2月25日 星期三

UFI

http://bbs.wuyou.net/viewthread.php?tid=127538&page=1#pid1430790

細節部分請上網下載Universal Serial Bus Mass Storage Class UFI Command Specification(PDF)。

BIOS透過USB總線(2.0的是通過EHCI USB)向USB存儲設備發送UFI指令來和它進行交流。
其中MODE SENSE Command(5AH)就是你所說的媒體介質、模式檢測功能,在返回的數據中包含了一個Media Type and Write Protect,裡面有介質描述符的字節(該字節在格式化的UFI命令中可以指定),BIOS就是根據這個來確認U盤的類型和是否有寫保護等。

系統對USB存儲設備訪問是採用用一種叫LBA的地址(目前採用的是LBA28),並沒有所謂的CHS,windows、linux等訪問USB儲存設備的時候並不使用BIOS的INT 13H,而是通過USB總線直接發UFI命令,採用的地址也是LBA,所以就不太存在兼容性問題。

然而U盤啟動引導程序卻是依靠BIOS的INT 13H來訪問。
BIOS的INT 13H對U盤的支援(不一定支援),就是把INT 13H中的C、H、S參數轉化成LBA地址,然而不同的BIOS設定的每道扇區數、每頭柱面數並不一定相同。假設我們有個U盤被自己機器的BIOS被認成16H、512C、64S,分區格式化后,分區的開始位置為0H、1CH、1S,分區引導程序所在的LBA是0000064H,另一台机子的BIOS認成16H、1024C、32S,那麼我們剛才做的那個啟動盤,分區起始扇區的LBA就變成0000032H,很顯然位置錯了,當然啟動不起來了。

BIOS對每道扇區數、每頭柱面數的不同設置就是導致U盤啟動不通用的根本原因。
也許你會說那麼我們引導程序干脆直接使用擴展INT 13H發送LBA包,GRUB就是這種思路,可是有的BIOS並不支持U盤的擴展INT 13H功能,並且在檢測的時候還會返回支持的假象-_-!!
其實386時代的硬盤也遇到這個問題,然而當時CMOS里面提供了硬盤類型參數的手動設置,再加上硬盤不是移動存儲設備,所以問題就沒那麼明顯了。

至於HDD、FDD、ZIP只是媒體介質描述的不同,並不能徹底解決CHS問題。

2009年2月16日 星期一

每個人心中 都有自己的priority

大家心中的priority是什麼?是親人?是朋友?是愛情?是工作?
我珍惜身邊的人,勝過一切,尤其是跟生命有關。

她時常抱怨我,為了工作忽略了她,我總是安慰。
但這次,我選擇明天的飛機回去,我並不是個喜歡與眾不同的人,很不喜歡這樣麻煩別人。
事情已經告一段落,剩下的時間留下又何妨,即使如此,我還是提出了需求。

我不喜歡故作悲傷,但強顏歡笑實在很累。

很多事有人不在乎,可我就是很care。
因為很多人沒有流過那眼淚,沒有感受那體溫。
當我鼻酸的那一瞬間,我知道,
曾經發生的事,是不會忘記的,只是想不起來而已。

我一直直覺事情不妙,但真的沒想到會來的那麼突然,所以接連好幾天妳的眼淚,我都只是安慰妳,
只是說服自己,眼淚不會流很久的,專心工作,人的生命是很堅強的。
我甚至出差前,才幫妳們搬家,才跟他一起扛書櫃,他才誇我懂事有禮貌。

如今,我卻再也聽不到老是抱怨爸爸管太多的女朋友了。

我曾經錯過太多,不是指情人節,不是指那段荒唐歲月,
或許是為了過去,或多或少的補償吧,或許是,無法再錯過了?

謝謝幫助我的人,也很感謝我遇到好的主管。
我知道我不是最需要安慰的人,我也知道有人覺得,女朋友的爸爸不是個好藉口,
我尊敬所有人心中的priority,也由衷的,謝謝你們的體諒。

2009年2月13日 星期五

PROCHOT#

http://it.sohu.com/20050228/n224465028.shtml


為彌補了第一代溫度監控技朮的缺陷,提高監控能力,Intel開發了第2代溫度監控技朮。

第二代溫度監控系統的一個突出特點是在微處理器內部集成了溫度控制電路(Thermal Control Circuit,TCC),由微處理器自身執行溫度控制功能,同時,微處理器內設置了兩個相互獨立的熱敏二极管,D1是本地熱敏二极管,所測信號提供給TCC,D2則為遠端熱敏二极管,其測量結果用于實現主板控制功能及顯示核心溫度,如圖8。




圖8 注∶第2代溫度監控系統框圖

我們先看看TCC是如何發揮作用的。TCC定義了兩種工作狀態:激活態和非激活態。TCC的狀態與PROCHOT#信號的電平高低相對應,PROCHOT#為低電平時,TCC為激活態,否則處于非激活態。當微處理器核心溫度達到警戒溫度(Warning Temperature)時,溫度檢測電路將PROCHOT#信號置為低電平,從而激活TCC。TCC激活后,采取“抑制任務周期”(Throttle duty Cycle)的方式(如圖9),使微處理器有效頻率下降,從而達到降低功耗的目的。當微處理器的溫度降低后(低于警戒溫度1℃以上),TCC回到非激活態,微處理器恢復到“標稱頻率”。可見,TCC實質上是一個由微處理器溫度控制的頻率調節器。



圖9 注∶TCC激活時,任務周期減少

如果發生災難性冷卻失敗的情況,使微處理器溫度超出极限溫度(thermal Trip),TCC將設THERMTRIP#信號為低電平,BIOS芯片檢測到這一變化后,直接關閉微處理器時鐘信號,並通過PWM控制器封鎖VRM向微處理器供電,直到溫度降到极限溫度以下,RESET#信號有效,THERMTRIP#才會重新變為高電平,系統才能繼續工作。否則THERMTRIP#總為低電平,系統就停留在暫停狀態。“當微處理器離開風扇的時候”,Pentium 4微處理器之所以能夠安然無恙,答案就在這里。

小知識∶警戒溫度與极限溫度有什麼不同?
微處理器警戒溫度(warning temperature)和极限溫度(thermal trip)都是指核心溫度,但它們所代表的意義有所不同。警戒溫度是能夠保証微處理器穩定運行的溫度;极限溫度也叫最高核心溫度(Maximum die temperature)或關机溫度(Shutdown temperature),是防止微處理器免于燒毀的溫度。

各款微處理器的警戒溫度和极限溫度值是制造商根据微處理器的制造工藝和封裝形式及封裝材料确定的,並在技朮白皮書中給出。為防止用戶自行設定而帶來危險,Intel已將Pentium 4微處理器的警戒溫度和极限溫度寫入TCC內的ROM單元中,用戶無法修改它們。

現在有不少主板的BIOS中也可以設置警戒溫度和關机溫度,不過可選的數值都比較保守,例如警戒溫度最大值為70℃、關机溫度為85℃,這是遠低于TCC內設定值的。

兼顧性能和可靠性是第2代溫度監控技朮的優秀之處。由公式P = CV2f(其中C是等效電容容量;V是工作電壓)可知,頻率f與能耗P之間是一種線性關系,降低頻率是減少發熱量的有效途徑。這種通過降低有效頻率實現降溫的措施,比之以前那種關斷時鐘信號的做法顯然要聰明一些,避免了因強行關閉微處理器,而導致數据丟失的情況。

Pentium 4處理器中的PROCHOT#引腳還有另外兩個實用的功能。其中的一個功能是向主板發出報警信號??PROCHOT#引腳為低電平時,說明微處理器核心溫度超過了警戒溫度,此時微處理器工作在較低的頻率上。如果超出警戒溫度(電腦用戶利用工具軟件可以獲得這個信息),應及時檢查散熱器安裝是否妥當,風扇轉速是否正常。

PROCHOT#引腳的另一個功能是可以保護主板上的其他元件。PROCHOT#引腳采用雙工設計??信號既可以從這根信號線出去,也能進得來。主板設計者可利用這一特性為供電模塊提供保護,當供電模塊的溫度超出警戒溫度時,監控電路輸出一個低電平到PROCHOT#引腳以激活TCC,通過降低微處理器功耗來達到保護供電模塊的目的。

可見,Pentium 4處理器不僅能自保平安,還能對供電電路提供保護,細微之處體現出設計者的良苦用心。同時,將TCC集成到微處理器內不僅對自身更加安全,也簡化了主板設計,降低了主板制造成本。可以說,第2代溫度監控技朮是一個給微處理器制造商與下游主板廠商帶來雙贏的技朮。

小知識∶如何設置BIOS中的“Processor speed throttling”?
Pentium 4主板的BIOS中通常有“Processor speed throttling ”之類的選擇項,用于選擇超警戒溫度后處理器任務周期(duty cycle)占全部周期的比例,在處理器頻率不變的情況下,這個比例越大說明處理器的工作效率越高。其中有“Automatic”和“On demand” 兩種選擇,“Automatic(自動)”表示任務周期的占空比為50%,也就是說比正常頻率低一半;“On demand(按要求)”下面有12.5%、25%、…、87.5%等多種選擇,選擇的數值越小,則任務周期的比例越小,降頻幅度也越大。