Sahar Mosleh California State University San MarcosPage 1 Review
Sahar Mosleh California State University San MarcosPage 2 The CPU flags Each instruction affects the CPU flags. The zero flag is set when the result of an operation equal zero. The carry flag is set when an instruction generates a result that is too large (or too small) for the destination operand. The sign flag is a copy of the high bit of the destination operand indicating that it is negative if set and positive if clear. The overflow flag is set when an instruction generates an invalid signed result. The parity flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand
Sahar Mosleh California State University San MarcosPage 3 Zero and Sign Flags: The zero flag is set when the destination operand of an arithmetic instruction is assigned a value of zero. example: mov cx,1 sub cx,1; cx = 0, ZF = 1 mov ax, 0FFFFh The sign flag is set when the result of an arithmetic operation is negative
Sahar Mosleh California State University San MarcosPage 4 Example: mov cx,0 sub cx,1; CX = -1, SF=1 add cx,2; CX= 1, SF = 0 Carry Flag: Carry flag is significant only when the CPU performs unsigned arithmetic. The result of an unsigned addition operation is too large (or too small) for the destination operand, the carry flag is set. Example: mov al, 0FFh add al,1; al = 00,CF= 1
Sahar Mosleh California State University San MarcosPage 5 The Overflow flag: The overflow flag is relevant only when performing signed arithmetic. It is set when an arithmetic operation generates a signed result that can not fit in the destination operand. Example: mov al, +127 add al, 1; OF = 1 Similarly, mov al, -128 sub al, 1; OF = 1
Sahar Mosleh California State University San MarcosPage 6 Neg Instruction and flag This can produce an invalid result if the destination operand can not be stored correctly Example, if we move -128 to al and try to negate it, the value can not stored in al. This causes the overflow flag to be set, and an invalid value to be moved to al mov al, -128; al = b neg al; al = b, OF=1 On the other hand 1f +127 is negated, the result is valid and the overflow flag is clear. moval, +127; al = b neg al; al = b, OF = 0
Sahar Mosleh California State University San MarcosPage 7 Boolean and comparison instruction We are going to begin the study of the conditional processing by working at the binary level, using the four basic operations from the boolean algebra AND, OR, XOR, and NOT. OperationDescription ANDBoolean AND operation between a source operand and the destination operand ORBoolean OR operation between a source operand and the destination operand XORBoolean XOR operation between a source operand and the destination operand NOTBoolean NOT operation on a destination operand TESTImplies boolean AND operation between a source and destination operand, setting the CPU flags appropriately BT, BTC, BTR, BTS Copy bit n from the source operand to the carry flag and complement/reset/set the same bit in the destination operand
Sahar Mosleh California State University San MarcosPage 8 The AND instruction performs a boolean (bitwise) AND operation between each pair of matching bits in 2 operands and place the result in the destination operann AND destination, source The following operand combination are permitted AND reg, reg AND reg, mem AND reg, imm AND mem, reg AND mem, imm
Sahar Mosleh California State University San MarcosPage 9 The AND instruction is often used to clear selected bits and preserve others mova1, b AND anda1, b The AND instruction always clears the overflow and carry flag. It modifies the sign, zero, parity flag according to the value of the destination operand
Sahar Mosleh California State University San MarcosPage 10 The OR instruction performs a boolean (bitwise) OR operation between each pair of matching bits in 2 operands and place the result in the destination operand. OR destination, source The following operand combination are permitted. OR reg, reg OR reg, mem OR reg, imm OR mem, reg OR mem, imm
Sahar Mosleh California State University San MarcosPage 11 The OR instruction is often used to set selected bits and preserve others. In the following example, 3Bh is ORed with 0Fh. The lower 4 bits of the result are set and the high 4 bits are unchanged OR
Sahar Mosleh California State University San MarcosPage 12 The XOR instruction performs a boolean (bitwise) XOR operation between each pair of matching bits in 2 operands and place the result in the destination operand. XOR destination, source The following operand combination are permitted. XOR reg, reg XOR reg, mem XOR reg, imm XOR mem, reg XOR mem, imm
Sahar Mosleh California State University San MarcosPage 13 Special Quality for XOR is that it reverses itself when applied twice to the same operand X Y (X + Y) + YYX This reversible property of XOR makes it ideal tool for a simple form of data encryption Flag: The XOR instruction always clears the overflow and carry flags. It modifies the sign, zero, and parity flags according to the value of the destination operand
Sahar Mosleh California State University San MarcosPage 14 parity flag The parity flag indicates whether the lowest byte of the result of a bitwise or arithmetic operation has an even or odd number of 1 bits. The flag is set when the parity is even and it is clear when the parity is odd. One way to check the parity of a number without changing its value is to XOR the number with all Zeros: Mov al, b; 5 bits = odd parity Xor al, 0; parity flag clear (PO) Mov al, b; 4 bits = even parity Xor al, 0; parity flag set (PE)
Sahar Mosleh California State University San MarcosPage 15 NOT instruction The NOT instruction toggles all bits in an operand. The result is called 1’s complement. The following operand types are permitted NOT reg NOT mem For example, the 1’s complement of F0h is 0Fh Mov al, b NOT al; AL = b Flag: No flags are affected by NOT instruction
Sahar Mosleh California State University San MarcosPage 16 TEST instruction The TEST instruction performs an implied AND operation between each pair of matching bits in 2 operands and set the flag accordingly. The difference between TEST and AND operation is that TEST does not modify the destination operand. The TEST instruction permits the same operand combinations as the AND instructions. TEST particularly is valuable for finding out if individual bits in an operand is set. Flags : The TEST instruction always clears the overflow and carry flag. It modifies the sign, zero, and parity flag in the same way as the AND instruction
Sahar Mosleh California State University San MarcosPage 17 Example Testing multiple bits The test instruction can check several bits at once. Suppose we want to know if either bit 0 or bit 3 is set in AL register We can use the following instruction to find this out TEST al, b; test bits 0 and 3 From the following example, data set we can infer that the zero flag is set only when all tested bits are clear input value test value result value input value test value result value
Sahar Mosleh California State University San MarcosPage 18 CMP Instruction The CMP instruction performs an implied subtraction of a source operand from a destination operand. Neither operand is modified: CMPdestination, source CMP uses the same operand combinations as the AND instruction. Flags: the CMP instruction changes the overflow, sign, zero, carry, auxiliary carry, and parity flags according to the destination operand would have had if the Sub instruction were used. For example, as shown, below two operands are compared, the zero and carry flags indicate the relation between operands: CMP ResultsZFCF destination < source01 destination > source00 destination = source10
Sahar Mosleh California State University San MarcosPage 19 Examples: Lets look at three code fragments that show how the flags are affected by the CMP instruction. When we put 5 in AX and compare it to 10, the carry flag is set because subtracting 10 from 5 requires a borrow: Movax,5 Cmpax,10; CF = 1 Comparing 1000 to 1000 sets the zero flag because subtracting the source from the destination produces zero: Movax, 1000 Movcx, 1000 Cmpcx,ax;ZF = 1 Comparing 105 to 0 clears both the zero and carry flags because 105 is greater than 0: Movsi,105 Cmpsi,0ZF = 0 and CF = 0
Sahar Mosleh California State University San MarcosPage 20 Conditional Jumps Two steps involve in any conditional jumps. First, an operation such as CMP, AND or SUB modifies the CPU flags. Second, a conditional jump instruction tests the flags and causes a branch to a new address. Example 1: The CMP instruction campers AL to Zero. The JZ ( jumps if Zero) instruction jumps to label L1 if Zero flags was set by the CMP instruction: Cmpa1,0 JzL1;Jump if ZF=1 : L1
Sahar Mosleh California State University San MarcosPage 21 Each conditional jump instruction checks one or more flags, returning a result of true or false. If the result is true, the jump is taken; otherwise the program skips the jump and conditions to the next instruction. MnemonicDescriptionFlags JZJump if zeroZF = 1 JNZJump if not zreoZF = 0 JCJump if carryCF = 1 JNCJump if not carryCF = 0 JOJump if overflowOF = 1 JNOJump if not overflowOF = 0 JSJump if signedSF = 1 JNSJump if not signedSF = 0 JPJump if parity (even)PF = 1 JNPJump if not parity (odd)PF = 0 Limitations: MASM requires the destination of the jump to be a label within -128 to 127 bytes from the jump instruction
Sahar Mosleh California State University San MarcosPage 22 Equality comparisons Based on equality between operands, or value of (E)cx. CMP destination, Source MnemonicDescription JEJump if destination = source JNEJump if not equal JCXZJump if CX=0 JECXZJump if ECX = 0
Sahar Mosleh California State University San MarcosPage 23 Unsigned Comparisons Jumps based on comparisons of unsigned integers are useful when comparing unsigned values, such as 7FFh and 8000h, where 7FFh is smaller than 8000h latter. CMP destination, Source MnemonicDescription JAJump if above (destination > source) JNBEJump if not below or equal (same as JA) JAEJump if above or equal (destination >= source) JNBJump if not below ( same as JAE) JBJump if below JNAEJump if not above or equal ( same as JB) JBEJump if below or equal ( destination <= source) JNAJump if not above ( same as JBE)
Sahar Mosleh California State University San MarcosPage 24 Signed Comparison It is used when the numbers you are comparing can be interpreted as signed values. CMP destination, Source Example: moval,7Fh; (7Fh or +127) Cmpal,80h; (80h or -128 ) JaIsabove; no: 7Fh not> 80 h JgisGreater; yes: > -128 MnemonicDescription JGJump if Greater (destination > source) JNLEJump if not less than or equal (same as JG) JGEJump if Greater than or equal (destination >= source) JNLJump if not Less than ( same as JGE) JLJump if less ( destination < source ) JNGEJump if not Greater or equal ( same as JL) JLEJump if Less than or equal ( destination <= source) JNGJump if not greater ( same as JLE)
Sahar Mosleh California State University San MarcosPage 25 MLU Operations The MUL instruction multiplies an 8-,16-,32-bits operand by either AL,AX,or EAX. The instruction format is: MULr/m8 MULr/m16 MULr/m32 The result is in EDX and EAX. EDX sets the flag register if it is not zero ( as the result of operation over flow situation is Eax carries the product of operation. Will we go through the detail of multiplication and division instruction later on this course.
Sahar Mosleh California State University San MarcosPage 26 Example: TITLE Multiply (AddSub2.asm) ; This program Multiply two decimal numbers together. We need to put the first operand in eax, the ;second operand in another multi purpose register. The product goes to eax ; Last update: 2/1/02 INCLUDE Irvine32.inc.data val1 dword 10 val2 dword 40.code main PROC moveax,val1; eax=10 movebx,val2; ebx=40 mulebx; eax=ebx*eax callwritedec exit main ENDP END main
Sahar Mosleh California State University San MarcosPage 27 DIV Instruction The DIV (unsigned divide) instruction performs 8 bit, 16 bit, and 32 bit division on unsigned integers. Divr/m8 Divr/m16 Divr/m32 The following illustration shows EDX:EAX as the default dividend when a 32- bit divisor is used. EDX EAX = eaxquotient r/m32 edxremainder DividendDivisorQuotientReminder AXr/m8ALAH DX:AXr/m16AXDX EDX:EAXr/m32EAXEDX
Sahar Mosleh California State University San MarcosPage 28 Example 1: The following instruction perform 8-bit unsigned division (83h/2), producing a quotient of 41h and a remainder of 1. mov edx,0 mov ax, 0083h mov bl, 2 div bl Example 2: The following instruction perform 16-bit unsigned division (8003h/100h), producing a quotient of 80h and a remainder of 3. DX contains the high pat of the dividend, so it must be cleared before the div instruction executes. mov dx, 0 mov ax, 8003h mov cx, 100h div cx
Sahar Mosleh California State University San MarcosPage 29 Signed Integer Division CBW, CWD, CDQ Instructions Before discussing signed integer division, we need to look at three instructions that perform integer signed extension. The CBW instruction extends the sign bit of AL into the ah register. This preserves the number’s sign.data ByteVal sbyte -101 ; 9Bh.code mov al, ByteVal; AL = 9Bh cbw; AX = FF9Bh In order words, 9Bh and FF9BH both equal -65. The only difference between the two is their storage size
Sahar Mosleh California State University San MarcosPage 30 The CWD (convert word to doubleword) instruction extends the sign bit of AX into the DX register.data WordVal sword -101; FF9Bh.code mov ax, WordVal; AX = FF9Bh cwd; DX:AX = FFFFFF9Bh The CDQ (convert doubleword to quadword) instruction extends the sign bit of EAX into the EDX register.data DwordVal sword -101; FFFFF9Bh.code mov eax, DwordVal cdq; EDX:EAX = FFFFFFFFFFFFF9Bh
Sahar Mosleh California State University San MarcosPage 31 The IDIV instruction The IDVI (signed divide) instruction) performs signed integer division, using the same operands as the DIV instruction. When doing 8-bit division, you must sign-extend the dividend into AH before IDIV executes, (The CBW instruction can be used). Example: We divide -48 by 5. After IDIV executes, the quotient in AL is -9 and the remainder in AH is -3..data ByteVal sbyte -48.code mov al, ByteVal; dividend cbw; AL into AH mov bl, 5; Divisor idiv bl; AL = -9, AH = -3
Sahar Mosleh California State University San MarcosPage 32 Logical Shift vs Arithmetic Shift There are two basic ways to shift the bits in a number. The first called a logical shift, fills the newly created bit position with Zero. In the following diagram, a byte is logically shifted one position to the right. Note that bit 7 is assigned 0 For example, if we do a single logical right shift on the binary value , it becomes CF
Sahar Mosleh California State University San MarcosPage 33 The other type of shift is called an arithmetic shift. The newly created bit position is filled with a copy of the original numbers sign bit. For example, the binary value , has a 1 in the sign bit. When shifted, arithmetically 1 bit to the right, it becomes CF
Sahar Mosleh California State University San MarcosPage 34 SHL Instruction The SHL instruction performs a logical left shift on the destination operand, filling the lowest bit with 0. The highest bit is moved to the carry flag, and the bit that was in the carry flag is lost. The first operand is the destination and the second is shift count. SHL destination, count The following lists the types of operands permitted by this instruction SHL reg, imm8 SHL mem, imm8 CF
Sahar Mosleh California State University San MarcosPage 35 Example In the following instructions, BL is shifted once to the left. The highest bit is copied into the carry flag and the lowest bit position is cleared. mov bl, 8FH; BL = b Shl b1, 1; BL = b, CF = 1
Sahar Mosleh California State University San MarcosPage 36 SHR Instruction The SHR instruction performs a logical right shift on the destination operand, filling the highest bit with 0. The lowest bit is moved to the carry flag, and the bit that was in the carry flag is lost. SHR uses the same instruction format as SHL. In the following example, the 0 from the lowest bit in AL is copied into the carry flag, and the highest bit in AL is cleared. mov al, 0D0h; AL = b shr al, 1; AL = b CF
Sahar Mosleh California State University San MarcosPage 37 SAL and SAR instructions SAL (Shift Arithmetic Left) is identical to SHL instruction. The SAR (Shift Arithmetic Right) instruction performs a right arithmetic shift on its destination operand. The syntax and operands for SHR and SAR are identical to those for SHL and SHR. The shift may be repeated, based on the counter in the second operand. SAR destination, count CF
Sahar Mosleh California State University San MarcosPage 38 Example The following example, shows how SAR duplicates the sign bit al is negative before and after it is shifted to the right. mov al, 0F0h; al = b (-16) sar al, 1; al = b (-8) CF = 0 Signed Division: You can divide a signed operand by a power of 2 using the SAR instruction. In the following example, -128 is divided by 2^3. The quotient is -16 mov dl, -128; dl = b sar dl, 3; dl = b
Sahar Mosleh California State University San MarcosPage 39 ROL Instruction The ROL (rotate left instruction) shifts each bit to the left. Also, the highest bit is copied both into the carry flag and into the lowest bit. The instruction format is the same as for the SHL instruction Bit rotation differs from bit shifting in that the former does not lose any bits. A bit that is rotated of one end of a number appears again at the other end. CF
Sahar Mosleh California State University San MarcosPage 40 In the following example, the high bit is copied into both the carry flag and bit position zero mov al, 40h; AL = b rol al, 1; AL = b ; CF = 0 rol al, 1; AL = b; CF = 1 rol al, 1; AL = b; CF = 0 You can use ROL to exchange the upper (4-7 bit) and lower (bits 0-3) halves of the byte. mov al, 26h rol al, 4; AL = 62h
Sahar Mosleh California State University San MarcosPage 41 ROR Instruction The ROR instruction shifts each bit to the right. Also the lowest bit is copied into the flag and into the highest bit at the same time The instruction format is the same as for SHL In the following example, the lowest bit is copied into the carry flag and into the highest bit of the result mov al, 01h; AL = b ror al, 1; AL = b, CF = 1 ror al, 1; AL = b, CF = 0 CF
Sahar Mosleh California State University San MarcosPage 42 RCL and RCR instructions The RCL (Rotate Carry left) instruction shifts each bit to the left copies the carry flag to least significant bit (LSB), and copies the most significant bit (MSB) into the carry flag. If you think of the carry flag as just an extra bit added to the high end of the number, then RCL becomes a simple rotate left operation. CF
Sahar Mosleh California State University San MarcosPage 43 RCR Instruction The RCR (Rotate Carry Right) instruction shifts each bit to the right, copies the right flag into the most significant bit, and copies the least significant bit into the carry flag. As in the case of RCL, it helps to visualize the integer in this figure as a 9-bit value, with the carry flag to the right of the least significant bit CF
Sahar Mosleh California State University San MarcosPage 44 SHLD/SHRD instructions The SHLD (Shift Left Double) instructions shifts a destination operand a given number of bits to the left. The bit positions opened up by the shift are filled by the most significant bits of the source operand. The source operand is not affected, but the sign, zero, auxiliary, parity, and carry flags are affected. SHLD destination, source, count The SHRD (Shift Right Double) instructions shifts a destination operand a given number of bits to the right. The bit positions opened up by the shift are filled by the least significant bits of the source operand. SHRD destination, source, count
Sahar Mosleh California State University San MarcosPage 45 Example 1: The following statements shift wval to the left 4 bits and insert the high 4 bits of ax into the low 4-bit position of wval.data wval word 9BA6h.code mov ax, 0AC36h shld wval, ax, 4; wval = BA6Ah The data movement is shown in the following figure AC36 wvalAX
Sahar Mosleh California State University San MarcosPage 46 Example 2: In the following example, ax is shifted to the right 4-bits and the low 4-bits of dx are shifted into the high 4-bit positions of ax mov ax 234Bh mov dx, 7654h shrd ax, dx, 4; AX = 4234h 7654 B DXAX
Sahar Mosleh California State University San MarcosPage 47 ADC instruction The ADC (add with carry flag) instruction adds both a source operand and the content of the carry flag to the destination operand. The instruction formats are the same as mov instruction. ADC reg, reg ADC mem, reg ADC reg, mem ADC mem, imm ADC reg, imm Example: the following instruction add two 8-bits integers (0FFh+0FFh), producing a 16-bit sum in DL.. AL, which is 01FEh mov dl, 0 mov al, 0FFh add al, 0FFh; AL = FE adc dl, 0; DL = 01
Sahar Mosleh California State University San MarcosPage 48 SBB Instruction The SBB (Subtract with borrow) instruction subtracts both a source operand and the value of the carry flag from the destination operand. The possible instruction formats are the same as for ADC instruction. The following example code performs a 64-bit subtraction. It sets edx:eax to h and subtracts 1 from this value The lower 32 bits are subtracted first, setting the carry flag and the upper 32-bit are subtracted including the carry flag mov edx, 1; upper half mov eax, 0; lower half sub eax, 1; subtract 1 sbb edx, 0; subtract upper half The 64-bit difference in EDX:EAX is FFFFFFFFh