1 / 36

Instruction System - Arithmetic Instruction

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.

lemuel
Download Presentation

Instruction System - Arithmetic Instruction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Instruction System -Arithmetic Instruction 计算机学院 李征 Tel:13882153765 Email:lizheng@cs.scu.edu.cn OICQ: 1340915

  2. Arithmetic Instruction • (1) ADD • (2) ADC • (3) INC • (4) SUB • (5) SBB • (6) DEC • (7) NEG • (8) CMP

  3. Arithmetic Instruction • Arithmetic instruction represent operation status in flags. • All arithmetic instructions affect status flags.

  4. (1) ADD (Addition) • ADD DEST, SRC • DEST <=(SRC)+(DEST) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF

  5. (1) ADD (Addition) • Example: • ADD BX,SI • ADD DA_WORD,0F8CH • ADD DL,TAB[BX]

  6. ALU + 目的地址 源地址

  7. (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)

  8. (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

  9. (2) ADC (Addition with carry) • ADC DEST, SRC • DEST <=(SRC)+(DEST)+(CF) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF

  10. (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

  11. ALU + 目的地址 源地址

  12. (2) ADC (Addition with carry) • Example: • MOV AL,93H • MOV AH,02H • MOV BL,88H • MOV BH,0EEH • ADD AL,BL • ADC AH,BH

  13. (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

  14. (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.

  15. (3) INC (Increase) • INC DEST • DEST <=(DEST)+1 • Corresponding status: • OF、SF、ZF、AF、PF • Why not CF?

  16. ALU +1 目的地址 1

  17. (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

  18. (3) INC (Increase) • Example: • MOV AL,0FFH • INC AL • CF = ? Maintain its status before ‘INC’, may be 1 or 0

  19. (4) SUB (Subtraction) • SUB DEST,SRC • DEST <=(DEST)-(SRC) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF

  20. ALU - 目的地址 源地址

  21. (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.

  22. (5) SBB (Subtraction with borrow) • SBB DEST,SRC • DEST<=(DEST)-(SRC)-(CF) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF

  23. 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

  24. (6) DEC (Decrease) • DEC DEST • DEST <=(DEST)-1 • Corresponding status flag: • OF、SF、ZF、AF、PF • not affect CF

  25. (7) NEG (Negative) • NEG DEST • DEST <= -(DEST) • Corresponding status flag: • OF、SF、ZF、AF、PF、CF

  26. NEG ALU 0 目的地址

  27. (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.

  28. (7) NEG (Negative) • Overflow in signed codes: • -128 in byte, -32768 in word, etc.

  29. (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

  30. (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

  31. ALU - 目的地址 源地址

  32. (8) CMP (Compare) • Not store operation result, only store the status • Along with program transfer instructions, it can realize some branch structure.

  33. (8) CMP (Compare) • Explain for unsigned compare: • CF=0:(DEST)≥(SRC) • CF=1:(DEST)<(SRC)

  34. (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.

  35. (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.

  36. (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.

More Related