Micro-Computer Applications: Arithmetic, Logic & Data Movement Instructions Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Instructions Types Arithmetic/Logic Data Movement Program Control (if,..) Loop Control Interrupts
Dr. Amr Talaat ELECT 707 Micro-Computer Applications MOV et.al. MOV is still the main data transfer instruction, b ut there are many variations that perform speci al tasks such as PUSH and POP. We do not often code in hexadecimal machine la nguage, but an understanding tends to help wit h learning the instruction set and the form of ins tructions in the memory.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications The Opcode WD OpcodeModRegR/M DescriptionFeild the direction of data flow either from or to R/M from REG. If D = 0 REG R/M and if D = 1 R/M REG D specifies a word (16-bit mode) or a doubleword (32-bit mode). W = 0 byte and W = 1 word/doubleword W specifies how R/M is used and whether a displacement exists. MOD = 00 memory no displacement MOD = 01 memory 8-bit displacement MOD = 10 memory 16/32-bit displacement MOD = 11 register MOD
Dr. Amr Talaat ELECT 707 Micro-Computer Applications
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Segment Register Moves Although we do not often directly address segm ent registers it is important to understand the li mitations of the segment register MOV instructio n. Immediate data cannot be moved into a se gment register. CS cannot successfully be loaded with a se gment register MOV.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Addition The ADD instruction is used for binary addition. The addition causes the flag bits to change. Addition can be 8-, 16-, and 32-bits. All of the addressing modes presented in Chapte r 2 are used by addition. ;EAX = EAX + EBX ADD EAX,EBX ;EAX = EAX + EBX APOSZC rrrrrr
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Flags Flag Status After Instructions: 1 - instruction sets this flag to 1. 0 - instruction sets this flag to 0. r - flag value depends on result of the instruct ion. ? - flag value is undefined (maybe 1 or 0)
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Increment one The INC instruction adds a one to a register or t he contents of a memory location. ;BX = BX + 1 INC BX;BX = BX + 1 INC BYTE PTR [EBX]
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Add with Carry The ADC instruction adds the carry bit into the s um. Used for wide additions (wider than 32-bits ) and other reasons. ;AX = AX + DX + C ADC AX,DX;AX = AX + DX + C ADX ESI,[EBX]
Dr. Amr Talaat ELECT 707 Micro-Computer Applications
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Subtraction The SUB instruction performs subtraction and th e flags change to reflect condition of the result. As with other arithmetic and logic instructions, s ubtraction exists for 8-, 16-, and 32-bit data. ;AL = AL - 3 SUB AL,3;AL = AL - 3 SUB ECX,ESI
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Decrement one The DEC instruction subtracts one from a registe r or the contents of a memory location. ;EBX = EBX - 1 DEC EBX;EBX = EBX - 1 DEC DWORD PTR [EAX]
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Subtract with Borrow The SBB instruction subtracts with borrow (wher e the borrow is held in the carry flag). ;EAX = EAX – EBX – C SBB EAX,EBX ;EAX = EAX – EBX – C
Dr. Amr Talaat ELECT 707 Micro-Computer Applications
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Compare The CMP instruction is a special form of the SUB instruction. A compare does not result in a diffe rence that is saved, on the flag bits change to re flect the difference. CMP AL,3 ;if AL = 3 the result to zero (flag)
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Multiplication The MUL (unsigned) and IMUL (signed) instructi on exist to perform 8-, 16-, or 32-bit multiplicati on. The result is always a double wide result. The carry and overflow bits indicate conditions a bout the multiplication. A special IMUL exists with 3 operands.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Division The DIV (unsigned) and IDIV (signed) instructio n exist to perform division on 8-, 16-, or 32-bit numbers. Division is always performed o a double wide div idend. The result is always in the form of an integer qu otient and an integer remainder.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications AND The AND instruction performs logical multiplicati on (the AND operation).
Dr. Amr Talaat ELECT 707 Micro-Computer Applications OR The OR instruction generates the logical sum (O R operation).
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Exclusive OR The XOR instruction performs the Exclusive OR operation.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications NEG and NOT The NEG (negate) instruction 2’s complements a number, The NOT instruction 1’s complements a number. NOT EAX NEG DWORD PTR [EBX]
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Shifts There are 4 shift instructions. Two are logical s hifts and two are arithmetic shifts. The logical shifts reposition the bits in a number. The arithmetic shifts multiply or divide signed numbers by powers of two. SHR and SHL are logical and SAR and SAL are ar ithmetic shifts. SHL AL,3 or SHL AL,CL
Dr. Amr Talaat ELECT 707 Micro-Computer Applications
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Rotates Rotates are shifts that re-circulate the bit that m oves out of an end of the register or memory loc ation. Four rotates exist where two just rotate the targ et and two rotate the target through the carry fl ag. ROL AL,3or RCL AL,3
Dr. Amr Talaat ELECT 707 Micro-Computer Applications
Dr. Amr Talaat ELECT 707 Micro-Computer Applications TEST The TEST instruction is a special form of the AN D instruction that produces no result except for a change of the flags. This instruction is often used to test multiple bit s of a number. test the right two bits (if both are z ero the result is zero) TEST AL,3 ;test the right two bits (if both are z ero the result is zero)
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Bit Test Instructions There are four bit test instructions BT (bit test), BTR (bit test and reset), BTS (bit test and set), and BTC (bit test and complement). Each tests the prescribed bit by moving it into c arry. Then the bit is modified (except for BT) ;bit 3 is moved to carry BT AL,3;bit 3 is moved to carry ;bit 3 is moved to carry then set BTS AL,3;bit 3 is moved to carry then set ;bit 3 is moved to carry then reset BTR AL,3;bit 3 is moved to carry then reset ;bit 3 is moved to carry then inverted. BTC AL,3;bit 3 is moved to carry then inverted.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications PUSH and POP PUSH and POP transfer data to and from the stack. The st ack is an area of memory that is reused and grows in size with each PUSH and shrinks in size with each POP. PUSH and POP function with either 16- or 32-bit data. PUSHF (PUSHFD) and POPF (POPFD) save and restore the flags (EFLAGS) PUSHA (PUSHAD) and POPA (POPAD) save and restore all the registers
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Load Segment and Index LDS, LES, LSS, LFG, and LGS allow a segment r egisters and a pointer to both be loaded from m emory. LDS BX,BOB ;loads DS and BX with the offset and segment add ress stored in a 32-bit memory location called B OB.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications LEA The LEA instruction loads the effective ad dress of a memory location into a pointer or index register. At times we do the same operation with a MOV and the keyword OFFSET MOV BX,OFFSET FRED LEA BX,FRED Both instruction accomplish the same task.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications String Data Transfer Instructions String data transfer instructions are: LOD S, STOS, MOVS, INS, and OUTS. These instructions use the direction flag b it to select the way that a pointer is modi fied after the instruction. D = 0 auto-incr ement and D = 1 auto-decrement. Many of these instructions can be prefixe d with a REP (repeat) to repeat the instru ction the number of times stored in the C X register.
Dr. Amr Talaat ELECT 707 Micro-Computer Applications Miscellaneous NOP (does nothing) XCHG (swaps contents) CLC, STC, CMC (modify Carry)
Dr. Amr Talaat ELECT 707 Micro-Computer Applications References: Based on slides from B. Brey, The Intel Microprocessor: Archit ecture, Programming, and Interfacing, 8 th Edition, 2009 & other s