Sunteți pe pagina 1din 30

Chapter 12

Selected Pentium Instructions

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

Chapter 12

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

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

Chapter 12

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.

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.

Chapter 12

126 If the overflow flag is set, src is greater than dest operand when these two operands are treated
as signed numbers.

Chapter 12

127 No. The carry flag is set when src is greater than dest whereas ZF is set when they are equal.

Chapter 12

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

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.

11

Chapter 12
1210 The following code implements a count-down loop using the zero flag:
mov
repeat1:
<<loop
dec
jnz

CX,count
body>>
CX
repeat1

The above code is better than the count-up loop implementation shown below:
mov
repeat1:
<<loop
inc
cmp
jne

CX,0
body>>
CX
CX,count
repeat1

12

Chapter 12

1211
AL

CF

ZF

SF

OF

PF

mov
add

AL,127
AL,-128

FF

mov
sub

AL,127
AL,-128

FF

mov
add

AL,-1
AL,1

mov
inc

AL,127
AL

80

mov
neg

AL,127
AL

81

mov
neg

AL,0
AL

13

Chapter 12
1212
Before execution

After execution

Instruction

AL

BL

AL

ZF

SF

PF

and
or
xor
test
and
or
xor
test

79H
79H
79H
79H
36H
36H
36H
36H

86H
86H
86H
86H
24H
24H
24H
24H

0
FFH
FFH
79H
24H
36H
12H
36H

1
0
0
1
0
0
0
0

0
1
1
0
0
0
0
0

1
1
1
1
1
1
1
1

AL,BL
AL,BL
AL,BL
AL,BL
AL,BL
AL,BL
AL,BL
AL,BL

14

Chapter 12

1213

Instruction
shl
rol
shr
ror
sal
sar
rcl
rcr

AL,1
AL,1
AL,1
AL,1
AL,1
AL,1
AL,1
AL,1

Before execution
AL
CF
1
1
50
50
20
20
20
20

?
?
?
?
?
?
1
1

After execution
AL
CF
FFH
FFH
19H
19H
D8H
F6H
D9H
F6H

1
1
0
0
1
0
1
0

15

Chapter 12
1214

Instruction
shl
sal
rcl
rcr
ror
rol

AL,CL
AL,CL
AL,CL
AL,CL
AL,CL
AL,CL

Before execution
AL
CF
76H
76H
76H
76H
76H
76H

?
?
1
1
?
?

After execution
AL
CF
B0H
B0H
B5H
AEH
CEH
B3H

1
1
1
1
1
1

16

Chapter 12

1215 The reason is that in unsigned numbers, there is no sign bit; even the most significant bit represents magnitude. In the signed numbers, the most significant bit represents sign, not magnitude.

17

Chapter 12

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 by
255D is the maximum number we can expect from the multiplication of two 8-bit numbers. Since
this number (65025D) is less than 2 16 1, there will not be any overflow.
A more general proof that looks at x-bit inputs:
Multiplying two x-bit numbers results in (2 x 1)2

= (22x + 1

2x+1 ), which is less than 22x

1.

18

Chapter 12

1217 jg condition:
The logical expression ((SF xor OF) or ZF) = 0 is true when ZF = 0 and SF = OF (because of the
xor operation). This is the condition given in Table 12.6.
jge condition:
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.
jl condition:
The logical expression (SF xor OF) = 0 is true only SF 6= OF (because of the xor operation). This
is the condition given in Table 12.6.
jle condition:
The logical expression ((SF xor OF) or ZF) = 1 is true when either ZF = 1 or SF 6= OF (because
of the xor operation). This is the condition given in Table 12.6.

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, for
example, 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.

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 example, testing for the sentinel character). In addition, in sentinel-terminated variable strings, the
sentinel character must be avoided in the string.

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 is
that 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.

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 advantage 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).

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 stos instruction, is often used when processing is
required while copying a string.

24

Chapter 12

1223 This is because these string instructions do not compare values like the cmps instruction.

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 in
contrast 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 jcxz check is needed.

26

Chapter 12

1225 Usually it does not matter whether the string processing direction is forward or backward. However, for sentinel character-terminated strings, the forward direction is preferred as it improves
efficiency (copy the character until the sentinel character is read).

27

Chapter 12

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., in
the backward direction) as in the following example:
Initial string !

After one shift !

After two shifts !

After three shifts!

Final string !

If we proceed in the forward direction, only the first character is copied through the string, as
shown below:
Initial string !

After one shift !

After two shifts !

After three shifts!

Final string !

28

Chapter 12

1227 The code for lds


mov
mov
mov

SI,string can be implemented as shown below:

SI,[string+2]
DS,SI
SI,[string]

29

Chapter 12

1228 From the Pentium data book, we get the following timing information: The code above takes 4
cycles as indicated below:
mov
mov
mov

SI,[string+2]
DS,SI
SI,[string]

; 1 cycle
; 2 cycles
; 1 cycle

The lds instruction also takes 4 clock cycles.

30

Chapter 12

1229 In direct procedure calls, the offset of the target procedure is provided directly. In indirect procedure calls, this offset is given with one level of indirection as in the indirect jump. That is, the
call instruction itself will contain either a memory address (through a label), or a 16-bit generalpurpose 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 call instruction 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 at target_proc_ptr contains the offset of the target procedure.

S-ar putea să vă placă și