Chapter5 - davidspellman

Download Report

Transcript Chapter5 - davidspellman

Chapter 5
Branching and Looping
5.1 Unconditional Jumps
jmp Instruction
• Like a goto in a high-level language
• Format: jmp StatementLabel
• The next statement executed will be the
one at StatementLabel:
Program Design: Calculate 1+2+3+…
number := 0;
sum := 0;
forever loop
add 1 to number;
add number to sum;
end loop;
Program Code: Calculate 1+2+3+…
; program to find sum 1+2+...+n for n=1, 2, ...
.586
.MODEL FLAT
.STACK 4096
.DATA
.CODE
main
PROC
mov
ebx,0
; number := 0
mov
eax,0
; sum := 0
forever: inc
add
jmp
main
END
ENDP
ebx
eax, ebx
forever
; add 1 to number
; add number to sum
; repeat
Program Stopped at Breakpoint
jmp Encoding
• Relative short encodes a single byte signed
displacement telling how far forward or
backward to jump for the next instruction to
execute – the assembler uses this format if
possible
• Relative near encodes a signed doubleword
displacement – this allows a forward or
backward jump essentially anywhere in memory
• Indirect forms that encode the address of the
destination in a register or memory are not often
used
5.2 Conditional Jumps, Compare
Instructions and if Structures
Conditional Jump Instructions
• Format: j-- targetStatement
• The last part of the mnemonic identifies the
condition under which the jump is to be
executed
– If the condition holds, then the jump takes place and
the next statement executed is at targetStatement:
– Otherwise, the next instruction (the one following the
conditional jump) is executed
• Used to implement if structures, other selection
structures, and loop structures in 80x86
assembly language
Conditional Jumps and Flags
• Most “conditions” considered by the conditional
jump instructions are settings of flags in the
EFLAGS register.
• Example
jz endWhile
means to jump to the statement with label
endWhile if the zero flag ZF is set to 1
• Conditional jump instructions don’t modify flags;
they react to previously set flag values
cmp Instructions
• Most common way to set flags for
conditional jumps
• Format: cmp operand1, operand2
– Flags are set the same as for the subtraction
operation operand1 – operand2
– Operands are not changed
Conditional Jumps To Use After Signed
Operand Comparison
mnemonic
jumps if
jg
jnle
jump if greater
jump if not less or equal
SF=OF and ZF=0
jge
jnl
jump if greater or equal
jump if not less
SF=OF
jl
jnge
jump if less
jump if not above or equal
SFOF
jle
jng
jump if less or equal
jump if not greater
SFOF or ZF=1
Conditional Jumps To Use After Unsigned
Operand Comparison
mnemonic
jumps if
ja
jnbe
jump if above
jump if not below or equal
CF=0 and ZF=0
jae
jnb
jump if above or equal
jump if not below
CF=0
jb
jnae
jump if below
jump if not above or equal
CF=1
jbe
jna
jump if below or equal
jump if not above
CF=1 or ZF=1
Some Other Conditional Jumps
mnemonic
jumps if
je
jz
jump if equal
jump if zero
ZF=1
jne
jnz
jump if not equal
jump if not zero
ZF=0
js
jump if sign (negative)
SF=1
jc
jump if carry
CF=1
jo
jump if overflow
OF=1
Example Usage
cmp
jle
eax, nbr
smaller
• The jump will occur if the value in EAX is
less than or equal than the value in nbr,
where both are interpreted as signed
numbers
if Example 1
Design
if value < 10
then
add 1 to smallCount;
else
add 1 to largeCount;
end if;
Code
cmp ebx, 10
jnl elseLarge
inc smallCount
jmp endValueCheck
elseLarge: inc largeCount
endValueCheck:
Assumptions
• value in EBX
• smallCount and largeCount in memory
if Example 2
Design
if (total  100)
or (count = 10)
then
add value to total;
end if;
Code
cmp
jge
cmp
jne
addValue: mov
add
total, 100
addValue
ecx, 10
endAddCheck
ebx, value
total, ebx
endAddCheck:
Assumptions
• total and value in memory
• count in ECX
5.3 Implementing Loop
Structures
while Loops
• while pseudocode design
while continuation condition loop
... { body of loop }
end while;
• Typical while implementation
while1:
.
.
.
body:
.
.
.
jmp
endWhile1:
; code to check Boolean expression
; loop body
while1 ; go check condition again
Continuation Condition
• A Boolean expression
• Checked before the loop body is executed
– Whenever it is true the loop body is executed
and then the continuation condition is
checked again
– When it is false execution continues with the
statement following the loop.
• It may take several 80x86 statements to
evaluate and check a continuation
condition
while Example
Code
Design
whileSum:
while (sum < 1000) loop
add count to sum;
add 1 to count;
end while;
cmp
jnl
add
inc
jmp
endWhileSum:
Assumptions
• sum in memory
• count in ECX
sum, 1000
endWhileSum
sum, ecx
ecx
whileSum
for Loops
• Counter-controlled loop
• for pseudocode design
for index := initialValue to finalValue loop
... { body of loop }
end for;
• Loop body executed once for each value
of the loop index in the given range
for Implementation
• Convert for loop to equivalent while loop
index := initialValue;
while index ≤ finalValue loop
... { body of loop }
add 1 to index;
end while;
• Implement while loop in 80x86 code
• Section 5.4 shows another implementation
until Loops
• until pseudocode design
repeat
... { body of loop }
until termination condition;
• Termination condition checked after the
loop body is executed
– If true execution continues with the statement
following the until loop
– If false the loop body is executed again
until Example
Design
repeat
add 2*count to sum;
add 1 to count;
until (sum > 1000);
Code
repeatLoop:
add
add
inc
cmp
jng
endUntilLoop:
Assumptions
• sum in memory
• count in ECX
sum, ecx
sum, ecx
ecx
sum, 1000
repeatLoop
5.4 for Loops in Assembly
Language
for Loops
• Can be implemented by converting into
while loops
• 80x86 loop instruction designed to
implement “backward” counter-controlled
loops:
for index := count downto 1 loop
... { body of loop }
end for;
loop Instruction
• format: loop statementLabel
– statementLabel is the label of a statement which is a
short displacement from the loop instruction
• execution
– The value in ECX is decremented
– If the new value in ECX is zero, then execution
continues with the statement following the loop
instruction
– If the new value in ECX is non-zero, then a jump to
the instruction at statementLabel takes place
for Example
Design
sum := 0
for count := 20 downto 1 loop
add count to sum;
end for;
Code
mov
mov
forCount: add
loop
Assumptions
• sum in EAX
• count in ECX
eax, 0
ecx, 20
eax, ecx
forCount
Cautions
• If ECX is initially 0, then 00000000 will
be decremented to FFFFFFFF, then
FFFFFFFE, etc., for a total of
4,294,967,296 iterations
• The jecxz (“jump if ECX is zero”)
instruction can be used to guard a loop
implemented with the loop instruction
5.5 Arrays
Defining an Array
• Typically declare a collection of contiguous
elements in the data section
• Examples
– array1 DWORD 25, 47, 15, 50, 32
creates an array of 5 doublewords with initial
values
– array2 DWORD 1000 DUP (?)
creates an array of 1000 logically uninitialized
doublewords
Sequential Array Access
• Put the address of the first element in a
register (typically with a lea instruction)
• Register indirect addresssing allows the
register to “point at” the array element to
be used
• Add the element size to the register to
point at the next element
Example: Using Sequential Access
to Add 50 Doublewords in an Array
nbrArr
DWORD 50 DUP (?)
...
mov eax, 0
;
lea esi, nbrArr ;
mov ecx, 50
;
addElt: add eax, [esi] ;
add esi, 4
;
element
loop addElt
;
sum := 0
load array address
number of elements
add array element
point at next
repeat
Random Array Access
• Use indexed addressing
• Example nbrArray[4*ecx]
where nbrArray references the array, ECX
is the index of the element to be accessed
and scaling factor 4 gives doubleword
element size
Example: Using Random Access to
Add 50 Doublewords in an Array
nbrArr
DWORD 50 DUP (?)
...
mov eax, 0
; sum := 0
mov ecx, 50
; number of elements
mov esi, 0
; array index
addElt: add eax, nbrArr[4*esi] ; add element
inc esi
; increment array
index
loop addElt
; repeat