組語合法小抄 ASCII General-Purpose EAX accumulator EBX 好像很自由 ECX loop counter ESP stack pointer ESI, EDI index registers EBP extended frame pointer (stack) ( EIP instruction pointer (PUSH 會使 ESP 減小,POP 反之 ) Program Template INCLUDE Irvine32.inc main PROC exit main ENDP END main Status Flags: Carry:unsigned arithmetic out of range Overflow:signed arithmetic out of range Sign:result is negative Zero:result is zero Auxiliary Carry:carry from bit 3 to bit 4 Parity:sum of 1 bits is an even number BYTE, SBYTE 8b WORD, SWORD 16b DWORD, SDWORD 32b QWORD64b TBYTE 80b 0Dh = carriage return 0Ah = line feed DUP C:char arrary1[100] 組語 :array1 BYTE 100 DUP(?)
Equal-Sign Directive C: #define MAX_SIZE 20 組語 (1): MAX_SIZE = 20 組語 (2): MAX_SIZE EQU <20> ; Cannot be redefined list BYTE 10,20,30,40 ListSize = ($ - list) list WORD 1000h,2000h,3000h,4000h ListSize = ($ - list) / 2 8086 指令集數學運算 : ADC: 連同進位的加法 ADD: 加法 SUB: 減法 DEC, INC: 減一, 增一 NEG: 二補數 DIV, IDIV: 除法 (Unsigned), 整數 ( 二補數 ) 除法 MUL, IMUL: 乘法, 整數乘法 SBB: 連同借位的減法 XADD: 交換, 相加 資料移動 : LDS, LES, LSS: 載入 DS, ES, SS 暫存器 LEA: 載入有效地址 ( 將所指資料的地址放到暫存器 ) LODSn: 載入字串 (SI 所指位置內容放到暫存器 A, 增減 SI) MOV: 移動資料 MOVSn: 移動字串 ([SI] [DI}, 增減 SI, DI) MOVSX: 移動資料 ( 到較大位置 ), 延伸正負號 MOVZX: 移動資料 ( 到較大位置 ), 延伸 0 STOSn: 貯存字串 ( 暫存器 A 內容放到 DI 所指位置, 增減 DI) XCHG: 交換資料 XLAT: 翻譯 ( 以 BX 為基底, AL 為索引, 取資料放到 AL) 比較 : BSF/BSR: 位元掃描 BT/BTC/BTR/BTS: 位元測試 CMP: 比較 ( 相減, 但不存結果, 只改變旗標 ) CMPSn: 比較字串 CMPXCHG: 比較和交換 CMPXCHG8B: 比較和交換 TEST: 測試位元 (AND, 但不存結果, 只改變旗標 ) 位元移動 : RCL, RCR: 經進位旗標向左旋轉, 向右旋轉 ROL, ROR: 向左旋轉, 向右旋轉 SAL, SAR: 算術性向左移位, 向右移位 SHL, SHR: 邏輯性向左移位, 向右移位 SHLD, SHRD: 雙字組向左移位, 向右移位
旗標操作 : CLC, CLD, CLI: 清除進位 方向 插斷旗標 CMC: 將進位旗標反相 ( 補數 ) POPF: 由堆疊彈出旗標 SAHF: 將 AH 存到旗標 LAHF: 將旗標載入 AH PUSHF: 將旗標堆入堆疊 STC, STD, STI: 設定進位 方向 插斷旗標 I/O: IN: 輸入 INSn: 輸入字串 OUT: 輸出 OUTSn: 輸出字串 邏輯運算 : AND OR NOT XOR 迴圈 : LOOP: CX 減一 ; 若 CX 不是 0, 跳到指定位置 LOOPE, LOOPZ: CX 減一 ; 若 CX 不是 0, 且 ZF=1, 跳到指定位置 LOOPNE, LOOPNZ: CX 減一 ; 若 CX 不是 0, 且 ZF=0, 跳到指定位置 LOOPNEW, LOOPNZW: 程式移轉 ( 無條件 ): CALL: 呼叫副程式 INT: 插斷 IRET: 插斷回轉 RET: 回轉 JMP: 跳越 RETN/RETF: 回轉 ( 近距 )/( 遠距 ) 程式移轉 ( 有條件 ): INTO: Interrupt on overflow JA/JNBE: Jump if above (not below or equal) JAE/JNB: Jump if above or equal JB/JNAE: Jump if below JBE/JNA: Jump if below or equal JC: Jump if CF=1 ( 有進位 ) JCXZ: Jump if CX=0 JE/JZ: Jump if ZF=1 (zero, equal) JG/JNLE: Jump if greater than (not less than or equal) JGE/JNL: Jump if greater than or equal JL/JNGE: Jump if less than JLE/JNG: Jump if less than or equal JNC: Jump if CF=0 JNE/JNZ: Jump if ZF=0 JNO: Jump if OF=0 (no overflow) JNP/JPE: Jump if PF=0 (parity even) JNS: Jump if SF=0 ( 正數 ) JO: Jump if OF=1 (Overflow) JP/JPO: Jump if PF=1 (Parity Odd) JS: Jump if SF=1 ( 負數 )
STACK: POP, PUSH: 彈出, 堆入 POPF, PUSHF: 彈出, 堆入旗標 POPA, PUSHA: 彈出, 堆入所有一般用途暫存器字串 : CMPSn: 比較字串 LODSn: 載入字串 MOVSn: 移動字串 SCASn: 掃描字串 STOSn: 貯存字串 REP: 重覆 ( 前置指令, 必須配合字串指令 ) REPE, REPZ: 若相等 (ZF=1), 則重覆 REPNE, PRENZ: 若不相等 (ZF=0), 則重覆 資料型式轉換 CBW: Byte word CWD: Word Dword CDQ: Word Qword CWDE: Word extended doubleword NOP: 無動作 ( EIP++ ) OFFSET : returns the distance in bytes, of a label from the beginning of its enclosing segment bval BYTE? wval WORD? dval DWORD? dval2 DWORD? mov esi,offset bval mov esi,offset wval mov esi,offset dval ; ESI = 00404000 ; ESI = 00404001 ; ESI = 00404003 mov esi,offset dval2 ; ESI = 00404007 PTR:Overrides the default type of a label (variable). Provides the flexibility to access part of a variable. mydouble DWORD 12345678h mov ax,mydouble ; error why? mov ax,word PTR mydouble ; loads 5678h mov WORD PTR mydouble,4321h ; saves 4321h TYPE var1 BYTE? var2 WORD? mov eax,type var1 ; 1 mov eax,type var2 ; 2 LENGTHOF byte1 BYTE 10,20,30 ; 3 array1 WORD 30 DUP(?),0,0 ; 32 array2 WORD 5 DUP(3 DUP(?)) ; 15 SIZEOF byte1 BYTE 10,20,30 ; 3 array1 WORD 30 DUP(?),0,0 ; 64 array2 WORD 5 DUP(3 DUP(?)) ; 30 array3 DWORD 1,2,3,4 ; 16
Indirect Operands esi = 0000 0000 esi = OFFSET a; a= G esi = 0000 00AF [esi] = G mycount WORD 0 mov esi,offset mycount inc [esi] ; error: ambiguous inc WORD PTR [esi] ; ok Indexed Operands arrayw WORD 1000h,2000h,3000h mov esi,0 mov ax,[arrayw + esi] mov ax,arrayw[esi] add esi,2 add ax,[arrayw + esi] ; AX = 1000h ; alternate format Index Scaling arrayb BYTE 0,1,2,3,4,5 arrayw WORD 0,1,2,3,4,5 arrayd DWORD 0,1,2,3,4,5 mov esi,4 mov al,arrayb[esi*type arrayb] ; 04 mov bx,arrayw[esi*type arrayw] ; 0004 mov edx,arrayd[esi*type arrayd] ; 00000004 Pointers arrayw WORD 1000h,2000h,3000h ptrw DWORD arrayw mov esi,ptrw mov ax,[esi] ; AX = 1000h # Alternate format : ptrw DWORD OFFSET arrayw Library Procedures Overview Clrscr - Clears console, locates cursor at upper left corner CreateOutputFile - Creates new disk file for writing in output mode Crlf - Writes end of line sequence to standard output Delay - Pauses program execution for n millisecond interval DumpMem - Writes block of memory to standard output in hex DumpRegs Displays general-purpose registers and flags (hex) GetDateTime Gets the current date and time from the system GetTextColor - Returns active foreground and background text colors in the console window Gotoxy - Locates cursor at row and column on the console IsDigit - Sets Zero flag if AL contains ASCII code for decimal digit (0 9) MsgBox, MsgBoxAsk Display popup message boxes ParseDecimal32 Converts unsigned integer string to binary ParseInteger32 - Converts signed integer string to binary
Random32 - Generates 32-bit pseudorandom integer in the range 0 to FFFFFFFFh Randomize - Seeds the random number generator RandomRange - Generates a pseudorandom integer within a specified range ReadChar - Reads a single character from standard input ReadDec - Reads 32-bit unsigned decimal integer from keyboard ReadHex - Reads 32-bit hexadecimal integer from keyboard ReadInt - Reads 32-bit signed decimal integer from keyboard ReadKey Reads character from keyboard input buffer ReadString - Reads string from standard input, terminated by [Enter] SetTextColor - Sets foreground and background colors of all subsequent console text output Str_compare Compares two strings Str_copy Copies a source string to a destination string StrLength Returns length of a string Str_trim - Removes unwanted characters from a string. Str_ucase - Converts a string to uppercase letters. WaitMsg - Displays message, waits for Enter key to be pressed WriteBin - Writes unsigned 32-bit integer in ASCII binary format. WriteBinB Writes binary integer in byte, word, or doubleword format WriteChar - Writes a single character to standard output WriteDec - Writes unsigned 32-bit integer in decimal format WriteHex - Writes an unsigned 32-bit integer in hexadecimal format WriteHexB Writes byte, word, or doubleword in hexadecimal format WriteInt - Writes signed 32-bit integer in decimal format WriteStackFrame - Writes the current procedure s stack frame to the console. WriteStackFrameName - Writes the current procedure s name and stack frame to the console. WriteString - Writes null-terminated string to console window(edx) WriteWindowsMsg - Displays most recent error message generated by MS-Windows ReadString filename BYTE 80 DUP(0) mov edx,offset filename mov ecx,sizeof filename 1 call ReadString Write & Read(char.int.dec.hex.bin):EAX Textcolor: 字 + 底 *1 換行 :call clrf 比大小 :CMP eax, a JA(JB, ) 檢查 0. +.- :AND / test JS, JNS, JZ.. WriteString mov call edx,offset str1 WriteString