Interrupts & Input/output
Download
Report
Transcript Interrupts & Input/output
Selection and Iteration
Chapter 8
S. Dandamudi
Outline
• Unconditional jump
• Compare instruction
• Conditional jumps
Single flags
Unsigned comparisons
Signed comparisons
• Loop instructions
2005
• Implementing high-level
language decision
structures
Selection structures
Iteration structures
• Illustrative examples
• Indirect jumps
Multiway conditional
statements
S. Dandamudi
Chapter 8: Page 2
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Unconditional Jump
• Unconditional jump transfers control to the
instruction at the target address
• Format
jmp
target
• Specification of target
Direct
» Target address is specified as a part of the instruction
Indirect
» Target address is specified indirectly either through memory or
a general-purpose register
2005
S. Dandamudi
Chapter 8: Page 3
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Unconditional Jump (cont’d)
Example
• Two jump instructions
Forward jump
jmp
ECX_init_done
Backward jump
jmp
repeat1
• Programmer specifies
target by a label
• Assembler computes the
offset using the symbol
table
2005
. . .
mov
ECX,10
jmp
ECX_init_done
init_CX_20:
mov
ECX,20
CX_init_done:
mov
EAX,ECX
repeat1:
dec
ECX
. . .
jmp
repeat1
. . .
S. Dandamudi
Chapter 8: Page 4
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Unconditional Jump (cont’d)
• Address specified in the jump instruction is not the
absolute address
Uses relative address
» Specifies relative byte displacement between the target
instruction and the instruction following the jump instruction
» Displacement is w.r.t the instruction following jmp
– Reason: EIP is already pointing to this instruction
Execution of jmp involves adding the displacement
value to current EIP
Displacement is a signed number
» Negative value for backward jumps
» Positive value for forward jumps
2005
S. Dandamudi
Chapter 8: Page 5
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Target Location
• Inter-segment jump
Target is in another segment
CS = target-segment (2 bytes)
EIP = target-offset (4 bytes)
» Called far jumps (needs five bytes to encode jmp)
• Intra-segment jumps
Target is in the same segment
EIP = EIP + relative-displacement
Uses 1-byte displacement if target is within -128 to +127
» Called short jumps (needs two bytes to encode jmp)
If target is outside this range, uses 2/4-byte displacement
» Called near jumps (needs 3 or 5 bytes to encode jmp)
2005
S. Dandamudi
Chapter 8: Page 6
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Target Location (cont’d)
• In most cases, the assembler can figure out the
type of jump
• For backward jumps, assembler can decide
whether to use the short jump form or not
• For forward jumps, it needs a hint from the
programmer
Use SHORT prefix to the target label
If such a hint is not given
» Assembler reserves three bytes for jmp instruction
» If short jump can be used, leaves one byte of rogue data
– See the next example for details
2005
S. Dandamudi
Chapter 8: Page 7
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Example
167 0…09
.
EB 14
168 0…0B
169 0…10
B9
E9
170
171 0…15
172 0…1A
B9
E9
173
174 0…1F
89
2005
.
.
jmp
SHORT ECX_init_done
0…1F – 0…0B = 0…014
78563412 mov
ECX,12345678H
0A000000 jmp
ECX_init_done
0…1F – 0…15 = 0…00A
init_ECX:
12EFCDAB
mov
ECX,0ABCDEF12H
52060000
jmp
near_jump
0…671 – 0…01F = 0…652
ECX_init_done:
C8
mov
EAX,ECX
S. Dandamudi
Chapter 8: Page 8
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Example (cont’d)
175
176 0…21
177 0…22
557 0…662
558 0…667
559
560 0…66C
561
562 0…671
2005
repeat1:
49
dec
ECX
EB FD
jmp
repeat1
0…21 – 0…24 = -3 = FD
. . .
EB 05000000
jmp
short_jump
0…66C – 0…667 = 5
B9 FFFF00FF
mov
ECX, 0FF00FFFFH
short_jump:
BA 32547698
mov
EDX, 98765432H
near_jump:
E9 9FF9FFFF
jmp
init_ECX
0…015 – 0…676 = FFFFF99F
S. Dandamudi
Chapter 8: Page 9
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Compare Instruction
• Compare instruction can be used to test the
conditions
• Format
cmp
destination, source
• Updates the arithmetic flags by performing
destination - source
• The flags can be tested by a subsequent
conditional jump instruction
2005
S. Dandamudi
Chapter 8: Page 10
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
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
» The operands of cmp instruction are treated as unsigned
numbers
Jumps based on signed comparisons
» The operands of cmp instruction are treated as signed
numbers
2005
S. Dandamudi
Chapter 8: Page 11
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Jumps Based on Single Flags
Testing for zero
2005
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 8: Page 12
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
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
2005
jump if negative
jump if not negative
S. Dandamudi
jumps if SF = 1
jumps if SF = 0
Chapter 8: Page 13
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Jumps Based on Single Flags (cont’d)
Testing for parity
2005
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 8: Page 14
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
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
2005
S. Dandamudi
Condition
ZF = 1
ZF = 1
Chapter 8: Page 15
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
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
2005
S. Dandamudi
Chapter 8: Page 16
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
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
2005
S. Dandamudi
Condition
ZF = 1
ZF = 1
ZF=0 & SF=OF
ZF=0 & SF=OF
Chapter 8: Page 17
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Jumps Based on Signed Comparisons (cont’d)
Mnemonic
jge
jnl
Meaning
jump if greater
or equal
jump if not less
Condition
SF = OF
SF = OF
jl
jnge
jump if less
SF OF
jump if not greater SF OF
or equal
jle
jump if less
ZF=1 or SF OF
or equal
jump if not greater ZF=1 or SF OF
jng
2005
S. Dandamudi
Chapter 8: Page 18
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
A Note on Conditional Jumps
• All conditional jumps are encoded using 2 bytes
Treated as short jumps
• What if the target is outside this range?
• Use this code to get around
target:
. . .
cmp
AX,BX
je
target
mov
CX,10
. . .
target:
traget is out of range for a
short jump
2005
. . .
cmp
AX,BX
jne
skip1
jmp
target
skip1:
mov
CX,10
. . .
S. Dandamudi
Chapter 8: Page 19
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Loop Instructions
• Loop instructions use CX/ECX to maintain the
count value
• target should be within the range of a short
jump as in conditional jump instructions
• Three loop instructions
loop
target
Action: ECX = ECX-1
Jump to target if ECX 0
2005
S. Dandamudi
Chapter 8: Page 20
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Loop Instructions (cont’d)
• The following two loop instructions also test the
zero flag status
loope/loopz
target
Action: ECX = ECX - 1
Jump to target if (ECX 0 and ZF = 1)
loopne/loopnz
target
Action: ECX = ECX - 1
Jump to target if (ECX 0 and ZF = 0)
2005
S. Dandamudi
Chapter 8: Page 21
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Instruction Execution Times
• Functionally, loop instruction can be replaced by
dec
jnz
ECX
target
• loop instruction is slower than dec/jnz version
• loop requires 5/6 clocks whereas dec/jnz takes
only 2 clocks
• jcxz also takes 5/6 clocks
• Equivalent code (shown below) takes only 2 clocks
cmp
jz
2005
ECX,0
target
S. Dandamudi
Chapter 8: Page 22
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Implementing HLL Decision Structures
• High-level language decision structures can be
implemented in a straightforward way
• See Section 8.5 for examples that implement
2005
if-then-else
if-then-else with a relational operator
if-then-else with logical operators AND and OR
while loop
repeat-until loop
for loop
S. Dandamudi
Chapter 8: Page 23
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Illustrative Examples
• Two example programs
Linear search
» LIN_SRCH.ASM
» Searches an array of non-negative numbers for a given input
number
Selection sort
» SEL_SORT.ASM
» Uses selection sort algorithm to sort an integer array in
ascending order
2005
S. Dandamudi
Chapter 8: Page 24
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Indirect Jumps
• Jump target address is not specified directly as a
part of the jump instruction
• With indirect jump, we can specify target via a
general-purpose register or memory
Example: Assuming ECX has the offset value
jmp
[ECX]
Note: The offset value in indirect jump is the absolute
value (not relative value as in the direct jumps)
• Program example
IJUMP.ASM
» Uses a jump table to direct the jump
2005
S. Dandamudi
Chapter 8: Page 25
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Indirect Jumps (cont’d)
switch (ch)
{
case '0':
• Another example
Implementing multiway
jumps
count[0]++;
break;
» We use switch statement
of C
case '1':
count[1]++;
break;
We can use a table with
appropriate target pointers
for the indirect jump
Segment override is needed
case '2':
count[2]++;
break;
case '3':
» jump_table is in the
code segment (not in the
data segment)
count[3]++;
break;
default:
count[4]++;
}
2005
S. Dandamudi
Chapter 8: Page 26
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Indirect Jumps (cont’d)
_main
mov
cbw
sub
mov
cmp
ja
shl
jmp
case_0:
inc
jmp
case_1:
inc
jmp
2005
PROC
. . .
AL,ch
NEAR
AX,48 ; 48 =‘0’
BX,AX
BX,3
default
BX,1 ; BX= BX*2
WORD PTR
CS:jump_table[BX]
WORD PTR [BP-10]
SHORT end_switch
WORD PTR [BP-8]
SHORT end_switch
case_2:
inc
WORD PTR [BP-6]
jmp
SHORT end_switch
case_3:
inc
WORD PTR [BP-4]
jmp
SHORT end_switch
default:
inc
WORD PTR [BP-2]
end_switch:
. . .
_main
ENDP ;end of main
jump_table
dw
dw
dw
dw
.
S. Dandamudi
LABEL WORD
case_0
case_1
case_2
case_3
. .
Last slide
Chapter 8: Page 27
To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.