• 沒有找到結果。

• LOOPZ and LOOPE

N/A
N/A
Protected

Academic year: 2022

Share "• LOOPZ and LOOPE"

Copied!
33
0
0

加載中.... (立即查看全文)

全文

(1)

Conditional Loop Instructions Conditional Loop Instructions

• LOOPZ and LOOPE

• LOOPNZ and LOOPNE

(2)

LOOPZ and LOOPE LOOPZ and LOOPE

• Syntax:

LOOPE destination LOOPZ destination

• Logic:

• ECX ← ECX – 1

• if ECX > 0 and ZF=1, jump to destination

• Useful when scanning an array for the first element

that does not match a given value.

(3)

LOOPNZ and LOOPNE LOOPNZ and LOOPNE

• LOOPNZ (LOOPNE) is a conditional loop instruction

• Syntax:

LOOPNZ destination LOOPNE destination

• Logic:

• ECX ← ECX – 1;

• if ECX > 0 and ZF=0, jump to destination

• Useful when scanning an array for the first element

that matches a given value.

(4)

LOOPNZ Example LOOPNZ Example

.data

array SWORD -3,-6,-1,-10,10,30,40,4 sentinel SWORD 0

.code

mov esi,OFFSET array mov ecx,LENGTHOF array next:

test WORD PTR [esi],8000h ; test sign bit

pushfd ; push flags on stack

add esi,TYPE array

popfd ; pop flags from stack

The following code finds the first positive value in an array:

(5)

Your turn . . . Your turn . . .

.data

array SWORD 50 DUP(?) sentinel SWORD 0FFFFh .code

mov esi,OFFSET array mov ecx,LENGTHOF array

L1: cmp WORD PTR [esi],0 ; check for zero

(fill in your code here)

Locate the first nonzero value in the array. If none is found, let

ESI point to the sentinel value:

(6)

. . . (solution) . . . (solution)

.data

array SWORD 50 DUP(?) sentinel SWORD 0FFFFh .code

mov esi,OFFSET array mov ecx,LENGTHOF array

L1: cmp WORD PTR [esi],0 ; check for zero

pushfd ; push flags on stack

add esi,TYPE array

popfd ; pop flags from stack

loope next ; continue loop

(7)

Conditional Structures Conditional Structures

• Block-Structured IF Statements

• Compound Expressions with AND

• Compound Expressions with OR

• WHILE Loops

• Table-Driven Selection

(8)

Block

Block - - Structured IF Statements Structured IF Statements

Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example:

mov eax,op1 cmp eax,op2 jne L1

mov X,1 jmp L2 L1: mov X,2 L2:

if( op1 == op2 ) X = 1;

else

X = 2;

(9)

Your turn . . . Your turn . . .

Implement the following pseudocode in assembly language. All values are unsigned:

if( ebx <= ecx ) {

eax = 5;

edx = 6;

}

(10)

Your turn . . . Your turn . . .

Implement the following pseudocode in assembly language. All values are 32-bit signed integers:

if( var1 <= var2 ) var3 = 10;

else {

var3 = 6;

var4 = 7;

}

(11)

Compound Expression with AND [1/3]

Compound Expression with AND [1/3]

• When implementing the logical AND operator, consider that HLLs use short-circuit evaluation

• In the following example, if the first expression is false, the second expression is skipped:

if (al > bl) AND (bl > cl) X = 1;

(12)

Compound Expression with AND [2/3]

Compound Expression with AND [2/3]

cmp al,bl ; first expression...

ja L1 jmp next L1:

cmp bl,cl ; second expression...

ja L2

if (al > bl) AND (bl > cl) X = 1;

This is one possible implementation . . .

(13)

Compound Expression with AND [3/3]

Compound Expression with AND [3/3]

cmp al,bl ; first expression...

jbe next ; quit if false

cmp bl,cl ; second expression...

jbe next ; quit if false

mov X,1 ; both are true

if (al > bl) AND (bl > cl) X = 1;

But the following implementation uses 29% less code by

reversing the first relational operator.

(14)

Your turn . . . Your turn . . .

Implement the following pseudocode in assembly language. All values are unsigned:

if(ebx <= ecx) AND (ecx > edx )

{

eax = 5;

edx = 6;

}

(15)

Compound Expression with OR [1/2]

Compound Expression with OR [1/2]

• When implementing the logical OR operator, consider that HLLs use short-circuit evaluation

• In the following example, if the first expression is true, the second expression is skipped:

if (al > bl) OR (bl > cl) X = 1;

(16)

Compound Expression with OR [2/2]

Compound Expression with OR [2/2]

cmp al,bl ; is AL > BL?

ja L1 ; yes

cmp bl,cl ; no: is BL > CL?

jbe next ; no: skip next statement if (al > bl) OR (bl > cl)

X = 1;

We can use "fall-through" logic to keep the code as short as

possible:

(17)

WHILE Loops WHILE Loops

while( eax < ebx) eax = eax + 1;

A WHILE loop is really an IF statement followed by the body of the loop, followed by an unconditional jump to the top of the loop. Consider the following example:

top: cmp eax,ebx ; check loop condition jae next ; false? exit loop

inc eax ; body of loop

jmp top ; repeat the loop

This is a possible implementation:

(18)

Your turn . . . Your turn . . .

while( ebx <= val1) {

ebx = ebx + 5;

val1 = val1 - 1 }

Implement the following loop, using unsigned 32-bit integers:

(19)

Table

Table - - Driven Selection [1/3] Driven Selection [1/3]

• Create a table containing lookup values and the offsets of labels or procedures

• Use a loop to search the table

(20)

Table

Table - - Driven Selection [2/3] Driven Selection [2/3]

.data

CaseTable BYTE 'A' ; lookup value

DWORD Process_A ; address of procedure EntrySize = ($ - CaseTable)

BYTE 'B'

DWORD Process_B BYTE 'C'

DWORD Process_C BYTE 'D'

Step 1: Create a table containing lookup values and procedure

offsets.

(21)

Table

Table - - Driven Selection [3/3] Driven Selection [3/3]

mov ebx,OFFSET CaseTable ; point EBX to the table mov ecx,NumberOfEntries ; loop counter

L1: cmp al,[ebx] ; match found?

jne L2 ; no: continue

call NEAR PTR [ebx + 1] ; yes: call the procedure

jmp L3 ; and exit the loop

L2: add ebx,EntrySize ; point to next entry

loop L1 ; repeat until ECX = 0

L3:

Step 2: Use a loop to search the table. When a match is found,

we call the procedure offset stored in the current table

entry.

(22)

Application: Finite

Application: Finite - - State Machines State Machines

• A finite-state machine (FSM) is a graph structure that changes state based on some input. Also called a state-transition diagram.

• We use a graph to represent an FSM, with squares or circles called

nodes,

and lines with arrows between the circles called edges (or arcs).

• A FSM is a specific instance of a more general structure called a

directed graph

(or digraph).

• Three basic states, represented by nodes:

• Start state

(23)

Finite

Finite - - State Machine State Machine

• Accepts any sequence of symbols that puts it into an accepting (final) state

• Can be used to recognize, or validate a sequence of characters that is governed by language rules (called a regular expression)

• Advantages:

• Provides visual tracking of program's flow of control

• Easy to modify

• Easily implemented in assembly language

(24)

FSM Examples FSM Examples

• FSM that recognizes strings beginning with 'x', followed by letters 'a'..'y', ending with 'z':

start 'x'

'a'..'y'

'z '

A B

C

• FSM that recognizes signed integers:

digit

(25)

Your turn . . . Your turn . . .

• Explain why the following FSM does not work as well for signed integers as the one shown on the previous slide:

start

digit

A +,- B

digit

(26)

Implementing an FSM Implementing an FSM

StateA:

call Getnext ; read next char into AL cmp al,'+' ; leading + sign?

je StateB ; go to State B

cmp al,'-' ; leading - sign?

je StateB ; go to State B

call IsDigit ; ZF = 1 if AL = digit

jz StateC ; go to State C

call DisplayErrorMsg ; invalid input found

The following is code from State A in the Integer FSM:

(27)

Flowchart of State A

Flowchart of State A

StateA

GetNext

AL = '+' ? true

AL = '-' ? true

ZF = 1 ? true IsDigit

false

false

false

StateB

StateB

StateC

State A accepts a plus or

minus sign, or a decimal

digit.

(28)

Runtime Expressions Runtime Expressions

.IF eax > ebx mov edx,1 .ELSE

mov edx,2 .ENDIF

• .IF, .ELSE, .ELSEIF, and .ENDIF can be used to create block-structured IF statements.

• Examples:

.IF eax > ebx && eax > ecx mov edx,1

.ELSE

mov edx,2 .ENDIF

(29)

Relational and Logical Operators

Relational and Logical Operators

(30)

MASM MASM - - Generated Code Generated Code

mov eax,6 cmp eax,val1 jbe @C0001 mov result,1

@C0001:

.data

val1 DWORD 5 result DWORD ? .code

mov eax,6

.IF eax > val1 mov result,1 .ENDIF

Generated code:

(31)

MASM MASM - - Generated Code Generated Code

mov eax,6 cmp eax,val1 jle @C0001 mov result,1

@C0001:

.data

val1 SDWORD 5 result SDWORD ? .code

mov eax,6

.IF eax > val1 mov result,1 .ENDIF

Generated code:

MASM automatically generates a signed jump (JLE).

(32)

.REPEAT Directive .REPEAT Directive

; Display integers 1 – 10:

mov eax,0 .REPEAT

inc eax

call WriteDec call Crlf

Executes the loop body before testing the loop condition associated with the .UNTIL directive.

Example:

(33)

.WHILE Directive .WHILE Directive

; Display integers 1 – 10:

mov eax,0

.WHILE eax < 10 inc eax

call WriteDec call Crlf

.ENDW

Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop.

Example:

參考文獻

相關文件

了⼀一個方案,用以尋找滿足 Calabi 方程的空 間,這些空間現在通稱為 Calabi-Yau 空間。.

You are given the wavelength and total energy of a light pulse and asked to find the number of photons it

• ‘ content teachers need to support support the learning of those parts of language knowledge that students are missing and that may be preventing them mastering the

If a contributor is actively seeking an appointment in the aided school sector but has not yet obtained an appointment as a regular teacher in a grant/subsidized school, or he

Wang, Solving pseudomonotone variational inequalities and pseudocon- vex optimization problems using the projection neural network, IEEE Transactions on Neural Networks 17

volume suppressed mass: (TeV) 2 /M P ∼ 10 −4 eV → mm range can be experimentally tested for any number of extra dimensions - Light U(1) gauge bosons: no derivative couplings. =&gt;

Define instead the imaginary.. potential, magnetic field, lattice…) Dirac-BdG Hamiltonian:. with small, and matrix

incapable to extract any quantities from QCD, nor to tackle the most interesting physics, namely, the spontaneously chiral symmetry breaking and the color confinement.. 