彙編指令速查及SSE指令巨集實現
阿新 • • 發佈:2019-02-08
80X86彙編指令
一、資料傳輸指令 ─────────────────────────────────────── 它們在存貯器和暫存器、暫存器和輸入輸出埠之間傳送資料. 1. 通用資料傳送指令. MOV 傳送字或位元組. MOVSX 先符號擴充套件,再傳送. MOVZX 先零擴充套件,再傳送. PUSH 把字壓入堆疊. POP 把字彈出堆疊. PUSHA 把AX,CX,DX,BX,SP,BP,SI,DI依次壓入堆疊. POPA 把DI,SI,BP,SP,BX,DX,CX,AX依次彈出堆疊. PUSHAD 把EAX,ECX,EDX,EBX,ESP,EBP,ESI,EDI依次壓入堆疊. POPAD 把EDI,ESI,EBP,ESP,EBX,EDX,ECX,EAX依次彈出堆疊. BSWAP 交換32位暫存器裡位元組的順序 XCHG 交換字或位元組.( 至少有一個運算元為暫存器,段暫存器不可作為運算元) CMPXCHG 比較並交換運算元.( 第二個運算元必須為累加器AL/AX/EAX ) XADD 先交換再累加.( 結果在第一個運算元裡 ) XLAT 位元組查錶轉換. ── BX 指向一張 256 位元組的表的起點, AL 為表的索引值 (0-255,即 0-FFH); 返回 AL 為查表結果. ( [BX+AL]->AL ) 2. 輸入輸出埠傳送指令. IN I/O埠輸入. ( 語法: IN 累加器, {埠號│DX} ) OUT I/O埠輸出. ( 語法: OUT {埠號│DX},累加器 ) 輸入輸出埠由立即方式指定時, 其範圍是 0-255; 由暫存器 DX 指定時, 其範圍是 0-65535. 3. 目的地址傳送指令. LEA 裝入有效地址. 例: LEA DX,string ;把偏移地址存到DX. LDS 傳送目標指標,把指標內容裝入DS. 例: LDS SI,string ;把段地址:偏移地址存到DS:SI. LES 傳送目標指標,把指標內容裝入ES. 例: LES DI,string ;把段地址:偏移地址存到ES:DI. LFS 傳送目標指標,把指標內容裝入FS. 例: LFS DI,string ;把段地址:偏移地址存到FS:DI. LGS 傳送目標指標,把指標內容裝入GS. 例: LGS DI,string ;把段地址:偏移地址存到GS:DI. LSS 傳送目標指標,把指標內容裝入SS. 例: LSS DI,string ;把段地址:偏移地址存到SS:DI. 4. 標誌傳送指令. LAHF 標誌暫存器傳送,把標誌裝入AH. SAHF 標誌暫存器傳送,把AH內容裝入標誌暫存器. PUSHF 標誌入棧. POPF 標誌出棧. PUSHD 32位標誌入棧. POPD 32位標誌出棧. 二、算術運算指令 ─────────────────────────────────────── ADD 加法. ADC 帶進位加法. INC 加 1. AAA 加法的ASCII碼調整. DAA 加法的十進位制調整. SUB 減法. SBB 帶借位減法. DEC 減 1. NEC 求反(以 0 減之). CMP 比較.(兩運算元作減法,僅修改標誌位,不回送結果). AAS 減法的ASCII碼調整. DAS 減法的十進位制調整. MUL 無符號乘法. IMUL 整數乘法. 以上兩條,結果回送AH和AL(位元組運算),或DX和AX(字運算), AAM 乘法的ASCII碼調整. DIV 無符號除法. IDIV 整數除法. 以上兩條,結果回送: 商回送AL,餘數回送AH, (位元組運算); 或 商回送AX,餘數回送DX, (字運算). AAD 除法的ASCII碼調整. CBW 位元組轉換為字. (把AL中位元組的符號擴充套件到AH中去) CWD 字轉換為雙字. (把AX中的字的符號擴充套件到DX中去) CWDE 字轉換為雙字. (把AX中的字元號擴充套件到EAX中去) CDQ 雙字擴充套件. (把EAX中的字的符號擴充套件到EDX中去) 三、邏輯運算指令 ─────────────────────────────────────── AND 與運算. OR 或運算. XOR 異或運算. NOT 取反. TEST 測試.(兩運算元作與運算,僅修改標誌位,不回送結果). SHL 邏輯左移. SAL 算術左移.(=SHL) SHR 邏輯右移. SAR 算術右移.(=SHR) ROL 迴圈左移. ROR 迴圈右移. RCL 通過進位的迴圈左移. RCR 通過進位的迴圈右移. 以上八種移位指令,其移位次數可達255次. 移位一次時, 可直接用操作碼. 如 SHL AX,1. 移位>1次時, 則由暫存器CL給出移位次數. 如 MOV CL,04 SHL AX,CL 四、串指令 ─────────────────────────────────────── DS:SI 源串段暫存器 :源串變址. ES:DI 目標串段暫存器:目標串變址. CX 重複次數計數器. AL/AX 掃描值. D標誌 0表示重複操作中SI和DI應自動增量; 1表示應自動減量. Z標誌 用來控制掃描或比較操作的結束. MOVS 串傳送. ( MOVSB 傳送字元. MOVSW 傳送字. MOVSD 傳送雙字. ) CMPS 串比較. ( CMPSB 比較字元. CMPSW 比較字. ) SCAS 串掃描. 把AL或AX的內容與目標串作比較,比較結果反映在標誌位. LODS 裝入串. 把源串中的元素(字或位元組)逐一裝入AL或AX中. ( LODSB 傳送字元. LODSW 傳送字. LODSD 傳送雙字. ) STOS 儲存串. 是LODS的逆過程. REP 當CX/ECX<>0時重複. REPE/REPZ 當ZF=1或比較結果相等,且CX/ECX<>0時重複. REPNE/REPNZ 當ZF=0或比較結果不相等,且CX/ECX<>0時重複. REPC 當CF=1且CX/ECX<>0時重複. REPNC 當CF=0且CX/ECX<>0時重複. 五、程式轉移指令 ─────────────────────────────────────── 1>無條件轉移指令 (長轉移) JMP 無條件轉移指令 CALL 過程呼叫 RET/RETF過程返回. 2>條件轉移指令 (短轉移,-128到+127的距離內) ( 當且僅當(SF XOR OF)=1時,OP1<OP2 ) JA/JNBE 不小於或不等於時轉移. JAE/JNB 大於或等於轉移. JB/JNAE 小於轉移. JBE/JNA 小於或等於轉移. 以上四條,測試無符號整數運算的結果(標誌C和Z). JG/JNLE 大於轉移. JGE/JNL 大於或等於轉移. JL/JNGE 小於轉移. JLE/JNG 小於或等於轉移. 以上四條,測試帶符號整數運算的結果(標誌S,O和Z). JE/JZ 等於轉移. JNE/JNZ 不等於時轉移. JC 有進位時轉移. JNC 無進位時轉移. JNO 不溢位時轉移. JNP/JPO 奇偶性為奇數時轉移. JNS 符號位為 "0" 時轉移. JO 溢位轉移. JP/JPE 奇偶性為偶數時轉移. JS 符號位為 "1" 時轉移. 3>迴圈控制指令(短轉移) LOOP CX不為零時迴圈. LOOPE/LOOPZ CX不為零且標誌Z=1時迴圈. LOOPNE/LOOPNZ CX不為零且標誌Z=0時迴圈. JCXZ CX為零時轉移. JECXZ ECX為零時轉移. 4>中斷指令 INT 中斷指令 INTO 溢位中斷 IRET 中斷返回 5>處理器控制指令 HLT 處理器暫停, 直到出現中斷或復位訊號才繼續. WAIT 當晶片引線TEST為高電平時使CPU進入等待狀態. ESC 轉換到外處理器. LOCK 封鎖匯流排. NOP 空操作. STC 置進位標誌位. CLC 清進位標誌位. CMC 進位標誌取反. STD 置方向標誌位. CLD 清方向標誌位. STI 置中斷允許位. CLI 清中斷允許位. 六、偽指令 ─────────────────────────────────────── DB 定義位元組. DW 定義字(2位元組). PROC 定義過程. ENDP 過程結束. SEGMENT 定義段. ASSUME 建立段暫存器定址. ENDS 段結束. END 程式結束.
SSE指令的巨集實現
;-------------------------------------------------------------- ;應用此檔案要以masm32軟體包為背景,請更改下 ;邊include語句後的路徑為軟體包中macros.asm的 ;實際路徑,以確保能正確包含進此檔案(請確保你 ;沒有刪除軟體包中的macros.asm)。 ;例如:你的masm32軟體包安裝在E盤根目錄下 ;則應將下邊語句改成include E:\masm32\macros\macros.asm ;使用時在源程式的asm檔案中加入“include 此檔案路徑” ;例如:你將此檔案放在E:\masm32\include目錄下 ;則在原始檔中加入此行語句include E:\masm32\include\MyMacro.inc ;做好這些工作後,就能在源程式中使用SSE3、SSSE3、SSE4.1指令了。 ;例如: ;pshufb xmm(4),MakeXmmword(8080808080808080h,0001020304050607h) ;psrlq xmm(7),16 ;pextrb edx,xmm(5),0 ;■■■■■■■■■■■■■ ;■SSE4.1中有條指令未實現■ ;■■■■■■■■■■■■■ ;-------------------------------------------------------------- ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;1.此檔案免費,僅供參考學習,請勿用於任何形式的商業行為 ; ;2.尊重個人勞動成果,請勿隨意改動,引用請註明出處 ; ;3.不負責此檔案的絕對正確性,因引用此檔案造成的程式bug或任何其它 ; ;損失概不負責 ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include G:\masm32\macros\macros.asm ArgsReverse MACRO arglist:VARARG LOCAL txt, arg txt TEXTEQU <> FOR arg,<arglist> txt CATSTR <arg>,<!,>,txt ENDM txt SUBSTR txt,1, @SizeStr(%txt)-1 EXITM <txt> ENDM Reverse MACRO arg:req LOCAL txt txt TEXTEQU <> FORC Char,<arg> txt CATSTR <Char>,txt ENDM EXITM <txt> ENDM MakeDword MACRO args:VARARG LOCAL ValueName,NumberOfArgs,ReversedArgs .data align 4 NumberOfArgs = argcount(args) ReversedArgs textequ ArgsReverse(args) IF NumberOfArgs eq 4 ValueName byte ReversedArgs ELSEIF NumberOfArgs eq 2 ValueName word ReversedArgs ELSEIF NumberOfArgs eq 1 ValueName dword ReversedArgs ENDIF .code EXITM <ValueName> ENDM MakeQword MACRO args:VARARG LOCAL ValueName,NumberOfArgs,ReversedArgs .data align 16 NumberOfArgs = argcount(args) ReversedArgs textequ ArgsReverse(args) IF NumberOfArgs eq 8 ValueName byte ReversedArgs ELSEIF NumberOfArgs eq 4 ValueName word ReversedArgs ELSEIF NumberOfArgs eq 2 ValueName dword ReversedArgs ELSEIF NumberOfArgs eq 1 ValueName qword ReversedArgs ENDIF .code EXITM <qword ptr ValueName> ENDM MakeXmmword MACRO args:VARARG LOCAL ValueName,NumberOfArgs,ReversedArgs .data align 16 NumberOfArgs = argcount(args) ReversedArgs textequ ArgsReverse(args) FORC Char,<args> IFIDN <Char>,<.> IF NumberOfArgs eq 4 ValueName real4 ReversedArgs ELSEIF NumberOfArgs eq 2 ValueNmae real8 ReversedArgs ENDIF EXITM ENDIF ENDM IFNDEF ValueName IF NumberOfArgs eq 16 ValueName byte ReversedArgs ELSEIF NumberOfArgs eq 8 ValueName word ReversedArgs ELSEIF NumberOfArgs eq 4 ValueName dword ReversedArgs ELSEIF NumberOfArgs eq 2 ValueName qword ReversedArgs ENDIF ENDIF .code EXITM <oword ptr ValueName> ENDM Shared MACRO Dest,Src,BinCode:VARARG LOCAL Last,Current,VarNum,Count VarNum = argcount(BinCode) Count = 0 WHILE Count NE VarNum - 2 db getarg(Count + 1,BinCode) Count = Count + 1 ENDM Last equ $ addps Dest,Src Current equ $ ORG Last db getarg(VarNum - 1,BinCode),getarg(VarNum,BinCode) ORG Current ENDM ExInShared MACRO Dest,Src,Imm8,BinCode:VARARG LOCAL Last,Current,VarNum,Count VarNum = argcount(BinCode) Count = 0 WHILE Count NE VarNum - 3 db getarg(Count + 1,BinCode) Count = Count + 1 ENDM Last equ $ IF isregister(Src) pinsrw Dest,Src,Imm8 ELSE pinsrw Dest,word ptr Src,Imm8 ENDIF Current equ $ ORG Last db getarg(VarNum - 2,BinCode),getarg(VarNum - 1,BinCode),getarg(VarNum,BinCode) ORG Current ENDM ;SSE3指令 movshdup MACRO Dest,Src Shared Dest,Src,0f3h, 0fh, 16h ENDM movsldup MACRO Dest,Src Shared Dest,Src,0f3h, 0fh, 12h ENDM movddup MACRO Dest,Src Shared Dest,Src,0f2h, 0fh, 12h ENDM addsubps MACRO Dest,Src Shared Dest,Src,0f2h, 0fh,0d0h ENDM addsubpd MACRO Dest,Src Shared Dest,Src,066h, 0fh,0d0h ENDM haddps MACRO Dest,Src Shared Dest,Src,0f2h, 0fh, 7ch ENDM hsubps MACRO Dest,Src Shared Dest,Src,0f2h, 0fh, 7dh ENDM haddpd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 7ch ENDM hsubpd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 7dh ENDM ;SSSE3指令 phaddw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 01h ENDM phaddsw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 03h ENDM phaddd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 02h ENDM phsubw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 05h ENDM phsubsw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 07h ENDM phsubd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 06h ENDM pabsb MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 1ch ENDM pabsw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 1dh ENDM pabsd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 1eh ENDM pmaddubsw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 04h ENDM pmulhrsw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 0bh ENDM pshufb MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 00h ENDM psignb MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 08h ENDM psignw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 09h ENDM psignd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 0ah ENDM ;SSE4.1指令 pmulld MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 40h ENDM pmuldq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 28h ENDM dpps MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 40h db Imm8 ENDM dppd MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 41h db Imm8 ENDM movntdqa MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 2ah ENDM blendps MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 0ch db Imm8 ENDM blendpd MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 0dh db Imm8 ENDM blendvps MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 14h ENDM blendvpd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 15h ENDM pblendvb MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 10h ENDM pblendw MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 0eh db Imm8 ENDM pminsb MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 38h ENDM pmaxsb MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 3ch ENDM pminuw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 3ah ENDM pmaxuw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 3eh ENDM pminud MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 3bh ENDM pmaxud MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 3fh ENDM pminsd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 39h ENDM pmaxsd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 3dh ENDM roundps MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 08h db Imm8 ENDM roundss MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 0ah db Imm8 ENDM roundpd MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 09h db Imm8 ENDM roundsd MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 0bh db Imm8 ENDM insertps MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 21h db Imm8 ENDM ;extractps MACRO Dest,Src,Imm8 ; ; ENDM pinsrb MACRO Dest,Src,Imm8 ExInShared Dest,Src,Imm8,066h, 0fh, 3ah, 20h ENDM pinsrd MACRO Dest,Src,Imm8 ExInShared Dest,Src,Imm8,066h, 0fh, 3ah, 22h ENDM pextrb MACRO Dest,Src,Imm8 ExInShared Src,Dest,Imm8,066h, 0fh, 3ah, 14h ENDM pextrwm MACRO Dest,Src,Imm8 ExInShared Src,Dest,Imm8,066h, 0fh, 3ah, 15h ENDM pextrd MACRO Dest,Src,Imm8 ExInShared Src,Dest,Imm8,066h, 0fh, 3ah, 16h ENDM pmovsxbw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 20h ENDM pmovzxbw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 30h ENDM pmovsxbd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 21h ENDM pmovzxbd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 31h ENDM pmovsxbq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 22h ENDM pmovzxbq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 32h ENDM pmovsxwd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 23h ENDM pmovzxwd MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 33h ENDM pmovsxwq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 24h ENDM pmovzxwq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 34h ENDM pmovsxdq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 25h ENDM pmovzxdq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 35h ENDM mpsadbw MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 42h db Imm8 ENDM phminposuw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 41h ENDM pcmpeqq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 29h ENDM ptest MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 17h ENDM packusdw MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 2bh ENDM ;SSE4.2指令 pcmpestri MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 61h db Imm8 ENDM pcmpestrm MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 60h db Imm8 ENDM pcmpistri MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 63h db Imm8 ENDM pcmpistrm MACRO Dest,Src,Imm8 Shared Dest,Src,066h, 0fh, 3ah, 62h db Imm8 ENDM pcmpgtq MACRO Dest,Src Shared Dest,Src,066h, 0fh, 38h, 37h ENDM