• 沒有找到結果。

# • Midterm exam: Room 103, 10:00am-12:00am next Thursday, open book, chapters 1-5.

N/A
N/A
Protected

Share "• Midterm exam: Room 103, 10:00am-12:00am next Thursday, open book, chapters 1-5."

Copied!
66
0
0

(1)

## Conditional Processing

### 2005/11/03

with slides by Kip Irvine

(2)

(3)

(4)

(5)

### • Less important:

– The Parity flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand.

– The Auxiliary Carry flag is set when an operation produces a carry out from bit 3 to bit 4

(6)

NOT

inverted

(7)

### 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 1

AND

unchanged cleared

(8)

OR

set unchanged

(9)

### 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 1 0 0

XOR

inverted unchanged

(10)

### Applications (1 of 5)

mov al,'a' ; AL = 01100001b

and al,11011111b ; AL = 01000001b

(11)

### Applications(2 of 5)

mov al,6 ; AL = 00000110b

or al,00110000b ; AL = 00110110b

(12)

(13)

(14)

(15)

(16)

### • Compares the destination operand to the source operand

– Nondestructive subtraction of source from destination (destination operand is not changed)

(17)

### cmp al,5; ZF = 0, CF = 0

(both the Zero and Carry flags are clear)

(18)

### • Example: destination > source

mov al,5

cmp al,-2 ; Sign flag == Overflow flag

### The comparisons shown here are performed with signed integers.

• Example: destination < source mov al,-1

cmp al,5 ; Sign flag != Overflow flag

(19)

(20)

## Conditional jumps

(21)

(22)

(23)

(24)

(25)

(26)

(27)

### Examples

mov Large,bx cmp ax,bx jna Next

mov Large,ax Next:

### • Compare unsigned AX to BX, and copy the larger of the two into a variable named Large

mov Small,ax cmp bx,ax jnl Next

mov Small,bx Next:

(28)

### Examples

.date

intArray DWORD 7,9,3,4,6,1 .code

...

mov ebx, OFFSET intArray mov ecx, LENGTHOF intArray L1: test DWORD PTR [ebx], 1

jz found add ebx, 4 loop L1

...

(29)

(30)

### Encrypting a string

KEY = 239 .data

buffer BYTE BUFMAX DUP(0) bufSize DWORD ?

.code

mov ecx,bufSize ; loop counter

mov esi,0 ; index 0 in buffer L1:

xor buffer[esi],KEY ; translate a byte

inc esi ; point to next byte

loop L1

(31)

## Conditional loops

(32)

(33)

(34)

### 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

popfd ; pop flags from stack

loopnz next ; continue loop

jnz quit ; none found

sub esi,TYPE array ; ESI points to value quit:

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

(35)

.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

quit:

(36)

### 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

popfd ; pop flags from stack

loope next ; continue loop

jz quit ; none found

sub esi,TYPE array ; ESI points to value quit:

(37)

## Conditional structures

(38)

(39)

(40)

(41)

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

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

(42)

### Compound expression with AND

cmp al,bl ; first expression...

ja L1 jmp next L1:

cmp bl,cl ; second expression...

ja L2 jmp next

L2: ; both are true

mov X,1 ; set X to 1

next:

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

(43)

### Compound expression with AND

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

next:

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

(44)

(45)

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

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

(46)

### Compound Expression with OR

cmp al,bl ; is AL > BL?

ja L1 ; yes

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

jbe next ; no: skip next statement

L1: mov X,1 ; set X to 1

next:

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

(47)

### 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:

_while:

cmp eax,ebx ; check loop condition jae _endwhile ; false? exit loop

inc eax ; body of loop

jmp _while ; repeat the loop _endwhile:

(48)

(49)

(50)

(51)

### Table-driven selection

.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'

DWORD Process_D

NumberOfEntries = (\$ - CaseTable) / EntrySize

(52)

### mov ebx,OFFSET CaseTable

; point EBX to the table

; loop counter

; match found?

; no: continue

### call NEAR PTR [ebx + 1]

; yes: call the procedure

### jmp L3

; and exit the loop

; point to next entry

### loop L1

; repeat until ECX = 0

### 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:

required for procedure pointers

(53)

(54)

(55)

start 'x'

'a'..'y'

'z '

A B

C

start

digit

+,- digit

digit

A B

C

(56)

(57)

start

digit

+,- digit

digit

A B

C

(58)

(59)

start

digit

+,- digit

digit

A B

C

(60)

start

digit

+,- digit

digit

A B

C

(61)

(62)

### High-level directives

.IF eax > ebx mov edx,1 .ELSE

mov edx,2 .ENDIF

### • MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump instructions.

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

.ELSE

mov edx,2 .ENDIF

(63)

(64)

### MASM-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

(65)

### .REPEAT directive

; Display integers 1 – 10:

mov eax,0 .REPEAT

inc eax

call WriteDec call Crlf

.UNTIL eax == 10

(66)

### .WHILE directive

; Display integers 1 – 10:

mov eax,0

.WHILE eax < 10 inc eax

call WriteDec call Crlf

.ENDW

### Example:

In order to encourage the reinforcement learning agent to discover positive symptoms more quickly, a simple heuristic is to provide the agent with an auxiliary piece of reward when

Animal or vegetable fats and oils and their fractiors, boiled, oxidised, dehydrated, sulphurised, blown, polymerised by heat in vacuum or in inert gas or otherwise chemically

Milk and cream, in powder, granule or other solid form, of a fat content, by weight, exceeding 1.5%, not containing added sugar or other sweetening matter.

When we want to extend an operation from functions to distributions — e.g., when we want to define the Fourier transform of a distribution, or the reverse of distribution, or the

(c) Draw the graph of as a function of and draw the secant lines whose slopes are the average velocities in part (a) and the tangent line whose slope is the instantaneous velocity

A random variable is called continuous if it can takes on an uncountably infinite number of possible values.. The percentage of exam complete after

• The SBB (subtract with borrow) instruction subtracts both a source operand and the value of the Carry flag both a source operand and the value of the Carry flag from a

The Sign flag is set when the destination operand is negative The flag is clear when the destination

– Zero flag – destination equals zero – Sign flag – destination is negative – Carry flag – unsigned value out of range – Overflow flag – signed value out of range. • The

When we know that a relation R is a partial order on a set A, we can eliminate the loops at the vertices of its digraph .Since R is also transitive , having the edges (1, 2) and (2,

Robinson Crusoe is an Englishman from the 1) t_______ of York in the seventeenth century, the youngest son of a merchant of German origin. This trip is financially successful,

(a) A special school for children with hearing impairment may appoint 1 additional non-graduate resource teacher in its primary section to provide remedial teaching support to

Courtesy: Ned Wright’s Cosmology Page Burles, Nolette &amp; Turner, 1999?. Total Mass Density

The packed comparison instructions compare the destination (second) operand to the source (ﬁrst) oper- and to test for equality or greater than.. These instructions compare eight

Microphone and 600 ohm line conduits shall be mechanically and electrically connected to receptacle boxes and electrically grounded to the audio system ground point.. Lines in

It is interesting that almost every numbers share a same value in terms of the geometric mean of the coefficients of the continued fraction expansion, and that K 0 itself is