CCoommppuutteerr OOrrggaanniizzaattiioonn && AAsssseemmbbllyy LLaanngguuaaggeess Integer Arithmetic Pu-Jen Cheng Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed. Chapter Overview Shift and Rotate Instructions (cid:132) Shift and Rotate Applications (cid:132) Multiplication and Division Instructions (cid:132) Extended Addition and Subtraction (cid:132) ASCII and Unpacked Decimal Arithmetic (cid:132) Packed Decimal Arithmetic (cid:132) Shift and Rotate Instructions Logical vs Arithmetic Shifts (cid:132) SHL Instruction (cid:132) SHR Instruction (cid:132) SAL and SAR Instructions (cid:132) ROL Instruction (cid:132) ROR Instruction (cid:132) RCL and RCR Instructions (cid:132) SHLD/SHRD Instructions (cid:132) Logical vs Arithmetic Shifts A logical shift fills the newly created bit position (cid:132) with zero: 0 CF • AAn ariitthhmettiic shhiifftt ffiilllls tthhe newlly creattedd bbiitt position with a copy of the number’s sign bit: CF SHL Instruction The SHL (shift left) instruction performs a logical (cid:132) left shift on the destination operand, filling the lowest bit with 0. 0 CF • Operand types for SHL: SHL destination,count SHL reg,imm8 (Same for all shift and SHL mem,imm8 rotate instructions) SHL reg,CL SHL mem,CL Fast Multiplication Shifting left 1 bit multiplies a number by 2 mov dl,5 Before: 0 0 0 0 0 1 0 1 = 5 shl dl,1 After: 0 0 0 0 1 0 1 0 = 10 SShiffting lefft n bits multiplies the operand by 2n For example, 5 * 22 = 20 mov dl,5 shl dl,2 ; DL = 20 SHR Instruction The SHR (shift right) instruction performs a logical (cid:132) right shift on the destination operand. The highest bit position is filled with a zero. 0 CF Shifting right n bits divides the operand by 2n mov dl,80 shr dl,1 ; DL = 40 shr dl,2 ; DL = 10 SAL and SAR Instructions SAL (shift arithmetic left) is identical to SHL. (cid:132) SAR (shift arithmetic right) performs a right (cid:132) arithmetic shift on the destination operand. CF An arithmetic shift preserves the number's sign. mov dl,-80 sar dl,1 ; DL = -40 sar dl,2 ; DL = -10 Your turn . . . Indicate the hexadecimal value of AL after each shift: mov al,6Bh ; 01101011 shr al,1 a. 35h shl al,3 b. A8h mov al,8Ch ; 10001100 ssaarr aall,,11 cc.. CC66hh sar al,3 d. F8h ROL Instruction ROL (rotate) shifts each bit to the left (cid:132) The highest bit is copied into both the Carry flag (cid:132) and into the lowest bit No bits are lost (cid:132) CF mov al,11110000b rol al,1 ; AL = 11100001b mov dl,3Fh rol dl,4 ; DL = F3h
Description: