chapter7


Integer Arithmetic

  1. 1. 1. Shift and Rotate instructions – how do they work? What flags can they set? Look at code and be

able to tell what happens after one of these instructions runs.

a. Example: mov al,01101011b

shr al,2

  1. 2. 2. The MUL/IMUL instruction/The DIV/IDIV instruction

SHIFT INSTRUCTION

UNSIGNED integer

SHL

destination,count

images/9-1.png

SHL reg,imm8

SHL mem,imm8

SHL reg,CL

SHL mem,CL

SHR

images/9-2.png

SIGNED INTEGER

SAL = SHL

SAR != SHR

to save the signed extension

images/9-3.png

ROL instruction

uses the Carry flag as an intermediate point for shifted bits.

UNSINGED INTEGER

ROL

images/9-4.png

example:

images/9-5.png

ROR

images/9-6.png

images/9-7.png

SINGED INTEGER

RCL

images/9-8.png

RCR

images/9-9.png

images/9-10.png

SHLD/SHRD instruction

images/9-11.png

images/9-12.png

Multiplication and Division Instructions

UNSINGED INTEGER

MUL

MUL sets the Carry and Overflow flags if the upper half of the product is not equal to zero.

images/9-13.png images/9-14.png images/9-15.png

EXAMPLE 2

images/9-16.png

IMUL (SINGED VARIATION)

images/9-17.png

images/9-18.pngimages/9-19.png

Unsigned Integer Division (DIV)

images/9-20.png

images/9-21.png

8 bit division

images/9-22.png

16 bit division

images/9-23.png

32 bit division

images/9-24.png images/9-25.png

Signed Integer Division (IDIV)

beware of divide overflow when the result cannot hold value bigger than it range.

The dividend must be sign-extended before the division takes place

images/9-26.png

images/9-27.pngimages/9-28.png

example 2:

images/9-29.png

images/9-30.png

Sign Extension Instructions (CBW, CWD, CDQ)

images/9-31.pngimages/9-32.pngimages/9-33.png