360 likes | 511 Views
Instruction System - Arithmetic Instruction. 计算机学院 李征 Tel : 13882153765 Email : lizheng@cs.scu.edu.cn OICQ: 1340915. Arithmetic Instruction. (1) ADD (2) ADC (3) INC (4) SUB (5) SBB (6) DEC (7) NEG (8) CMP. Arithmetic Instruction.
E N D
Instruction System -Arithmetic Instruction 计算机学院 李征 Tel:13882153765 Email:lizheng@cs.scu.edu.cn OICQ: 1340915
Arithmetic Instruction • (1) ADD • (2) ADC • (3) INC • (4) SUB • (5) SBB • (6) DEC • (7) NEG • (8) CMP
Arithmetic Instruction • Arithmetic instruction represent operation status in flags. • All arithmetic instructions affect status flags.
(1) ADD (Addition) • ADD DEST, SRC • DEST <=(SRC)+(DEST) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF
(1) ADD (Addition) • Example: • ADD BX,SI • ADD DA_WORD,0F8CH • ADD DL,TAB[BX]
ALU + 目的地址 源地址
(1) ADD (Addition) • Example: • MOV AH,45H • MOV AL,0E3H • ADD AH,AL • 45H = 01000101B • 0E3H =11100011B • Unsigned code: 69+227 (Decimal) • Signed code: 69+(-29)(Decimal)
(1) ADD (Addition) • 01000101 • + 11100011 • 1 00101000 • Unsigned code:Carry, CF=1 • BCD code: No carry, AF=0 • Signed code:positive+negative OF=0 • Result is positive, SF=0 • Non-zero result,ZF=0 • Bit number of ‘1’ is even,PF=1
(2) ADC (Addition with carry) • ADC DEST, SRC • DEST <=(SRC)+(DEST)+(CF) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF
(2) ADC (Addition with carry) • Example: addition of two 32-bit codes, one code is stored in AX and DX. • ADD AX,1A23H • ADC DX,76FH
ALU + 目的地址 源地址
(2) ADC (Addition with carry) • Example: • MOV AL,93H • MOV AH,02H • MOV BL,88H • MOV BH,0EEH • ADD AL,BL • ADC AH,BH
(2) ADC (Addition with carry) • Explain for unsigned code: • (AX)= 0293H 659(Decimal) • (BX)=0EE88H 61064(Decimal) • ADC(高) ADD(低) • 00000010 10010011 • + 11101110 10001000 • 0 11110001 1 00011011 • CF CF • Result:F11BH 61723(Decimal) This CF represents if there is overflow. This CF joins the addition operations
(2) ADC (Addition with carry) • Explain for signed code: • (AX)= 0293H 659(Decimal) • (BX)=0EE88H - 4472(Decimal) • ADC(高) ADD(低) • 00000010 10010011 • + 11101110 10001000 • 0 11110001 1 00011011 • CF CF • OF=0 OF=1 • Result:F11BH -3813(Decimal) This CF is invalid. This CF joins the addition operations. This OF represent if there is overflow. This OF is invalid.
(3) INC (Increase) • INC DEST • DEST <=(DEST)+1 • Corresponding status: • OF、SF、ZF、AF、PF • Why not CF?
ALU +1 目的地址 1
(3) INC (Increase) • Example: addition of data with 5 word length • mov bx, 0 • mov cx, 5 • clc • lop1: mov ax, tab1[bx] • adc ax, tab2[bx] ; generate CF • mov res[bx], ax • inc bx ; not affect CF • inc bx • loop lop1 ; not affect flags
(3) INC (Increase) • Example: • MOV AL,0FFH • INC AL • CF = ? Maintain its status before ‘INC’, may be 1 or 0
(4) SUB (Subtraction) • SUB DEST,SRC • DEST <=(DEST)-(SRC) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF
ALU - 目的地址 源地址
(4) SUB (Subtraction) • Note: • Do not use signed code operation for CF judgment. • In CPU, CF generation is not correspond to the subtraction operation itself.
(5) SBB (Subtraction with borrow) • SBB DEST,SRC • DEST<=(DEST)-(SRC)-(CF) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF
ADC and SBB • With ADC and SBB, long code operation can be realized in CPU. • Basically, they are both based on CF. • Operation order: lower part first, then higher part
(6) DEC (Decrease) • DEC DEST • DEST <=(DEST)-1 • Corresponding status flag: • OF、SF、ZF、AF、PF • not affect CF
(7) NEG (Negative) • NEG DEST • DEST <= -(DEST) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF
NEG ALU 0 目的地址
(7) NEG (Negative) • Generally, the operation data is explained as signed code. • If operation code is unsigned, then there must be overflow (except 0). • CF = 1 is always the case, except operation code is 0.
(7) NEG (Negative) • Overflow in signed codes: • -128 in byte, -32768 in word, etc.
(7) NEG (Negative) • NEG is limited by code length. • Example: Negative the 32-bit code in DAW • NEG DAW • MOV AX,0 • SBB AX,DAW+2 • MOV DAW+2,AX
(8) CMP (Compare) • CMP DEST,SRC • (DEST)-(SRC) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF • Same as SUB, but not store operation result
ALU - 目的地址 源地址
(8) CMP (Compare) • Not store operation result, only store the status • Along with program transfer instructions, it can realize some branch structure.
(8) CMP (Compare) • Explain for unsigned compare: • CF=0:(DEST)≥(SRC) • CF=1:(DEST)<(SRC)
(8) CMP (Compare) • Explain for signed compare: • OF=SF:(DEST)≥ (SRC) • 1) OF=SF=0: • OF=0 -> SF=0 is correct • 2) OF=SF=1: • OF=1 -> SF=1 is incorrect, so SF=0 is correct.
(8) CMP (Compare) • OF≠SF:(DEST)<(SRC) • 1) OF=0,SF=1: • OF=0 -> SF=1 is correct • 2) OF=1,SF=0: • OF=1 -> SF=0 is incorrect, so SF=1 is correct.
(8) CMP (Compare) • The explaining of unsigned or signed compare is decided by different program transfer instruction choice. • If there is ZF in judgment, there will be more compare result.