在 x86 英特爾參考手冊中它說:
“僅當使用指令的單移位形式時才會設定溢位標志。[...]”
但是當我有以下情況時:
xor eax, eax
mov al, 0b11000000
shl al, 2
;content of al: 00000000
這里答案的高位與進位的結果不一樣,即cf = 1,溢位標志沒有設定。
我不明白為什么這是正確的行為。為什么只有在使用單班時才設定溢位標志?
uj5u.com熱心網友回復:
OF=undefined 對于除 1 以外的班次計數;實際結果取決于您的 CPU。請參閱下文,了解我如何在英特爾 CPU 上設定它的理論。
這個設計決策有些道理,讓硬體稍微簡單一些。
正確檢測 2 的補碼溢位將需要檢查所有移出的位是否與新的 MSB 匹配。這與像現在使用 CF 一樣僅檢查移出的最后一位不同,因此需要像使用原始 8086 那樣的一次一個移位器的一些內部狀態。
這可能是 Stephen Morse(8086 ISA 的架構師)在為 8086 做出設計選擇時所想的。他的書 8086 Primer可在他的網站上免費獲得,并確認 (pg96) 8086 具有未定義的 OF 為可變計數操作碼。(對于 8086,顯然這包括shl al, clCL=1,這與英特爾目前的檔案不同。)關于移位指令及其用途的部分(pg64-66)沒有提到 OF,只有 CF。
必須檢查所有移出的位也可能會使桶形移位器更昂貴,但莫爾斯不太可能考慮到這一點。
IDK 為什么 Morse 沒有將 OF 定義為始終以某種特定方式設定,可能是根據 CF 與當前 MSB 不匹配,這可能沒有用,但對于 1 的計數仍然有意義。ALU 已經需要獲得最后一位移出CF。也許那是因為 8086 沒有在變數計數操作碼中為 OF 定義任何東西,即使計數恰好是 1。
請注意,對于某些計數大于 1 的情況,某些 CPU 在實踐中會產生 OF=1。例如,我的 i7-6700k Skylake 使用0x7f << 2. 檔案說_
OF 標志僅在 1 位移位時受到影響(參見上面的“說明”);否則,它是 undefined。
未定義不是受影響的相反;那將是“不受影響”。它總是設定為某個值,他們只是沒有記錄 CPU 如何選擇 0 和 1。
實際上,未修改將強制讀取并與舊的 FLAGS 值合并,以獲取除0現代 CPU 之外的即時移位計數,例如變數計數,以防它為 0,所以最好不要以這種方式指定。(shl reg, cl在 Sandybridge-family 上是 3 uops,因為在 CL&31 == 0 的情況下需要保持 FLAGS 不變)。所以這將是一個不需要的資料依賴,不像現在移位寫入所有標志,除非計數為 0。
我用這個 NASM 程式測驗了我的 CPU
_start:
mov cl, 7
mov dl, 0x7f ; GDB set $dl = 0xc0 or whatever after this
.loop:
mov eax, edx
shl al, cl
dec cl ; set a breakpoint here to look at EFLAGS after every continue
jnz .loop
;; fall off the end; I'm only single-stepping this in GDB anyway
使用 nasm ld 將 鏈接到靜態可執行檔案,使用 GDB 運行并使用layout reg/ layout next。使用starti和si。
My Skylake CPU does set OF=1 for shl al,cl with AL=0x7f CL=2 (or 1 or any non-zero count). Or for AL=0x80. But never sets it for AL=0x3 for any count, or for AL=0xc0 (0b1100_0000)
My current guess to explain the behaviour is that OF is set as if it was a shift by 1,
i.e. if OF = (input[MSB] != input[MSB-1]) of the input bits.
This makes sense; it gives the correct result in the case where the paper spec requires a specific result, and it's cheap to implement. (The OF output would still have to come from different bits depending on the operand-size.)
Of course, other microarchitectures from other vendors can be different. As could pure-software x86 emulators which still comply with the on-paper spec.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/457482.html
