1、Part B,Fundamentals of Assembly Language,Chapter 4,Symbolic Instructions基本的符号指令,Textbook: 6 Symbolic Instructions and Addressing,8086 Symbolic instructions,A list of the symbolic instructions for the Intel processor family arranged by category. Data Transfer 数据传送类指令 Arithmetic and Logical 算术、逻辑运算类指令
2、 Bit Shifting 位操作类指令 String Operation 串操作类指令 Transfer (Conditional and Unconditional )控制转移类指令 Processor Control 处理机控制类指令,Overview,Format of the Instructions,Statement element: identifier mnemonic operand(s) ;comment 标识符 助记符 操作数 ;注释,Instructions without an operand,Eg. 1) NOP (空操作指令) HLT (停机指令) . 2) R
3、ET (子程序返回指令)MOVSW (串传送指令) DAA (加法的十进制调整指令),Instructions with one operand,1) with a destination operandINC BX DEC CX2 ) with a source operand PUSH AX POP DX,Instructions with two operands,destination operand (operand1) source operand (operand2). Examples:,Note:,Functions Addressing mode Affect Flag r
4、egister?,Copying Data mov xchg复制数据指令,4.1,MOV instruction (move),The MOV instruction transfers (or copies) data referenced by the address in the second operand to the address in the first operand. Format:,从源操作数地址把一个字节、字或双字复制到目的操作数地址,存放在源地址的值不会改变。 比如C语言中赋值语句 int count=0;,label: MOV reg/mem, reg/mem/im
5、m;reg/memreg/mem/imm,MOV instruction (move),You can move to a register a byte, a word, or a doubleword. AL BL CL DL AH BH CH DH AX BX CX DX SP BP SI DI EAX EBX ECX EDX ESP EBP ESI EDI,label: MOV reg/mem, reg/mem/imm;reg/memreg/mem/imm,Valid MOV Operations,Bytefld db ? ;Define a byte Wordfld db ? ;De
6、fine a word1) Register Moves mov edx,ecx ;register-to-register mov es,ax ;register-to-segment register mov bytefld,dh ;Register-to-memory,direct mov bx,al ;register-to-memory,indirect,Examples,Valid MOV Operations,Bytefld db ? ;Define a byte Wordfld db ? ;Define a word2) Immediate Moves mov cx,40h ;
7、immediate-to-register mov bytefld,25;immediate-to-memory,direct mov wordfldbx,25;immediate-to-memory,indirect,Examples,Valid MOV Operations,Bytefld db ? ;Define a byte Wordfld db ? ;Define a word3) Direct Momory Moves mov ch,bytefld ;memory-to-register mov cx,wordfldbx;memory-to-register,indirect,Ex
8、amples,Valid MOV Operations,Bytefld db ? ;Define a byte Wordfld db ? ;Define a word4) Segment Register Moves mov ax,ds ;segment register-to-register mov wordfld,ds;segment register-to-memory,Examples,Invalid MOV:,Invalid Mov: Memory - to - memory Immediate - to - segment register Segment register- t
9、o - segment registerDestination operand nots CS or immediate!注意:源和目的操作数不可以任意组合,MOV instruction,MOV instruction not affect flag.The operand affects only the portion of the referenced register. Moving a byte to the CH does not affect the CL.,imm-reg/mem mov指令,mov al,4 ;al4,字节传送 mov cx,0ffh ;cx00ffh,字传
10、送 mov si,200h ;si0200h,字传送mov byte ptr si,0ah ;byte ptr 说明是字节操作 mov word ptr si+2,0bh ;word ptr 说明是字操作,注意立即数是字节还是字明确指令是字节操作还是字操作,Examples,Mem-reg,mov al,bx mov dx,bp ;dxss:bp mov es,si ;esds:si,不存在存储器向存储器的传送指令,Examples,Seg reg-reg/mem,mov si,ds mov ax,es ;axes mov ds,ax ;dsaxes,对段寄存器的操作有一些限制,Example
11、s,Summary,MOV并非任意传送, 不改变状态标志位,非法传送种种,两个操作数的类型不一致 例如源操作数是字节,而目的操作数是字;或相反 两个操作数不能都是存储器 传送指令很灵活,但主存之间的直接传送却不允许 段寄存器的操作有一些限制 段寄存器属专用寄存器,对他们的操作能力有限,两个操作数的类型要一致,绝大多数双操作数指令,除非特别说明,目的操作数与源操作数必须类型一致,否则为非法指令 MOV AL,050AH ;(X) 050Ah为字,而AL为字节寄存器有明确的字节或字类型,有寄存器参与的指令其操作数类型就是寄存器的类型对于存储器单元与立即数同时作为操作数的情况,必须显式指明;byte
12、 ptr指示字节类型,word ptr指示字类型,两个操作数不能都是存储器,8086指令系统不允许两个操作数都是存储单元(除串操作指令),要实现这种传送,可通过寄存器间接实现mov ax,buffer1 ;axbuffer1;(buffer1内容送ax) mov buffer2,ax ;buffer2ax;这里buffer1和buffer2是两个字变量, ;实际表示直接寻址方式,Examples,要小心段寄存器的操作,不允许立即数传送给段寄存器 MOV DS,100H ;非法指令:立即数不能传送段寄存器不允许直接改变CS值 MOV CS,SI ;不允许使用的指令不允许段寄存器之间的直接数据传送
13、 MOV DS,ES ;非法指令:不允许段寄存器间传送,xchg,XCHG Instruction Format,XCHG swaps the two data items. 将两个不同地址的数据进行交换,2. 交换指令xchg(exchange),XCHG swaps the two data items.,mov cx, axmov ax, bxmov bx, cx,xchg cx, bx,Examples,Note:,Not allow exchanging between memory to memory Not allow using segment-register. Any ad
14、dressing mold excepting immediate-addressing Not affect flag-bit. 寄存器 与 寄存器/存储器 之间交换数据,但不能用xchg交换两个存储器操作数, 不能用立即数,reg-to-reg,mov ax,1234h ;ax=1234h mov bx,5678h ;bx=5678h xchg ax,bx ;ax=5678h,bx=1234h xchg ah,al ;ax=7856h,Examples,寄存器与存储器交换xchg,xchg ax,2000h ; xchg 2000h,ax xchg al,2000h ;字节交换 ;等同于
15、xchg 2000h,al,Examples,XCHG指令,load effective address (有效地址传送指令) Format:,LEA r16,mem;(REG) Effective-Address of source operand,Address transfer instruction LEA,Load effective address Not the physical address Not the content of the unit,Examples,LEA指令,The LEA Instruction,Lea is useful for initializing
16、 a register with an offset address. A common use of LEA is to initialize an offset in BX, DI, or SI for indexing an address in memory.,Datatbl db 25 dup(?) ;table of 25 bytes Bytefld db ? ;one byte Lea bx,datatbl ;load offset address Mov bytefld, bx ;move first byte of datatbl,Examples,Who can find
17、the error?,Datatbl db 25 dup(?) ;table of 25 bytes Bytefld db ? ;one byte Lea bx,datatbl ;load offset address Mov bytefld, bx ;move first byte of datatbl,Examples,Mov bytefld, bx ;move first byte of datatbl,The LEA Instruction,An equivalent operation to LEA is MOV with the OFFSET operator.,Lea bx, d
18、atatbl Mov bx, offset datatbl,Integer Addition and Subtraction Instructions 整数的加法和减法指令 add sub adc sbb inc dec neg cmp,4.2,Arithmetic Instructions,Addition instruction: ADD/ADC/INC Subtraction instruction: SUB/SBB/DEC NEG/CMPMultiplication: MUL/IMUL Division: DIV/IDIV,The ADD/SUB Instruction (加减法指令)
19、,Format: When an add/sub instruction is executed, The integer at source is added/ subtracted to the integer at destination and the sum/result replaces the old value at destination.,add/sub destination, source,The ADD/SUB Instruction (加减法指令),Operation: (DST) (DST) (SRC)Flags affected are SF, ZF, OF,
20、PF, AF, and CF.,add/sub DST, SRC,The ADD/SUB Instruction (加减法指令),There are no direct memory to memory.,add/sub dst, srcReg./Mem., Reg./Mem./Imm.; regreg imm/reg/mem ; Mem. Reg./Imm.,Example:,MOV DL, 12H ADD DL, 33HExecuted,(DL) = sum= 45H,Flags: Z = 0(result0) C = 0(no carry) A = 0(no auxiliary carr
21、y) S = 0(result is positive) P = 0(parity is odd) O = 0(no overflow),The Add instructions,mov al,0fbh add al,07h mov word ptr 200h,4652h mov bx,1feh add al,bl add word ptr bx+2,0f0f0h,For each of the unrelated instructions, show the result in the destination operand.,Examples,The Add instructions,mo
22、v al,0fbh ;al=0fbh add al,07h ;al=02h mov word ptr 200h,4652h ;200h=4652h mov bx,1feh ;bx=1feh add al,bl ;al=00h add word ptr bx+2,0f0f0h ;200h=3742h,Answers,For each of the unrelated instructions, show the result in the destination operand.,The SUB instructions,mov al,0fbh sub al,07h mov word ptr 2
23、00h,4652h mov bx,1feh sub al,bl sub word ptr bx+2,0f0f0h,For each of the unrelated instructions, show the result in the destination operand.,Examples,The SUB instructions,mov al,0fbh ;al=0fbh sub al,07h ;al=0f4h,CF0 mov word ptr 200h,4652h;200h=4652h mov bx,1feh ;bx=1feh sub al,bl ;al=0f6h ,CF1 sub
24、word ptr bx+2,0f0f0h;200h=5562h,CF1,Answers,ADC - Addition with carry (带进位加法指令),Operation:(DST) (DST)+(SCR)+CF,ADC reg,imm/reg/mem ;regregimm/reg/memCF ADC mem,imm/reg ;memmemimm/regCF,Format: ADC dst, src,ADC,mov ax,4652h add ax,0f0f0h mov dx,0234h adc dx,0f0f0h,Show the result in the destination o
25、perand.,Question,mov ax,4652h ;ax=4652h add ax,0f0f0h ;ax=3742h,CF=1 mov dx,0234h ;dx=0234h adc dx,0f0f0h ;dx=f325h,CF=0 ;DX.AX0234 4652HF0F0 F0F0HF325 3742H,Answers,ADC,performing arithmetic on double-word valuesAddition BX-AX to DX-CX, store result in BX-AX.,The ADC Instruction,Examples,SUM PROC N
26、EARADD AX,CXADC BX,DXRET SUM ENDP,SBB - Subtraction with borrow(带借位减法指令),Operation:(DST) (DST)-(SCR)- CF,SBB reg,imm/reg/mem regregimm/reg/memCF SBB mem,imm/reg ;memmemimm/regCF,Format: SBB dst, src,SBB,mov ax,4652h sub ax,0f0f0h mov dx,0234h sbb dx,0f0f0h,Show the result in the destination operand.
27、,Examples,SBB,mov ax,4652h ;ax=4652h sub ax,0f0f0h ;ax=5562h,CF=1 mov dx,0234h ;dx=0234h sbb dx,0f0f0h ;dx=1143h,CF=1 ;DX.AX0234 4652HF0F0 F0F0H1143 5562H,Examples,INC increment (增量指令),Format:Operation: (DST) (DST) + 1INC instruction clear or set the OF, SF, and ZF flags, but not effect CF flag bit.
28、 INC指令不影响进位CF标志,按定义设置其他状态标志,INC DST,INC - ADD,INC increment (增量指令),Examples,INC reg/mem ;reg/memreg/mem1,inc bx inc DI ; (X) assembler dont know byte,; word or d-word. inc byte ptr bx,DEC decrement (增量指令),Format:Operation: (DST) (DST) - 1DEC instruction clear or set the OF, SF, and ZF flags, but not
29、 effect CF flag bit. DEC不影响进位CF标志,按定义设置其他状态标志,DEC DST,DEC decrement (减量指令),Examples,DEC reg/mem ;reg/memreg/mem-1,DEC cx DEC word ptr bx,INC DEC,Useful for incrementing and decrementing counters.Take fewer bytes of code and execute in fewer clock cycles than corresponding addition or subtraction ins
30、tructions.,INC指令和DEC指令都是单操作数指令主要用于对计数器和地址指针的调整,NEG negate (取补指令),Format:Function: reverses the sign of a binary value, from positive to negative and vice versa.,NEG reg/mem ;reg/mem0 - reg/mem,NEG negate (取补指令),Format:Operation: NEG reverses the bits, and then adds 1 for proper 2s complement notatio
31、n. (DST) 0 - (DST) NEG instruction affect AF, CF, OF, PF, SF and ZF,NEG reg/mem ;reg/mem0 - reg/mem,NEG,mov ax,0ff64h neg al sub al,9dh neg ax dec al neg ax,Questions,Show the result in the destination operand.,NEG,mov ax,0ff64h neg al ;ax=ff9ch,OF=0、SF=1、ZF=0、PF=1、CF=1 sub al,9dh ;ax=ffffh,OF=0、SF=
32、1、ZF=0、PF=1、CF=1 neg ax ;ax=0001h,OF=0、SF=0、ZF=0、PF=0、CF=1 dec al ;ax=0000h,OF=0、SF=0、ZF=1、PF=1、CF=1 neg ax ;ax=0000h,OF=0、SF=0、ZF=1、PF=1、CF=0,Answers,CMP compare (比较指令),Format:Function: Compares the binary contents of two data fields.,CMP DST, SRC ; (DST) - (SRC),CMP compare (比较指令),Format:Operation
33、: CMP internally subtracts operand 2 from operand 1 and sets/clears flags, but not store the result. Both operands are byte, word, or doubleword.(DST) - (SRC),CMP DST, SRC ; (DST) - (SRC),CMP compare (比较指令),affect AF, CF, OF, PF, SF and ZF,CMP DST, SRC ; (DST) - (SRC),CMP compare (比较指令),CMP may comp
34、are register, memory, or immediate to a register or may compare register or immediate to memory.,CMP reg, imm/reg/mem ;reg-imm/reg/mem CMP mem, imm/reg ;mem-imm/reg,CMP,cmp al ,100 ;al-100 jb below ;al100,跳到below sub al,100 ;al100,alal-100 inc ah ;ahah+1 below: .,Examples,加法指令,减法指令,Addition and Subt
35、raction of 2s Complement补码的加减,1.4,Processing Unsigned and Signed Binary Data,For unsigned data, all bits are intended to be data bits. For signed data, the leftmost bit is a sign bit.Some example show how the addition set the CF and OF,(1) The addition does not set the OF or CF,The result is the sam
36、e for both unsigned and signed data.,(2) The addition set OF and clear CF,Arithmetic Carry: Where a carry occurs on unsigned data, the arithmetic result is invalid.,(3) Arithmetic Overflow:,An arithmetic operation sets the OF when a carry into the sign bit does not carry out, or a carry out occurs w
37、ith no carry in. If an overflow occurs on signed data, the arithmetic result is invalid.,(4) Both OF and CF Are Set:,An arithmetic operation may set both OF and CF.,Overflow (P219),Overflow occurs when the number of carries into the sign position is different from the number of carries out of the si
38、gn position.,CF vs. OF,C bit represents overflow for unsigned data; There is carry, C=1; otherwise C=0. For unsigned data left bit is data , nots sign, so the carry from this bit is a real carry of the result, but It explains the overflow of the result in limited bits range.O represents overflow for
39、 signed data;,Processing Unsigned and Signed Binary Data,For unsigned data, all bits are intended to be data bits. For signed data, the leftmost bit is a sign bit.Note: the ADD and SUB instructions do not distinguish between unsigned and signed data. CPU不知道操作数是有符号还是无符号,只是根据操作结果设置各种状态标志,由程序员自行选择使用或者忽
40、略各个标志。,Test,Each of these problems gives “before” conditions and an instruction. Fill in the blanks to show the “after” state.,4.3,Multiplication 乘法指令,The basic multiplication operations are byte time byte, word times word, double-word times double-word(80386 and later processors). 字节量相乘 或 字量相乘 或 双字
41、量相乘Both affect the CF and OF. AF, PF, SF, and ZF are undefined.,MUL Unsigned Multiply (无符号数乘法指令),Multiplies an unsigned multiplicand (被乘数) by an unsigned multiplier (乘数).MUL treats a leftmost 1-bit as a data bit, not a negative sign.,MUL Unsigned Multiply (无符号数乘法指令),Operation: assumes the multiplica
42、nd is in AL, AX or EAX, and takes its size from that of the multiplier. The generated product is double-multiplicator.,MUL reg8/mem8 ;无符号字节乘法:AXALr8/m8 MUL reg16/mem16 ;无符号字乘法:DX.AXAXr16/m16,Table 13-7 byte mutiplication,Table 13-8 word mutiplication,Table 13-9 Double-word mutiplication,IMUL Signed
43、(Integer) Multiply (有符号数乘法指令),Multiplies an signed multiplicand by an signed multiplier.IMUL treats the leftmost bit as the sign.,IMUL Signed (Integer) Multiply,Operation: assumes the multiplicand is in AL, AX or EAX, and takes its size from that of the multiplier. The product is double-multiplicato
44、r.,IMUL r8/m8 ;有符号字节乘:AXALr8/m8 IMUL r16/m16 ;有符号字乘:DX.AXAXr16/m16,multiply,mov al,0b4h mov bl,11h mul bl mov al,0b4h mov bl,11h imul bl,Examples,Show the result in the destination operand.,multiply,mov al,0b4h ;al=b4h=180 mov bl,11h ;bl=11h=17 mul bl ;ax=Obf4h=3060 ;OF=CF=1,AX高8位不为0 mov al,0b4h ;al
45、=b4h=76 mov bl,11h ;bl=11h=17 imul bl ;ax=faf4h=1292 ;OF=CF=1,AX高8位含有效数字,Answers,4.4,Division Instructions,The basic divide operations are byte into word, word into doubleword.,Division Instructions,Byte into Word: to divide byte into word, the dividend (被除数 ) is in AX and the divisor (除数) is a byte
46、 in memory or another register. The operation stores the remainder (余数) in AH and quotient (商) in AL.源操作数是字节量: 被除数是字长度,存放在AX中 商存放在AL中,余数存放在AH中。,Division Instructions,Word into DoubleWord: to divide word into doubleword, the dividend is in DX: AX and the divisor is a word in memory or another registe
47、r. The operation stores the remainder in DX and quotient in AX.源操作数是字量: 被除数是双字长度,低16位存放在AX中,高16位存放在DX中 商存放在AX中,余数存放在DX中。,DIV (无符号数除法指令),DIV handles unsigned data. All the flags are undefined.,IDIV (有符号数除法指令),IDIV handles signed data. All the flags are undefined.,Division Instructions,DIV and IDIV op
48、erations can easily cause an overflow; when it does, an interrupt occurs. 1) the divisor is zero 2) the quotient is too big.标志位无定义,但可能产生结果溢出,引发0型中断: )除数() )商太大,超过了限定的范围,注意,division,mov ax,0400h mov bl,0b4h div bl mov ax,0400h mov bl,0b4h idiv bl,Examples,Show the result in the destination operand.,division,mov ax,0400h ;ax=400h=1024 mov bl,0b4h ;bl=b4h=180 div bl ;商al05h5;余数ah7ch124 mov ax,0400h ;ax=400h=1024 mov bl,0b4h ;bl=b4h=76 idiv bl ;商alf3h13;余数ah24h36,