Download chapter7

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Compiler wikipedia , lookup

Go (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

Program optimization wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Subroutine wikipedia , lookup

Object-oriented programming wikipedia , lookup

Corecursion wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Structured programming wikipedia , lookup

Error detection and correction wikipedia , lookup

Coding theory wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Assembly language wikipedia , lookup

Transcript
CS344-321 Assembly Language Programming
chapter 7 page 1
________________________________________________________________________
Chapter 7
Control Instructions
mnemonic
call
ret
jmp
assembler format
Unconditional Transfers
call target
ret [popvalue]
jmp target
flags
OF DF IF TF SF ZF AF PF CF
-
- - - - - - - - - -
- - - - - - - - - -
Conditional Transfers
ja/jnbe ja shortlabel
jae/jnb jae shortlabel
jb/jnae/jc jb shortlabel
jcxz
jcxz shortlabel
je/jz
je shortlabel
jg/jnle jg shortlabel
jge/jnl jge shortlabel
jl/jnge jl shortlabel
jle/jng jle shortlabel
jnc
jnc shortlabel
jne/jnz jne shortlabel
jno
jno shortlabel
jnp/jpo jnp shortlabel
jns
jns shortlabel
jo
jo shortlabel
jp/jpe jp shortlabel
js
js shortlabel
-
-
-
Iteration Controls
loop
loop shortlabel
loope/loopz
loope shortlabel
loopne/loopnz loopne shortlabel
-
- - - - - - - - - -
Computer Science, Prince of Songkhla University
-
-
-
-
-
-
- - - - - - - - - -
CS344-321 Assembly Language Programming
chapter 7 page 2
________________________________________________________________________
Note! – means unchanged
7.1 Unconditional Transfers
CALL : Call a Procedure
Operation. Calls a near or far procedure. The assembler generates a near CALL if the called procedure
is NEAR and a far CALL if the called procedure is FAR. A near CALL pushes the IP (the address of the
next instruction) onto the stack; it then loads the IP with the destination offset address. A far CALL pushes
the CS onto the stack and loads an intersegment pointer onto the stack; it then pushes the IP onto the
stack and loads the IP with the destination offset address. On return, a subsequent RETN or RETF
reverses these steps.
Source Code. CALL {register/memory}
Object Code. (four formats)
Direct within segment:
Indirect within segment:
Indirect intersegment:
Direct intersegment:
|11101000|disp-low|disp-high|
|11111111|mod010r/m|
|11111111|mod011r/m|
|10011010|offset-low|offset-high|seg-low|seg-high|
RET/RETN/RETF : Return from a Procedure
Operation. Returns from a procedure previously entered by a near or far CALL. The assembler generates
a near RET if it is within a procedure labeled NEAR and a far RET if it is within a procedure labeled FAR.
For near, RET moves the word at the top of the stack to the IP and increments the SP by 2. For far, RET
moves the words at the top of the stack to the IP and CS and increments the SP by 4. Any numeric
operand ( a pop value coded as RET 4) is added to the SP.
RETN and RETF were introduced by MASM 5.0 so that you can code a near or far return
explicitly.
Source Code. RET/RETN/RETF [pop-value]
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 3
________________________________________________________________________
Object Code. (four formats)
Within a segment:
Within a segment with pop value:
Intersegment:
Intersegment with pop value:
|11000011|
|11000010|data-low|data-high|
|11001011|
|11001010|data-low|data-high|
JMP : Unconditional Jump
Operation. Jumps to a designted address under any condition. A JMP address may be short i.e. JMP
SHORT , (-128 to +127 bytes), near i.e. JMP NEAR PTR (within +/- 32K, the default), or far, i.e. JMP FAR
PTR (to another code segment). A short or near JMP replaces the IP with a destination offset address. A
far jump (such as JMP FAR PTR label) replaces the CS:IP with a new segment address.
Source Code. JMP {register/memory}
Object Code. (five formats)
Direct within seg short:
Direct within segment:
Indirect within segment:
Indirect intersegment:
Direct intersegment:
|11101011|--disp--|
|11101001|disp-low|disp-high|
|11111111|mod100r/m|
|11111111|mod101r/m|
|11101010|offset-low|offset-high|seg-low|seg-high|
7.2 Conditional Transfers
Jcondition : Jump on Condition
Operation. This section summarizes the conditional jump instructions that transfer to a stated operand if
the tested flag condition is true. If true, the operation adds the operand offset to the IP and performs the
jump; if not true, processing continues with the next instruction in sequence. For the 8086-80286, the
jump must be short (-128 to 127 bytes). The operations test the flags do not change them. The source
code is Jcondition label. All object codes are of the form |sistnnnn|--disp--|, where disp bits are 0111 for
short jumps.
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 4
________________________________________________________________________
In the first list, the instructions are typically used after a compare operation, which compares the
first operand to the second.
source object
flags
code code
checked
used after comparison
JA
|dist0111|
CF = 0, ZF = 0
Unsigned data, above (higher)
JAE |dist0011|
CF = 0
Unsigned data, above/equal
JB
|dist0010|
CF = 1
Unsigned data, below (lower)
JBE |dist0110|
CF = 1 or ZF = 1
Unsigned data, below/equal
JE
|dist0100|
ZF = 1
Signed/unsigned data, equal
JG
|dist1111|
ZF = 0, SF = OF
Signed data, greater
JGE |dist1101|
SF = OF
Signed data, greater/equal
JL
|dist1100|
SF not = OF
Signed data, lower
JLE |dist1110|
ZF = 1 or SF not = OF Signed data, lower/equal
JNA |dist0110|
CF = 1 or ZF = 1
Unsigned data, not above
JNAE |dist0010|
CF = 1
Unsigned data, not above/equal
JNB |dist0011|
CF = 0
Unsigned data, not below
JNBE |dist0111|
CF = 0, ZF = 0
Unsigned data, not below/equal
JNE |dist0101|
ZF = 0
Signed/unsigned, not equal
JNG |dist1110|
ZF = 1 or SF not = OF Signed data, not greater
JNGE |dist1100|
SF = not = OF
Signed data, not greater/equal
JNL |dist1101|
SF = OF
Signed data, not lower
JNLE |dist1111|
ZF = 0, SF = OF
Signed data, not lower/equal
In the second list, the instructions are typically used after an arithmetic or other operation, which
clears or sets bits according to the result.
source object
flags
code code
checked
used to test
JC
|dist0010|
CF = 1
If CF set (same as JB/JNAE)
JNC |dist0011|
CF = 0
If CF off (same as JAE/JNB)
JNO |dist0001|
OF = 0
If OF off
JNP |dist1011|
PF = 0
If odd parity
JNS |dist1001|
SF = 0
If sign is positive
JNZ |dist0101|
ZF = 0
If signed/unsigned data not zero
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 5
________________________________________________________________________
JO
JP
JPE
JPO
JS
JZ
|dist0000|
|dist1010|
|dist1010|
|dist1011|
|dist1000|
|dist0100|
OF = 1
PF = 1
PF = 1
PF = 0
SF = 1
ZF = 1
If OF set
If even parity
Same as JP
Same as JNP
If sign is negative
If signed/unsigned data is zero
7.3 Iteration Controls
LOOP : Loop Until Complete
Operation. Controls the execution of a routine a specified number of times. The CX should contain a
count before starting the loop. LOOP appears at the end of the loop and decrements the CX by 1. If the
CX is nonzero, LOOP transfers to its operand address (a short jump), which points to the start of the loop
(adds the offset in the IP); otherwise LOOP drops through to the next instruction.
Source Code. LOOP label
Object Code. |11100010|--disp--|
ตัวอย่าง
mov cx,100
begin:
…
…
loop begin
LOOPE/LOOPZ : Loop While Equal or Loop While Zero
Operation. Controls the repetitive execution of a routine. LOOPE and LOOPZ are similar to LOOP, except
that they terminate if the CX is zero or if the ZF is 0 (nonzero condition, set by another instruction).
Source Code. LOOPE label
LOOPZ label
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 6
________________________________________________________________________
Object Code. |11100001|--disp--|
LOOPNE/LOOPNZ : Loop While Not Equal or Loop While Not Zero
Operation. Controls the repetitive execution of a routine. LOOPNE and LOOPNZ are similar to LOOP,
except that they terminate if the CX is zero or the ZF is 1 (zero condition, set by another instruction).
Source Code. LOOPNE label
LOOPNZ label
Object Code. |11100000|--disp--|
7.4 High-Level Logic Structures
The assembly language has no structured IF, ELSE, or WHILE statements. This is not a severe
limitation, as several instructions may be combined to create any logical structure. In fact, you can
optimize logic structure to make them execute much more efficiently than they would in a high-level
language.
 IF Statement
Example 1,
if (op1 = op2) then
<statement1>
<statement2>
endif
The following is a sample of set of instructions to do:
cmp op1,op2
je
next_label
; short jump only
jmp end_if
; o.k. size < 128 bytes
next_label:
<statement1>
<statement2>
end_if:
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 7
________________________________________________________________________
In assembly language, we can eliminate one of the jumps by reversing the comparison for equal
to not equal:
cmp op1,op2
jne
next_label
; short jump only
<statement1>
<statement2>
Note! maximum size 128 bytes
next_label:
Example 2, compound IF with OR operator
if (al > op1)
or (al >= op2)
or (al = op3)
or (al < op4) then
<statements>
endif
The following is a sample of set of instructions to do:
cmp al,op1
jg
L1
cmp al,op2
jge
L1
cmp al,op3
je
L1
cmp al,op4
jl
L1
jmp L2
L1:
<statements>
L2:
Example 3, in general
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 8
________________________________________________________________________
if (logical_expression) then <true_statements>
else <false_statements>
endif
The following is a sample of set of instructions to do:
; compute logical expression and load 0 = false otherwise = true (C convention) to ax
cmp ax,0
jne
true_label
jmp false_label
true_label:
<true_statements>
jmp end_if
false_label:
<false_statements>
end_if:
 WHILE Statements
Example, in general
high-level:
while (logical_expression) do
<statements>
assembly:
…
while_label:
; compute logical expression and load 0 = false otherwise = true (C convention) to ax
cmp ax,0
jne
true_label
jmp end_while
true_label:
<statements>
jmp while_label
end_while:
Computer Science, Prince of Songkhla University
CS344-321 Assembly Language Programming
chapter 7 page 9
________________________________________________________________________
 REPEAT Statements
repeat <statements> until (logical_expression)
repeat and case left as an exercise.
Computer Science, Prince of Songkhla University