Interrupts & Input/output

Download Report

Transcript Interrupts & Input/output

Selected Pentium Instructions
Chapter 12
S. Dandamudi
Outline
• Status flags






• Conditional execution
 Indirect jumps
Zero flag
Carry flag
Overflow flag
Sign flag
Auxiliary flag
Parity flag
• Conditional jumps
 Single flags
 Unsigned comparisons
 Signed comparisons
• Arithmetic instructions
 Multiplication instructions
 Division instructions
• Implementing high-level
language decision
structures
• Application examples
 Selection structures
 Iteration structures
 PutInt8
 GetInt8
2003
 S. Dandamudi
Chapter 12: Page 2
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Outline (cont’d)
• Logical expressions in
high-level languages
 Representation of
Boolean data
 Logical expressions
• String representation
• String instructions




Repetition prefixes
Direction flag
String move instructions
String compare
instructions
 String scan instructions
• Logical expression
evaluation
 Full evaluation
 Partial evaluation
• Bit instructions
• Illustrative examples
 Bit test and modify
 Bit scan
• Illustrative examples
2003
 str_len
 str_mov
• Indirect procedure call
 S. Dandamudi
Chapter 12: Page 3
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags
2003
 S. Dandamudi
Chapter 12: Page 4
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Status flags are updated to indicate certain
properties of the result
 Example: If the result is zero, zero flag is set
• Once a flag is set, it remains in that state until
another instruction that affects the flags is
executed
• Not all instructions affect all status flags
 add and sub affect all six flags
 inc and dec affect all but the carry flag
 mov, push, and pop do not affect any flags
2003
 S. Dandamudi
Chapter 12: Page 5
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Example
; initially, assume ZF = 0
mov
AL,55H ; ZF is still zero
sub
AL,55H ; result is 0
; ZF is set (ZF = 1)
push
BX
; ZF remains 1
mov
BX,AX
; ZF remains 1
pop
DX
; ZF remains 1
mov
CX,0
; ZF remains 1
inc
CX
; result is 1
; ZF is cleared (ZF = 0)
2003
 S. Dandamudi
Chapter 12: Page 6
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Zero Flag
 Indicates zero result
– If the result is zero, ZF = 1
– Otherwise, ZF = 0
 Zero can result in several ways (e.g. overflow)
mov
add
AL,0FH
AL,0F1H
mov
inc
AX,0FFFFH
AX
mov
dec
AX,1
AX
» All three examples result in zero result and set ZF
 Related instructions
jz jump if zero (jump if ZF = 1)
jnz jump if not zero (jump if ZF = 0)
2003
 S. Dandamudi
Chapter 12: Page 7
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Uses of zero flag
 Two main uses of zero flag
» Testing equality
– Often used with cmp instruction
cmp
char,’$’ ; ZF = 1 if char is $
cmp
AX,BX
» Counting to a preset value
– Initialize a register with the count value
– Decrement it using dec instruction
– Use jz/jnz to transfer control
2003
 S. Dandamudi
Chapter 12: Page 8
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Assembly code
• Consider the following
code
sum := 0
for (i = 1 to M)
for (j = 1 to N)
sum := sum + 1
end for
end for
2003
sub
AX,AX ; AX := 0
mov
DX,M
outer_loop:
mov
CX,N
inner_loop:
inc
AX
loop inner_loop
dec
DX
jnz
outer_loop
exit_loops:
mov
sum,AX
 S. Dandamudi
Chapter 12: Page 9
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Two observations
 loop instruction is equivalent to
dec
DX
jnz
outer_loop
» This two instruction sequence is more efficient than the loop
instruction (takes less time to execute)
» loop instruction does not affect any flags!
 This two instruction sequence is better than initializing
DX = 1 and executing
inc
cmp
jle
2003
DX
DX,M
inner_loop
 S. Dandamudi
Chapter 12: Page 10
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Carry Flag
 Records the fact that the result of an arithmetic
operation on unsigned numbers is out of range
 The carry flag is set in the following examples
mov
add
AL,0FH
AL,0F1H
mov
sub
AX,12AEH
AX,12AFH
 Range of 8-, 16-, and 32-bit unsigned numbers
size
2003
range
8 bits
0 to 255 (28 - 1)
16 bits
32 bits
0 to 65,535 (216 - 1)
0 to 4,294,967,295 (232-1)
 S. Dandamudi
Chapter 12: Page 11
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
 Carry flag is not set by inc and dec instructions
» The carry flag is not set in the following examples
mov
AL,0FFH
mov
AX,0
inc
AL
dec
AX
 Related instructions
jc
jnc
jump if carry (jump if CF = 1)
jump if no carry (jump if CF = 0)
 Carry flag can be manipulated directly using
stc
clc
cmc
2003
set carry flag (set CF to 1)
clear carry flag (clears CF to 0)
complement carry flag (inverts CF value)
 S. Dandamudi
Chapter 12: Page 12
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Uses of carry flag
 To propagate carry/borrow in multiword
addition/subtraction
1
x = 3710 26A8
y = 489B A321
7FAB C9CA
 carry from lower 32 bits
1257 9AE7H
FE60 4213H
10B7 DCFAH
 To detect overflow/underflow condition
» In the last example, carry out of leftmost bit indicates overflow
 To test a bit using the shift/rotate instructions
» Bit shifted/rotated out is captured in the carry flag
» We can use jc/jnc to test whether this bit is 1 or 0
2003
 S. Dandamudi
Chapter 12: Page 13
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Overflow flag
 Indicates out-of-range result on signed numbers
– Signed number counterpart of the carry flag
 The following code sets the overflow flag but not the
carry flag
mov
add
AL,72H
AL,0EH
; 72H = 114D
; 0EH = 14D
 Range of 8-, 16-, and 32-bit signed numbers
size
range
8 bits
- 128 to +127
16 bits - 32,768 to +32,767
32 bits -2,147,483,648 to +2,147,483,647
2003
 S. Dandamudi
27 to (27 - 1)
215 to (215 - 1)
231 to (231 - 1)
Chapter 12: Page 14
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Signed or unsigned: How does the system know?
 The processor does not know the interpretation
 It sets carry and overflow under each interpretation
Unsigned interpretation
Signed interpretation
mov
AL,72H
add
AL,0EH
jc
overflow
no_overflow:
mov
AL,72H
add
AL,0EH
jo
overflow
no_overflow:
(no overflow code here)
....
overflow:
(no overflow code here)
....
overflow:
(overflow code here)
....
(overflow code here)
....
2003
 S. Dandamudi
Chapter 12: Page 15
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
 Related instructions
jo
jno
jump if overflow (jump if OF = 1)
jump if no overflow (jump if OF = 0)
 There is a special software interrupt instruction
into
interrupt on overflow
Details on this instruction in Chapter 20
• Uses of overflow flag
 Main use
» To detect out-of-range result on signed numbers
2003
 S. Dandamudi
Chapter 12: Page 16
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Sign flag
 Indicates the sign of the result
– Useful only when dealing with signed numbers
– Simply a copy of the most significant bit of the result
 Examples
mov
AL,15
add
AL,97
clears the sign flag as
the result is 112
(or 0111000 in binary)
mov
AL,15
sub
AL,97
sets the sign flag as
the result is -82
(or 10101110 in binary)
 Related instructions
js
jns
2003
jump if sign (jump if SF = 1)
jump if no sign (jump if SF = 0)
 S. Dandamudi
Chapter 12: Page 17
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Usage of sign flag
 To test the sign of the result
 Also useful to efficiently implement countdown loops
• Consider the count down loop:
for (i = M downto 0)
<loop body>
end for
The count down loop can be
implemented as
• If we don’t use the jns, we
need cmp as shown below:
cmp
CX,0
jl
for_loop
2003
 S. Dandamudi
mov
CX,M
for_loop:
<loop body>
dec
CX
jns
for_loop
Chapter 12: Page 18
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Auxiliary flag
 Indicates whether an operation produced a carry or
borrow in the low-order 4 bits (nibble) of 8-, 16-, or 32bit operands (i.e. operand size doesn’t matter)
 Example
mov
add
AL,43
AL,94
1
43D = 0010
94D = 0101
137D = 1000
 carry from lower 4 bits
1011B
1110B
1001B
» As there is a carry from the lower nibble, auxiliary flag is set
2003
 S. Dandamudi
Chapter 12: Page 19
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
 Related instructions
» No conditional jump instructions with this flag
» Arithmetic operations on BCD numbers use this flag
aaa
ASCII adjust for addition
aas
ASCII adjust for subtraction
aam
ASCII adjust for multiplication
aad
ASCII adjust for division
daa
Decimal adjust for addition
das
Decimal adjust for subtraction
– Appendices I has more details on these instructions
 Usage
» Main use is in performing arithmetic operations on BCD
numbers
2003
 S. Dandamudi
Chapter 12: Page 20
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
• Parity flag
 Indicates even parity of the low 8 bits of the result
– PF is set if the lower 8 bits contain even number 1 bits
– For 16- and 32-bit values, only the least significant 8 bits
are considered for computing parity value
 Example
mov
add
AL,53
AL,89
53D = 0011 0101B
89D = 0101 1001B
142D = 1000 1110B
» As the result has even number of 1 bits, parity flag is set
 Related instructions
jp
jnp
2003
jump on even parity (jump if PF = 1)
jump on odd parity (jump if PF = 0)
 S. Dandamudi
Chapter 12: Page 21
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Status Flags (cont’d)
 Usage of parity flag
» Useful in writing data encoding programs
» Example: Encodes the byte in AL (MSB is the parity bit)
parity_encode PROC
shl AL
jp
parity_zero
stc
jmp move_parity_bit
parity_zero:
clc
move_parity_bit:
rcr AL
parity_encode ENDP
2003
 S. Dandamudi
; CF = 1
; CF = 0
Chapter 12: Page 22
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions
• Pentium provides several arithmetic instructions
that operate on 8-, 16- and 32-bit operands
» Addition: add, adc, inc
» Subtraction: sub, sbb, dec, neg, cmp
Discussed
in Chapter 9
» Multiplication: mul, imul
» Division: div, idiv
» Related instructions: cbw, cwd, cdq, cwde, movsx, movzx
 There are few other instructions such as aaa, aas, etc.
that operate on decimal numbers
» See Appendix I for details
2003
 S. Dandamudi
Chapter 12: Page 23
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Multiplication
 More complicated than add/sub
» Produces double-length results
– E.g. Multiplying two 8 bit numbers produces a 16-bit
result
» Cannot use a single multiply instruction for signed and
unsigned numbers
– add and sub instructions work both on signed and
unsigned numbers
– For multiplication, we need separate instructions
mul
for unsigned numbers
imul for signed numbers
2003
 S. Dandamudi
Chapter 12: Page 24
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Unsigned multiplication
mul
source
» Depending on the source operand size, the location of the
other source operand and destination are selected
2003
 S. Dandamudi
Chapter 12: Page 25
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
 Example
mov
mov
mul
AL,10
DL,25
DL
produces 250D in AX register (result fits in AL)
• The imul instruction can use the same syntax
» Also supports other formats
 Example
mov
mov
imul
DL,0FFH
AL,0BEH
DL
; DL = -1
; AL = -66
produces 66D in AX register (again, result fits in AL)
2003
 S. Dandamudi
Chapter 12: Page 26
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Division instruction
 Even more complicated than multiplication
» Produces two results
– Quotient
– Remainder
» In multiplication, using a double-length register, there will not
be any overflow
– In division, divide overflow is possible
Pentium provides a special software interrupt when a
divide overflow occurs
 Two instructions as in multiplication
div
idiv
2003
source
source
 S. Dandamudi
for unsigned numbers
for signed numbers
Chapter 12: Page 27
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Dividend is twice the size of the divisor
• Dividend is assumed to be in
 AX (8-bit divisor)
 DX:AX (16-bit divisor)
 EDX:EAX (32-bit divisor)
2003
 S. Dandamudi
Chapter 12: Page 28
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
2003
 S. Dandamudi
Chapter 12: Page 29
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Example
mov
mov
div
AX,251
CL,12
CL
produces 20D in AL and 11D as remainder in AH
• Example
sub
mov
mov
div
DX,DX
AX,141BH
CX,012CH
CX
; clear DX
; AX = 5147D
; CX = 300D
produces 17D in AX and 47D as remainder in DX
2003
 S. Dandamudi
Chapter 12: Page 30
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Signed division requires some help
» We extended an unsigned 16 bit number to 32 bits by placing
zeros in the upper 16 bits
» This will not work for signed numbers
– To extend signed numbers, you have to copy the sign bit
into those upper bit positions
 Pentium provides three instructions in aiding sign
extension
» All three take no operands
cbw converts byte to word (extends AL into AH)
cwd converts word to doubleword (extends AX into DX)
cdq converts doubleword to quadword
(extends EAX into EDX)
2003
 S. Dandamudi
Chapter 12: Page 31
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
 Some additional related instructions
» Sign extension
cwde converts word to doubleword
(extends AX into EAX)
» Two move instructions
movsx
movzx
dest,src (move sign-extended src to dest)
dest,src (move zero-extended src to dest)
» For both move instructions, dest has to be a register
» The src operand can be in a register or memory
– If src is 8-bits, dest must be either a 16- or 32-bit register
– If src is 16-bits, dest must be a 32-bit register
2003
 S. Dandamudi
Chapter 12: Page 32
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Example
mov
cbw
mov
idiv
AL,-95
; AH = FFH
CL,12
CL
produces -7D in AL and -11D as remainder in AH
• Example
mov
cwd
mov
idiv
AX,-5147
; DX := FFFFH
CX,300
CX
produces -17D in AX and -47D as remainder in DX
2003
 S. Dandamudi
Chapter 12: Page 33
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Arithmetic Instructions (cont’d)
• Use of Shifts for Multiplication and Division
 Shifts are more efficient
 Example: Multiply AX by 32
mov
CX,32
imul CX
takes 12 clock cycles
 Using
sal
AX,5
takes just one clock cycle
2003
 S. Dandamudi
Chapter 12: Page 34
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Application Examples
• PutInt8 procedure
 To display a number, repeatedly divide it by 10 and
display the remainders obtained
108/10
10/10
1/10
quotient
10
1
0
remainder
8
0
1
 To display digits, they must be converted to their
character form
» This means simply adding the ASCII code for zero
line 24:
add
AH,’0’
2003
 S. Dandamudi
Chapter 12: Page 35
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Application Examples (cont’d)
• GetInt8 procedure
 To read a number, read each digit character
» Convert to its numeric equivalent
» Multiply the running total by 10 and add this digit
Input digit
Initial value
‘1’
‘5’
‘8’
2003
Numeric
value (N)
-1
5
8
Number := Number*10 + N
0
0 * 10 + 1 = 1
1 * 10 + 5 = 15
15 * 10 + 8 = 158
 S. Dandamudi
Chapter 12: Page 36
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Indirect Jumps
• Direct jump
 Target address is encoded in the instruction itself
• Indirect jump
 Introduces a level of indirection
» Address is specified either through memory of a generalpurpose register
 Example
jmp
CX
jumps to the address in CX
 Address is absolute
» Not relative as in direct jumps
2003
 S. Dandamudi
Chapter 12: Page 37
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Indirect Jumps (cont’d)
Switch (ch) {
Case ’0’:
count[0]++;
Case ’1’:
count[1]++;
Case ’2’:
count[2]++;
Case ’3’:
count[3]++;
Default:
count[3]++;
}
2003
break;
break;
break;
break;
 S. Dandamudi
Chapter 12: Page 38
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Indirect Jumps (cont’d)
Turbo C assembly code for the switch statement
_main
Indirect
jump
2003
PROC
NEAR
. . .
mov
AL,ch
cbw
sub
AX,48
; 48 = ASCII for 0
mov
BX,AX
cmp
BX,3
ja
default
shl
BX,1
; BX = BX * 2
jmp
WORD PTR CS:jump_table[BX]
 S. Dandamudi
Chapter 12: Page 39
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Indirect Jumps (cont’d)
case_0:
inc
jmp
case_1: inc
jmp
case_2: inc
jmp
case_3: inc
jmp
default: inc
end_switch:
. .
_main
ENDP
2003
WORD PTR [BP-10]
SHORT end_switch
WORD PTR [BP-8]
SHORT end_switch
WORD PTR [BP-6]
SHORT end_switch
WORD PTR [BP-4]
SHORT end_switch
WORD PTR [BP-2]
.
 S. Dandamudi
Chapter 12: Page 40
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Indirect Jumps (cont’d)
jump_table
LABEL
DW
DW
DW
DW
. .
WORD
case_0
case_1
case_2
case_3
.
Jump table for
the indirect jump
• Indirect jump uses this table to jump to the appropriate
case routine
• The indirect jump instruction uses segment override prefix
to refer to the jump_table in the CODE segment
2003
 S. Dandamudi
Chapter 12: Page 41
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Conditional Jumps
• Three types of conditional jumps
 Jumps based on the value of a single flag
» Arithmetic flags such as zero, carry can be tested using these
instructions
 Jumps based on unsigned comparisons
» Operands of cmp instruction are treated as unsigned
numbers
 Jumps based on signed comparisons
» Operands of cmp instruction are treated as signed numbers
2003
 S. Dandamudi
Chapter 12: Page 42
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Single Flags
Testing for zero
2003
jz
je
jump if zero
jump if equal
jumps if ZF = 1
jumps if ZF = 1
jnz
jne
jump if not zero
jump if not equal
jumps if ZF = 0
jumps if ZF = 0
jcxz
jump if CX = 0
jumps if CX = 0
(Flags are not tested)
 S. Dandamudi
Chapter 12: Page 43
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Single Flags (cont’d)
Testing for carry
jc
jnc
jump if carry
jump if no carry
jumps if CF = 1
jumps if CF = 0
Testing for overflow
jo
jno
jump if overflow
jump if no overflow
jumps if OF = 1
jumps if OF = 0
Testing for sign
js
jns
2003
jump if negative
jump if not negative
 S. Dandamudi
jumps if SF = 1
jumps if SF = 0
Chapter 12: Page 44
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Single Flags (cont’d)
Testing for parity
2003
jp
jpe
jump if parity
jump if parity
is even
jumps if PF = 1
jumps if PF = 1
jnp
jpo
jump if not parity
jump if parity
is odd
jumps if PF = 0
jumps if PF = 0
 S. Dandamudi
Chapter 12: Page 45
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Unsigned Comparisons
Mnemonic
je
jz
Meaning
jump if equal
jump if zero
jne
jnz
jump if not equal ZF = 0
jump if not zero ZF = 0
ja
jnbe
jump if above
CF = ZF = 0
jump if not below CF = ZF = 0
or equal
2003
 S. Dandamudi
Condition
ZF = 1
ZF = 1
Chapter 12: Page 46
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Unsigned Comparisons
Mnemonic
jae
jnb
Meaning
Condition
jump if above
CF = 0
or equal
jump if not below CF = 0
jb
jnae
jump if below
CF = 1
jump if not above CF = 1
or equal
jbe
jump if below
CF=1 or ZF=1
or equal
jump if not above CF=1 or ZF=1
jna
2003
 S. Dandamudi
Chapter 12: Page 47
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Signed Comparisons
Mnemonic
je
jz
Meaning
jump if equal
jump if zero
jne
jnz
jump if not equal ZF = 0
jump if not zero ZF = 0
jg
jnle
jump if greater
jump if not less
or equal
2003
 S. Dandamudi
Condition
ZF = 1
ZF = 1
ZF=0 & SF=OF
ZF=0 & SF=OF
Chapter 12: Page 48
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Jumps Based on Signed Comparisons (cont’d)
Mnemonic
jge
Meaning
jump if greater
or equal
jump if not less
Condition
SF = OF
jl
jnge
jump if less
jump if not greater
or equal
SF  OF
SF  OF
jle
jump if less
or equal
jump if not greater
ZF=1 or SF  OF
jnl
jng
2003
 S. Dandamudi
SF = OF
ZF=1 or SF  OF
Chapter 12: Page 49
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Implementing HLL Decision Structures
• High-level language decision structures can be
implemented in a straightforward way
• See Section 12.4 for examples that implement







2003
if-then-else
if-then-else with a relational operator
if-then-else with logical operator AND
if-then-else with logical operator OR
while loop
repeat-until loop
for loops
 S. Dandamudi
Chapter 12: Page 50
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Logical Expressions in HLLs
• Representation of Boolean data
 Only a single bit is needed to represent Boolean data
 Usually a single byte is used
» For example, in C
– All zero bits represents false
– A non-zero value represents true
• Logical expressions
 Logical instructions AND, OR, etc. are used
• Bit manipulation
 Logical, shift, and rotate instructions are used
2003
 S. Dandamudi
Chapter 12: Page 51
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Evaluation of Logical Expressions
• Two basic ways
 Full evaluation
» Entire expression is evaluated before assigning a value
» PASCAL uses full evaluation
 Partial evaluation
» Assigns as soon as the final outcome is known without blindly
evaluating the entire logical expression
» Two rules help:
– cond1 AND cond2
If cond1 is false, no need to evaluate cond2
– cond1 OR cond2
If cond1 is true, no need to evaluate cond2
2003
 S. Dandamudi
Chapter 12: Page 52
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Evaluation of Logical Expressions (cont’d)
• Partial evaluation
 Used by C
• Useful in certain cases to avoid run-time errors
• Example
if ((X > 0) AND (Y/X > 100))
 If x is 0, full evaluation results in divide error
 Partial evaluation will not evaluate (Y/X > 100) if
X = 0
• Partial evaluation is used to test if a pointer value
is NULL before accessing the data it points to
2003
 S. Dandamudi
Chapter 12: Page 53
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Bit Instructions
• Bit Test and Modify Instructions
 Four bit test instructions
 Each takes the position of the bit to be tested
Instruction
bt (Bit Test)
bts (Bit Test and Set)
btr (Bit Test and Reset)
btc
Effect on the selected bit
No effect
selected bit  1
selected bit  0
selected bit  NOT(selected bit)
(Bit Test and Complement)
2003
 S. Dandamudi
Chapter 12: Page 54
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Bit Instructions (cont’d)
• All four instructions have the same format
• We use bt to illustrate the format
bt
operand,bit_pos
 operand is word or doubleword
» Can be in a register or memory
 bit_pos indicates the position of the bit to be tested
» Can be an immediate value or in a 16/32-bit register
• Instructions in this group affect only the carry flag
» Other five flags are undefined
2003
 S. Dandamudi
Chapter 12: Page 55
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Bit Scan Instructions
• These instructions scan the operand for a 1 bit
 return the bit position in a register
• Two instructions
bsf
dest_reg,operand ;bit scan forward
bsr
dest_reg,operand ;bit scan reverse
» operand can be a word or doubleword in a register or
memory
» dest_reg receives the bit position
– Must be a 16- or 32-bit register
 Only ZF is updated (other five flags undefined)
– ZF = 1 if all bits of operand are 0
– ZF = 0 otherwise (position of first 1 bit in dest_reg)
2003
 S. Dandamudi
Chapter 12: Page 56
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Illustrative Examples
• Example 1
 Linear search of an integer array
• Example 2
 Selection sort on an integer array
• Example 3
 Multiplication using shift and add operations
» Multiplies two unsigned 8-bit numbers
– Uses a loop that iterates 8 times
• Example 4
 Multiplication using bit instructions
2003
 S. Dandamudi
Chapter 12: Page 57
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Representation
• Two types
 Fixed-length
 Variable-length
• Fixed length strings
 Each string uses the same length
» Shorter strings are padded (e.g. by blank characters)
» Longer strings are truncated
 Selection of string length is critical
» Too large ==> inefficient
» Too small ==> truncation of larger strings
2003
 S. Dandamudi
Chapter 12: Page 58
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Representation (cont’d)
• Variable-length strings
 Avoids the pitfalls associated with fixed-length strings
• Two ways of representation
 Explicitly storing string length (used in PASCAL)
string
str_len
DB
DW
‘Error message’
$-string
– $ represents the current value of the location counter
$ points to the byte after the last character of string
 Using a sentinel character (used in C)
» Uses NULL character
– Such NULL-terminated strings are called ASCIIZ strings
2003
 S. Dandamudi
Chapter 12: Page 59
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Instructions
• Five string instructions
LODS
STOS
MOVS
CMPS
SCAS
LOaD String
STOre String
MOVe String
CoMPare String
SCAn String
source
destination
source & destination
source & destination
destination
• Specifying operands
 32-bit segments:
DS:ESI = source operand
ES:EDI = destination operand
 16-bit segments:
DS:SI = source operand
2003
 S. Dandamudi
ES:DI = destination operand
Chapter 12: Page 60
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Instructions (cont’d)
• Each string instruction
 Can operate on 8-, 16-, or 32-bit operands
 Updates index register(s) automatically
» Byte operands: increment/decrement by 1
» Word operands: increment/decrement by 2
» Doubleword operands: increment/decrement by 4
• Direction flag
 DF = 0: Forward direction (increments index registers)
 DF = 1: Backward direction (decrements index registers)
• Two instructions to manipulate DF
std
cld
2003
set direction flag (DF = 1)
clear direction flag (DF = 0)
 S. Dandamudi
Chapter 12: Page 61
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Repetition Prefixes
• String instructions can be repeated by using a
repetition prefix
• Two types
 Unconditional repetition
rep
REPeat
 Conditional repetition
repe/repz
REPeat while Equal
REPeat while Zero
repne/repnz REPeat while Not Equal
REPeat while Not Zero
2003
 S. Dandamudi
Chapter 12: Page 62
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Repetition Prefixes (cont’d)
rep
while (CX  0)
execute the string instruction
CX := CX-1
end while
• CX register is first checked
 If zero, string instruction is not executed at all
 More like the JCXZ instruction
2003
 S. Dandamudi
Chapter 12: Page 63
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Repetition Prefixes (cont’d)
repe/repz
while (CX  0)
execute the string instruction
CX := CX-1
if (ZF = 0)
then
exit loop
end if
end while
• Useful with cmps and scas string instructions
2003
 S. Dandamudi
Chapter 12: Page 64
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Repetition Prefixes (cont’d)
repne/repnz
while (CX  0)
execute the string instruction
CX := CX-1
if (ZF = 1)
then
exit loop
end if
end while
2003
 S. Dandamudi
Chapter 12: Page 65
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions
• Three basic instructions
 movs, lods, and stos
Move a string (movs)
• Format
movs
dest_string,source_string
movsb
; operands are bytes
movsw
; operands are words
movsd
; operands are doublewords
• First form is not used frequently
 Source and destination pointed by DS:(E)SI and
ES:(E)DI, respectively
2003
 S. Dandamudi
Chapter 12: Page 66
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
movsb --- move a byte string
ES:DI := (DS:SI) ; copy a byte
if (DF=0)
; forward direction
then
SI := SI+1
DI := DI+1
else
; backward direction
SI := SI-1
DI := DI-1
end if
Flags affected: none
2003
 S. Dandamudi
Chapter 12: Page 67
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
Example
.DATA
string1
DB
'The original string',0
strLen
EQU
$ - string1
string2
DB
80 DUP (?)
.CODE
.STARTUP
mov
AX,DS
; set up ES
mov
ES,AX
; to the data segment
mov
CX,strLen
; strLen includes NULL
mov
SI,OFFSET string1
mov
DI,OFFSET string2
cld
; forward direction
rep
movsb
2003
 S. Dandamudi
Chapter 12: Page 68
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
Load a String (LODS)
• Copies the value from the source string at
DS:(E)SI to
 AL (lodsb)
 AX (lodsw)
 EAX (lodsd)
• Repetition prefix does not make sense
 It leaves only the last value in AL, AX, or EAX register
2003
 S. Dandamudi
Chapter 12: Page 69
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
lodsb --- load a byte string
AL := (DS:SI)
; copy a byte
if (DF=0)
; forward direction
then
SI := SI+1
else
; backward direction
SI := SI-1
end if
Flags affected: none
2003
 S. Dandamudi
Chapter 12: Page 70
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
Store a String (STOS)
• Performs the complementary operation
• Copies the value in
» AL (lodsb)
» AX (lodsw)
» EAX (lodsd)
to the destination string at ES:(E)DI
• Repetition prefix can be used to initialize a block
of memory
2003
 S. Dandamudi
Chapter 12: Page 71
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
stosb --- store a byte string
ES:DI := AL; copy a byte
if (DF=0)
; forward direction
then
DI := DI+1
else
; backward direction
DI := DI-1
end if
Flags affected: none
2003
 S. Dandamudi
Chapter 12: Page 72
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
Example: Initializes array1 with -1
.DATA
array1
DW
100 DUP (?)
.CODE
.STARTUP
mov
AX,DS
; set up ES
mov
ES,AX
; to the data segment
mov
CX,100
mov
DI,OFFSET array1
mov
AX,-1
cld
; forward direction
rep
stosw
2003
 S. Dandamudi
Chapter 12: Page 73
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Move Instructions (cont’d)
• In general, repeat prefixes are not useful with
lods and stos
• Used in a loop to do conversions while copying
mov
CX,strLen
mov
SI,OFFSET string1
mov
DI,OFFSET string2
cld
; forward direction
loop1:
lodsb
or
AL,20H
stosb
loop loop1
done:
2003
 S. Dandamudi
Chapter 12: Page 74
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Compare Instruction
cmpsb --- compare two byte strings
Compare two bytes at DS:SI and ES:DI and set flags
if (DF=0)
then
SI := SI+1
DI := DI+1
else
SI := SI-1
DI := DI-1
end if
; forward direction
; backward direction
Flags affected: As per cmp instruction (DS:SI)-(ES:DI)
2003
 S. Dandamudi
Chapter 12: Page 75
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Compare Instruction (cont’d)
.DATA
string1
DB
'abcdfghi',0
strLen
EQU
$ - string1
string2
DB
'abcdefgh',0
.CODE
.STARTUP
mov
AX,DS
; set up ES
mov
ES,AX
; to the data segment
mov
CX,strLen
mov
SI,OFFSET string1
mov
DI,OFFSET string2
cld
; forward direction
repe
cmpsb
dec
SI
dec
DI
; leaves SI & DI pointing to the last character that differs
2003
 S. Dandamudi
Chapter 12: Page 76
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Compare Instruction (cont’d)
.DATA
string1
DB
'abcdfghi',0
strLen
EQU
$ - string1 - 1
string2
DB
'abcdefgh',0
.CODE
.STARTUP
mov
AX,DS
; set up ES
mov
ES,AX
; to the data segment
mov
CX,strLen
mov
SI,OFFSET string1 + strLen - 1
mov
DI,OFFSET string2 + strLen - 1
std
; backward direction
repne cmpsb
inc
SI ; Leaves SI & DI pointing to the first character that matches
inc
DI ; in the backward direction
2003
 S. Dandamudi
Chapter 12: Page 77
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Scan Instruction
scasb --- Scan a byte string
Compare AL to the byte at ES:DI and set flags
if (DF=0)
; forward direction
then
DI := DI+1
else
; backward direction
DI := DI-1
end if
Flags affected: As per cmp instruction (DS:SI)-(ES:DI)
• scasw uses AX and scasd uses EAX registers instead of
AL
2003
 S. Dandamudi
Chapter 12: Page 78
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Scan Instruction (cont’d)
.DATA
string1
DB
'abcdefgh',0
Example 1
strLen
EQU
$ - string1
.CODE
.STARTUP
mov
AX,DS
; set up ES
mov
ES,AX
; to the data segment
mov
CX,strLen
mov
DI,OFFSET string1
mov
AL,'e'
; character to be searched
cld
; forward direction
repne scasb
dec
DI ; leaves DI pointing to e in string1
2003
 S. Dandamudi
Chapter 12: Page 79
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
String Scan Instruction (cont’d)
.DATA
Example 2
string1
DB
'
abc',0
strLen
EQU
$ - string1
.CODE
.STARTUP
mov
AX,DS
; set up ES
mov
ES,AX
; to the data segment
mov
CX,strLen
mov
DI,OFFSET string1
mov
AL,' '
; character to be searched
cld
; forward direction
repe
scasb
dec
DI ; leaves DI pointing to the first non-blank character a
2003
 S. Dandamudi
Chapter 12: Page 80
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Illustrative Examples
LDS and LES instructions
• String pointer can be loaded into DS/SI or ES/DI
register pair by using lds or les instructions
• Syntax
lds
register,source
les
register,source
 register should be a 16-bit register
 source is a pointer to a 32-bit memory operand
• register is typically SI in lds and DI in les
2003
 S. Dandamudi
Chapter 12: Page 81
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Illustrative Examples (cont’d)
• Actions of lds and les
lds
register := (source)
DS := (source+2)
les
register := (source)
ES := (source+2)
• Pentium also supports lfs, lgs, and lss to load
the other segment registers
2003
 S. Dandamudi
Chapter 12: Page 82
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Illustrative Examples (cont’d)
• Seven popular string processing routines are given
as examples in string.asm
 str_len
 str_mov
Given in the text
 str-cpy
 str_cat
 str_cmp
 str_chr
 str_cnv
2003
 S. Dandamudi
Chapter 12: Page 83
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.
Indirect Procedure Call
• Direct procedure calls specify the offset of the first
instruction of the called procedure
• In indirect procedure call, the offset is specified
through memory or a register
 If BX contains pointer to the procedure, we can use
call
BX
 If the word in memory at target_proc_ptr
contains the offset of the called procedure, we can use
call
target_proc_ptr
• These are similar to direct and indirect jumps
Last slide
2003
 S. Dandamudi
Chapter 12: Page 84
To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer, 2003.