arch book solution ch12 sep

Upload: chantaiah

Post on 01-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Arch Book Solution Ch12 Sep

    1/30

    Chapter 12

    Selected Pentium Instructions

    1

  • 8/9/2019 Arch Book Solution Ch12 Sep

    2/30

    2 Chapter 12

    121 Carry flag indicates out-of-range error for unsigned operations.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    3/30

    Chapter 12 3

    122 Overflow flag indicates out-of-range error for signed operations.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    4/30

    4 Chapter 12

    123 Sign flag is a copy of the most significant bit of the result. We can detect this by several methods.

    Couple of them are given below:

    Use shift left (SHL) instruction to move the sign bit into the carry flag.

    Use and 80H and test the zero flag

  • 8/9/2019 Arch Book Solution Ch12 Sep

    5/30

    Chapter 12 5

    124 Parity flag is set when the least significant byte of the result contains even number of 1 bits (i.e.,

    even parity). This flag is useful in parity generation.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    6/30

    6 Chapter 12

    125 When executing

    sub dest,src

    if CF is set, it indicated that src is greater than dest operand when these two operand are treated

    as unsigned numbers.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    7/30

    Chapter 12 7

    126 If the overflow flag is set,srcis greater thandestoperand when these two operands are treated

    as signed numbers.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    8/30

    8 Chapter 12

    127 No. The carry flag is set whensrcis greater thandestwhereas ZF is set when they are equal.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    9/30

    Chapter 12 9

    128 No. The overflow flag is set whensrc is greater than dest whereas ZF is set when they are

    equal.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    10/30

    10 Chapter 12

    129 When the result contains even number of 1s (except the zero result), the parity flag is set but not

    the zero flag.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    11/30

    Chapter 12 11

    1210 The following code implements a count-down loop using the zero flag:

    mov CX,count

    repeat1:

    dec CX

    jnz repeat1

    The above code is better than the count-up loop implementation shown below:

    mov CX,0

    repeat1:

    inc CX

    cmp CX,count

    jne repeat1

  • 8/9/2019 Arch Book Solution Ch12 Sep

    12/30

    12 Chapter 12

    1211

    AL CF ZF SF OF PF

    mov AL,127

    add AL,-128 FF 0 0 1 0 1

    mov AL,127

    sub AL,-128 FF 1 0 1 1 1

    mov AL,-1

    add AL,1 0 1 1 0 0 1

    mov AL,127

    inc AL 80 1 0 1 1 0

    mov AL,127neg AL 81 1 0 1 0 1

    mov AL,0

    neg AL 0 0 1 0 0 1

  • 8/9/2019 Arch Book Solution Ch12 Sep

    13/30

    Chapter 12 13

    1212

    Before execution After execution

    Instruction AL BL AL ZF SF PF

    and AL,BL 79H 86H 0 1 0 1

    or AL,BL 79H 86H FFH 0 1 1

    xor AL,BL 79H 86H FFH 0 1 1

    test AL,BL 79H 86H 79H 1 0 1

    and AL,BL 36H 24H 24H 0 0 1

    or AL,BL 36H 24H 36H 0 0 1

    xor AL,BL 36H 24H 12H 0 0 1

    test AL,BL 36H 24H 36H 0 0 1

  • 8/9/2019 Arch Book Solution Ch12 Sep

    14/30

    14 Chapter 12

    1213

    Before execution After execution

    Instruction AL CF AL CF

    shl AL,1

    1 ? FFH 1

    rol AL,1 1 ? FFH 1

    shr AL,1 50 ? 19H 0

    ror AL,1 50 ? 19H 0

    sal AL,1 20 ? D8H 1

    sar AL,1 20 ? F6H 0

    rcl AL,1 20 1 D9H 1

    rcr AL,1 20 1 F6H 0

  • 8/9/2019 Arch Book Solution Ch12 Sep

    15/30

    Chapter 12 15

    1214

    Before execution After execution

    Instruction AL CF AL CF

    shl AL,CL 76H ? B0H 1

    sal AL,CL 76H ? B0H 1

    rcl AL,CL 76H 1 B5H 1

    rcr AL,CL 76H 1 AEH 1

    ror AL,CL 76H ? CEH 1

    rol AL,CL 76H ? B3H 1

  • 8/9/2019 Arch Book Solution Ch12 Sep

    16/30

    16 Chapter 12

    1215 The reason is that in unsigned numbers, there is no sign bit; even the most significant bit repre-

    sents magnitude. In the signed numbers, the most significant bit represents sign, not magnitude.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    17/30

    Chapter 12 17

    1216 We will show that the statement is true for the 8-bit numbers. The other two are similar. The

    maximum (unsigned) number we can represent using four bits is 255D. So multiplying 255D by255D is the maximum number we can expect from the multiplication of two 8-bit numbers. Since

    this number (65025D) is less than

    , there will not be any overflow.

    A more general proof that looks at x-bit inputs:

    Multiplying two

    -bit numbers results in

    , which is less than

    .

  • 8/9/2019 Arch Book Solution Ch12 Sep

    18/30

    18 Chapter 12

    1217 jgcondition:

    The logical expression ((SF xor OF) or ZF) = 0 is true when ZF = 0 and SF = OF (because of thexor operation). This is the condition given in Table 12.6.

    jgecondition:

    The logical expression (SF xor OF) = 1 is true only SF = OF (because of the xor operation). This

    is the condition given in Table 12.6.

    jlcondition:

    The logical expression (SF xor OF) = 0 is true only SF OF (because of the xor operation). This

    is the condition given in Table 12.6.

    jlecondition:

    The logical expression ((SF xor OF) or ZF) = 1 is true when either ZF = 1 or SF

    OF (because

    of the xor operation). This is the condition given in Table 12.6.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    19/30

    Chapter 12 19

    1218 In the fixed-length representation, each string occupies exactly the same number of character

    positions. In such a representation, if a string has fewer characters, it is extended by padding, forexample, with blank characters. On the other hand, if a string has more characters, it is usually

    truncated to fit the storage space available.

    Clearly, if we want to avoid truncation of larger strings, we need to fix the string length carefully

    so that it can accommodate the largest string. In practice, it may be difficult to guess this value. A

    further disadvantage is that memory space is wasted if the majority of strings are shorter than the

    fixed length used.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    20/30

    20 Chapter 12

    1219 The main advantages are:

    We dont have to decide on the string length.

    We dont waste memory space if the actual string is shorter than the fixed length used.

    We dont truncate longer strings if they exceed the fixed length used.

    The main disadvantage is the overhead associated with processing variable-length strings (for ex-

    ample, testing for the sentinel character). In addition, in sentinel-terminated variable strings, the

    sentinel character must be avoided in the string.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    21/30

    Chapter 12 21

    1220 Explicitly storing string length: In this method, the string length attribute is explicitly stored

    along with the string. Thus, it is efficient to find the string length. An additional advantage isthat we can use any character in the string (in contrast to the other method that reserves a special

    character as the sentinel). However, the disadvantage is that, if we modify the contents of the

    string, we have to update the string length value as well.

    Using a sentinel character: In this method, strings are stored with a trailing sentinel character.

    The main advantage is that there is no need to store string length explicitly. The disadvantage is

    that the sentinel character is a special character that cannot appear within a string. Furthermore,

    finding string length involves scanning the string until we find the sentinel character.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    22/30

    22 Chapter 12

    1221 The main advantage of the string instructions is that, as part of execution, they automatically

    update (i.e., increment or decrement) the index registers used by these instructions. Another ad-vantage is that they allow memory-to-memory copying of data. (Note that normal instructions do

    not allow memory-to-memory copying.) In addition, string instructions can accept a repetition

    prefix to repeatedly execute the operation. These features result in an efficient code for block

    movement of data (strings and other types of data).

  • 8/9/2019 Arch Book Solution Ch12 Sep

    23/30

    Chapter 12 23

    1222 The load string (lods) instruction copies the value at DS:SI from the source string to AL, AX,

    or EAX. Use of the rep prefix does not make sense, as it will leave only the last value in AL,AX, or EAX. This instruction, along with the stosinstruction, is often used when processing is

    required while copying a string.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    24/30

    24 Chapter 12

    1223 This is because these string instructions do not compare values like thecmpsinstruction.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    25/30

    Chapter 12 25

    1224 The repeat prefixes first check the CX register to see if it is not 0, only then is the string instruction

    executed. Thus, if CX is 0 to start with, the string instruction is not executed at all. This is incontrast to the loop instruction, which first decrements and then tests if CX is 0. Thus, with

    loop, CX = 0 results in a maximum number of iterations, and usually a jcxzcheck is needed.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    26/30

    26 Chapter 12

    1225 Usually it does not matter whether the string processing direction is forward or backward. How-

    ever, for sentinel character-terminated strings, the forward direction is preferred as it improvesefficiency (copy the character until the sentinel character is read).

  • 8/9/2019 Arch Book Solution Ch12 Sep

    27/30

    Chapter 12 27

    1226 There are situations where one particular direction is mandatory. For example, if we want to shift

    a string right by one position, we have to start with the tail and proceed toward the head (i.e., inthe backward direction) as in the following example:

    Initial string

    a b c 0 ?

    After one shift

    a b c 0 0

    After two shifts

    a b c c 0

    After three shifts

    a b b c 0

    Final string

    a a b c 0

    If we proceed in the forward direction, only the first character is copied through the string, as

    shown below:

    Initial string a b c 0 ?

    After one shift a a c 0 ?

    After two shifts

    a a a 0 ?

    After three shifts

    a a a a ?

    Final string

    a a a a a

  • 8/9/2019 Arch Book Solution Ch12 Sep

    28/30

    28 Chapter 12

    1227 The code forlds SI,string can be implemented as shown below:

    mov SI,[string+2]

    mov DS,SI

    mov SI,[string]

  • 8/9/2019 Arch Book Solution Ch12 Sep

    29/30

    Chapter 12 29

    1228 From the Pentium data book, we get the following timing information: The code above takes 4

    cycles as indicated below:mov SI,[string+2] ; 1 cycle

    mov DS,SI ; 2 cycles

    mov SI,[string] ; 1 cycle

    The ldsinstruction also takes 4 clock cycles.

  • 8/9/2019 Arch Book Solution Ch12 Sep

    30/30

    30 Chapter 12

    1229 In direct procedure calls, the offset of the target procedure is provided directly. In indirect pro-

    cedure calls, this offset is given with one level of indirection as in the indirect jump. That is, thecall instruction itself will contain either a memory address (through a label), or a 16-bit general-

    purpose register. The actual offset of the target procedure is obtained either from the memory or

    register. For example, we could use

    call BX

    if BX contains the offset of the target procedure. When this callinstruction is executed, the BX

    register contents are used to load IP in order to transfer control to the target procedure. Similarly,

    we can use

    call target_proc_ptr

    if the word in memory attarget_proc_ptr contains the offset of the target procedure.