• 沒有找到結果。

MOV instruction

N/A
N/A
Protected

Share "MOV instruction"

Copied!
139
0
0

(1)

(2)

Data Transfers Instructions

(3)

• Move from source to destination. Syntax:

MOV d ti ti

MOV destination, source

(4)

MOVinstruction

.data

count BYTE 100 wVal WORD 2 .code

mov bl,count V l mov ax,wVal mov count,al

mov al,wVal ; error mov ax count ; error mov ax,count ; error mov eax,count ; error

4

(5)

Exercise . . .

Explain why each of the following MOV statements are invalid:

invalid:

.data

bVal BYTE 100 bVal2 BYTE ? wVal WORD 2 dVal DWORD 5 .code

mov ds,45 ; a.

mov esi,wVal ; b.

i dV l

mov eip,dVal ; c.

(6)

6

(7)

Copy smaller to larger

.data

count WORD 1 count WORD 1 .code

mov ecx, 0,

mov cx, count d

.data

signedVal SWORD -16 ; FFF0h code

.code

mov ecx, 0 ; mov ecx, 0FFFFFFFFh mov cx signedVal

mov cx, signedVal

(8)

Zero extension

When you copy a smaller value into a larger destination, the MOVZX instruction fills (extends) the upper half of ( ) pp the destination with zeros.

1 0 0 0 1 1 1 1 Source 0 1 0 0 0 1 1 1 1 Source

0 movzx r32,r/m8

movzx r32,r/m16 movzx r16,r/m8

1 0 0 0 1 1 1 1 Destination 0 0 0 0 0 0 0 0

movzx r16,r/m8

mov bl,10001111b

movzx ax,bl ; zero-extension

h d b

8

The destination must be a register.

(9)

Sign extension

The MOVSX instruction fills the upper half of the destination with a copy of the source operand's sign bit

with a copy of the source operand s sign bit.

1 0 0 0 1 1 1 1 Source

1 0 0 0 1 1 1 1 Destination 1 1 1 1 1 1 1 1

mov bl,10001111b

movsx ax,bl ; sign extension

(10)

From a smaller location to a larger one

mov bx, 0A69Bh

movzx eax, bx ; EAX=0000A69Bh movzx edx, bl ; EDX=0000009Bh movzx cx, bl ; EAX=009Bh

mov bx, 0A69Bh

movsx eax, bx ; EAX=FFFFA69Bh movsx edx, bl ; EDX=FFFFFF9Bh movsx cx, bl ; EAX=FF9Bh

10

(11)

(12)

12

(13)

XCHGInstruction

XCHG exchanges the values of two operands. At least one operand must be a register. No immediate operands are p g p permitted.

data .data

var1 WORD 1000h var2 WORD 2000h .code

xchg ax,bx ; exchange 16-bit regs xchg ah,al ; exchange 8-bit regs xchg var1,bx ; exchange mem, reg

xchg eax ebx ; exchange 32-bit regs xchg eax,ebx ; exchange 32-bit regs

(14)

Exchange two memory locations

.data

1 1000h

var1 WORD 1000h var2 WORD 2000h .code

mov ax, val1 xchg ax, val2 mov val1, ax

14

(15)

Arithmetic Instructions

(16)

• Flags Affected by Arithmetic

– Zero Sign – Sign – Carry

Overflow – Overflow

16

(17)

• Add 1, subtract 1 from destination operand

– operand may be register or memory

• INC destination

• Logic: destination  destination + 1

• DEC destination

• Logic: destination  destination – 1

(18)

INCand DEC Examples

.data

myWord WORD 1000h myWord WORD 1000h

myDword DWORD 10000000h .code

inc myWord ; 1001h dec myWord ; 1000h

inc myDword ; 10000001h mov ax 00FFh

mov ax,00FFh

inc ax ; AX = 0100h mov ax,00FFh,

inc al ; AX = 0000h

18

(19)

Exercise...

Show the value of the destination operand after each of the following instructions executes:g

.data

myByte BYTE 0FFh, 0 .code

l B t AL FFh

mov al,myByte ; AL = mov ah,[myByte+1] ; AH =

dec ah ; AH =

FFh 00h FFh

dec ah ; AH

inc al ; AL =

dec ax ; AX =

FFh 00h FEFF

(20)

• Same operand rules as for the MOV instructionSame operand rules as for the MOV instruction

20

(21)

data .data

var1 DWORD 10000h var2 DWORD 20000h

.code ; ---EAX---

mov eax,var1 ; 00010000h add eax,var2 ; 00030000h add ax,0FFFFh ; 0003FFFFh add eax 1 ; 00040000h add eax,1 ; 00040000h sub ax,1 ; 0004FFFFh

(22)

NEG(negate) Instruction

Reverses the sign of an operand. Operand can be a register or memory operand

.data

1

register or memory operand.

valB BYTE -1

valW WORD +32767 code

.code

mov al,valB ; AL = -1 neg alg ; AL = +1

neg valW ; valW = -32767

22

(23)

Implementing Arithmetic Expressions

HLL compilers translate mathematical expressions into assembly language. You can do it also. For example:

Rval DWORD ?

y g g p

Rval = -Xval + (Yval – Zval) Rval DWORD ?

Xval DWORD 26 Yval DWORD 30 Zval DWORD 40 .code

mov eax,Xval

neg eax ; EAX = -26 mov ebx Yval

mov ebx,Yval

sub ebx,Zval ; EBX = -10

(24)

Exercise ...

Translate the following expression into assembly language.

Do not permit Xval Yval or Zval to be modified:

Do not permit Xval, Yval, or Zval to be modified:

Rval = Xval - (-Yval + Zval)

mov ebx,Yval

Assume that all values are signed doublewords.

mov ebx,Yval neg ebx

add ebx,Zval mov eax,Xval sub eax,ebx

R l

mov Rval,eax

24

(25)

reflect the o tcome of arithmetic (and bit ise) reflect the outcome of arithmetic (and bitwise) operations

b d h f h d i i d

– based on the contents of the destination operand

• Essential flags:

– Zero flag – destination equals zero – Sign flag – destination is negative

– Carry flag – unsigned value out of range – Overflow flag – signed value out of range

(26)

Zero Flag (ZF)

Whenever the destination operand equals Zero, the Zero flag is set.

mov cx,1

sub cx 1 ; CX = 0 ZF = 1 g

sub cx,1 ; CX = 0, ZF = 1 mov ax,0FFFFh

inc ax c a ; AX = 0, ZF = 1; 0, inc ax ; AX = 1, ZF = 0

A flag is set when it equals 1.

A flag is clear when it equals 0.

26

(27)

Sign Flag (SF)

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

0

negative. The flag is clear when the destination is positive.

mov cx,0

sub cx,1 ; CX = -1, SF = 1 add cx 2 ; CX = 1 SF = 0 add cx,2 ; CX = 1, SF = 0

h fl f h d h h b

The sign flag is a copy of the destination's highest bit:

mov al,0

sub al,1 ; AL=11111111b, SF=1

(28)

28

(29)

Exercise . . .

For each of the following marked entries, show the values of the destination operand and the Sign, Zero,

mov ax 00FFh

p g , ,

and Carry flags:

mov ax,00FFh

add ax,1 ; AX= SF= ZF= CF=

sub ax,1 ; AX= SF= ZF= CF=

0100h 0 0 0 00FFh 0 0 0 add al,1 ; AL= SF= ZF= CF=

mov bh,6Ch

dd bh 95h BH SF ZF CF

00h 0 1 1

01h 0 0 1

add bh,95h ; BH= SF= ZF= CF=

mov al,2

01h 0 0 1 mov al,2

sub al,3 ; AL= SF= ZF= CF=FFh 1 0 1

(30)

Overflow Flag (OF)

The Overflow flag is set when the signed result of an operation is invalid or out of range

operation is invalid or out of range.

; Example 1 mov al +127 mov al,+127

add al,1 ; OF = 1, AL = ??

; Example 2

mov al,7Fh ; OF = 1, AL = 80h add al,1

The two examples are identical at the binary level The two examples are identical at the binary level because 7Fh equals +127. To determine the value of the destination operand, it is often easier to calculate

30

(31)

Overflow flag is only set when . . .

– Two positive operands are added and their sum is negative

negative

– Two negative operands are added and their sum is positive

positive

What will be the values of OF flag?

l 80h mov al,80h

add al,92h ; OF = mov al,-2

(32)

32

(33)

• How the ADD instruction modifies OF and CF:

CF ( t f th MSB) – CF = (carry out of the MSB)

– OF = (carry out of the MSB) XOR (carry into the MSB)

• How the SUB instruction modifies OF and CF:

NEG th d ADD it t th d ti ti – NEG the source and ADD it to the destination – CF = INVERT (carry out of the MSB)

OF ( f h MSB) XOR ( i h MSB) – OF = (carry out of the MSB) XOR (carry into the MSB)

(34)

34

(35)

(36)

Jump and Loop

(37)

• Transfer of control or branch instructions

– unconditional – conditional

(38)

JMPInstruction

• JMP is an unconditional jump to a label that is usually within the same procedure.y p

• Syntax: JMP target

• Logic: EIP  target

• Example:

top:

. .

jmp top

38

(39)

LOOPInstruction

• The LOOP instruction creates a counting loop

• Syntax: LOOP target

• Syntax: LOOP target

• Logic:

ECX ECX 1

• ECX  ECX – 1

• Implementation:

• The assembler calculates the distance, in bytes, , y , between the current location and the offset of the target label. It is called the relative offset.

• The relative offset is added to EIP.

(40)

LOOPExample

The following loop calculates the sum of the integers 5 + 4 + 3 +2 + 1:tege s 5 3 :

00000000 66 B8 0000 mov ax,0

00000004 B9 00000005 5

offset machine code source code 00000004 B9 00000005 mov ecx,5 00000009 66 03 C1 L1:add ax,cx 00000009 66 03 C1 L1:add ax,cx 0000000C E2 FB loop L1

0000000E

When LOOP is assembled, the current location = 0000000E.

Looking at the LOOP machine code, we see that –5 (FBh) g , ( ) is added to the current location, causing a jump to

location 00000009:

00000009  0000000E + FB 40

(41)

Exercise . . .

If the relative offset is encoded in a single byte,

(a) what is the largest possible backward jump?

(b) what is the largest possible forward jump?

(a) 128 ( )

(b) +127

Average sizes of machine instructions are about 3 Average sizes of machine instructions are about 3 bytes, so a loop might contain, on average, a

(42)

Exercise . . .

mov ax,6 What will be the final value of AX? mov ecx,44

L1:

inc ax

10 inc ax

loop L1

How many times will the loop

execute? mov ecx,0

execute? X2:

inc ax loop X2 4,294,967,296

loop X2

42

(43)

Nested Loop

If you need to code a loop within a loop, you must save the outer loop counter's ECX value. In the following example, oute loop cou te s C value. t e ollow g e a ple, the outer loop executes 100 times, and the inner loop 20 times.

.data

count DWORD ? d

.code

mov ecx,100 ; set outer loop count L1:

mov count,ecx ; save outer loop count mov ecx,20 ; set inner loop count L2:

L2:...

loop L2 ; repeat the inner loop

(44)

Summing an Integer Array

The following code calculates the sum of an array of 16 bit integers

.data

16-bit integers.

intarray WORD 100h,200h,300h,400h .code

di OFFSET i t dd

mov ecx,LENGTHOF intarray ; loop counter

mov ax 0 ; zero the sum

mov ax,0 ; zero the sum

L1:

add ax [edi] ; add an integer add ax,[edi] ; add an integer add edi,TYPE intarray ; point to next loop L1 ; repeat until ECX = 0

44

p ; p

(45)

Copying a String

good use of g SIZEOF

The following code copies a string from source to target.

.data

source BYTE "This is the source string",0 target BYTE SIZEOF source DUP(0) 0

target BYTE SIZEOF source DUP(0),0 .code

mov esi,0 ; index register mov ecx,SIZEOF source ; loop counter L1:

L1:

mov al,source[esi] ; get char from source mov target[esi],al ; store in the target inc esi ; move to next char

(46)

Conditional Processing

(47)

Status flags - review

• The Zero flag is set when the result of an operation equals zero.q

• The Carry flag is set when an instruction generates a result that is too large (or too small) for the

destination operand.

• The Sign flag is set if the destination operand is

negative, and it is clear if the destination operand is positive.

Th O fl fl i t h i t ti t

• The Overflow flag is set when an instruction generates an invalid signed result.

Less important:

• Less important:

– The Parity flag is set when an instruction generates an even number

(48)

• Syntax: (no flag affected)

NOT destination NOT destination

• Example:

mov al 11110000b

NOT mov al, 11110000b

not al

0 0 1 1 1 0 1 1 1 1 0 0 0 1 0 0

NOT

inverted

1 1 0 0 0 1 0 0 inverted

48

(49)

• Syntax: (O=0,C=0,SZP)

AND destination, source AND

• Example:

mov al, 00111011b and al, 00001111b

0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1

AND

(50)

• Syntax: (O=0,C=0,SZP)

OR destination, source

OR

• Example:

mov dl, 00111011b or dl, 00001111b

0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1

OR

set unchanged

50

0 0 1 1 1 1 1 1 set

unchanged

(51)

• Syntax: (O=0,C=0,SZP)

XOR destination, source

• Example:

XOR

mov dl, 00111011b

XOR

xor dl, 00001111b

0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1

XOR

(52)

Applications

(1 of 4)

• Task: Convert the character in AL to upper case.

• Solution: Use the AND instruction to clear bit 5.

mov al,'a' ; AL = 01100001b and al,11011111b ; AL = 01000001b

52

(53)

Applications

(2 of 4)

• Task: Convert a binary decimal byte into its equivalent ASCII decimal digit

equivalent ASCII decimal digit.

• Solution: Use the OR instruction to set bits 4 and 5.

mov al,6 ; AL = 00000110b or al,00110000b ; AL = 00110110b, ;

The ASCII digit '6' = 00110110b The ASCII digit 6 = 00110110b

(54)

Applications

(3 of 4)

• Solution: AND the lowest bit with a 1. If the result is Zero, the number was even.

mov ax,wordVal

and ax,1 ; low bit set?, ;

jz EvenValue ; jump if Zero flag set

54

(55)

Applications

(4 of 4)

• Solution: OR the byte with itself, then use the JNZ (jump if not zero) instruction.

or al,al

jnz IsNotZero ; jump if not zero

j ; j p

ORi g b ith it lf d t h g it l

ORing any number with itself does not change its value.

(56)

TESTinstruction

• Performs a nondestructive AND operation between each pair of matching bits in two operands

pair of matching bits in two operands

• No operands are modified, but the flags are affected.

• Example: jump to a label if either bit 0 or bit 1 in AL is

• Example: jump to a label if either bit 0 or bit 1 in AL is set. test al,00000011b

jnz ValueFound jnz ValueFound

• Example: jump to a label if neither bit 0 nor bit 1 in

• Example: jump to a label if neither bit 0 nor bit 1 in AL is set.

test al 00000011b test al,00000011b jz ValueNotFound

56

(57)

CMPinstruction

(1 of 3)

• Compares the destination operand to the source operandp

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

• Syntax: (OSZCAP)

• Syntax: (OSZCAP)

CMP destination, source

• Example: destination == source

• Example: destination == source mov al,5

cmp al,5 ; Zero flag set

• Example: destination < source mov al,4

(58)

CMPinstruction

(2 of 3)

• Example: destination > source mov al,6

cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are clear)

The comparisons shown so far were unsigned The comparisons shown so far were unsigned.

58

(59)

CMPinstruction

(3 of 3)

The comparisons shown here are performed with

• Example: destination > source signed integers.

Example: destination source mov al,5

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

• Example: destination < source

• Example: destination < source mov al,-1

cmp al,5 ; Sign flag != Overflow flag c p a ,5 ; S g ag ! O e o ag

(60)

Conditions

unsigned ZF CF

destination<source 0 1 destination>source 0 0 destination>source 0 0

destination=source 1 0

signed flags

destination<source SF != OF

d ti ti SF OF

destination>source SF == OF destination=source ZF=1

60

(61)

(62)

Conditional jumps

(63)

CMP AL, 0 JZ L1

(64)

64

(65)

(66)

66

(67)

＞≧＜≦

(68)

68

(69)

Examples

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

mov Large,bx cmp ax,bx

t e two to a va able a ed a ge

p

jna Next

mov Large,ax N t

Next:

• Compare signed AX to BX, and copy the smaller of th t i t i bl d S ll

mov Small,ax b

the two into a variable named Small

cmp bx,ax

(70)

Examples

• Find the first even number in an array of unsigned integers

.date

i tA DWORD 7 9 3 4 6 1 integers

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

dd b 4

...

70

...

(71)

• Syntax: BT bitBase, n

– bitBase may be r/m16 or r/m32 – n may be r16, r32, or imm8

• Example: jump to label L1 if bit 9 is set in the Example: jump to label L1 if bit 9 is set in the AX register:

bt AX 9 ; CF = bit 9 bt AX,9 ; CF = bit 9

jc L1 ; jump if Carry

(72)

Conditional loops

(73)

– ECX ECX – 1

(74)

• Logic:

– ECX  ECX – 1; ECX  ECX 1;

74

(75)

LOOPNZ example

data

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

.data

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

d .code

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

test WORD PTR [esi],8000h ; test sign bit pushfd ; push flags on stack pushfd ; push flags on stack add esi,TYPE array

popfd ; pop flags from stack

l t ti l

loopnz next ; continue loop

jnz quit ; none found

(76)

Exercise ...

Locate the first nonzero value in the array. If none is found, let ESI point to the sentinel value:

.data

array SWORD 50 DUP(?)

, p

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

76

quit:

(77)

Solution

.data

0

array SWORD 50 DUP(?) sentinel SWORD 0FFFFh

code .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 L1 ; continue loop

loope L1 ; continue loop

jz quit ; none found

(78)

Conditional structures

(79)

If statements

if then elseC T E

JNE else

JMP endif else:

(80)

Block-structured IF statements

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

mov eax,op1 cmp eax,op2 if( op1 == op2 )

X = 1;

jne L1 mov X,1 j L2 X 1;

else

X = 2;

jmp L2 L1: mov X,2 L2:

;

L2:

80

(81)

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

cmp ebx ecx cmp ebx,ecx ja next

mov eax,5 if( ebx <= ecx )

{

5 ,

mov edx,6 next:

eax = 5;

edx = 6;

} }

(82)

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

mov eax var1 if( var1 <= var2 ) mov eax,var1 cmp eax,var2 jle L1

if( var1 <= var2 ) var3 = 10;

else j

mov var3,6 mov var4,7 else

{

var3 = 6;

jmp L2

L1: mov var3,10 L2:

var3 6;

var4 = 7;

} L2:

}

82

(83)

Compound expression with AND

• 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;

(84)

Compound expression with AND

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

This is one possible implementation . . .

cmp al,bl ; first expression...

ja L1

p p

j

jmp next L1:

cmp bl,cl ; second expression...

ja L2 jmp next jmp next

L2: ; both are true

mov X,1 ; set X to 1

84

next:

(85)

Compound expression with AND

if (al > bl) AND (bl > cl)

But the following implementation uses 29% less code X = 1;

by reversing the first relational operator. We allow the program to "fall through" to the second expression:

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 mov X,1 ; both are true next:

(86)

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

cmp ebx ecx cmp ebx,ecx ja next

cmp ecx,edx if( ebx <= ecx

&& ecx > edx )

{ p ,

jbe next mov eax,5 {

eax = 5;

edx = 6; mov edx,6

next:

edx = 6;

}

(There are multiple correct solutions to this problem )

86

(There are multiple correct solutions to this problem.)

(87)

Compound Expression with OR

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

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

(88)

Compound Expression with OR

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

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

cmp al,bl ; is AL > BL?

ja L1 ; yes

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

jbe next ; no: skip next statement

L1 X 1 t X t 1

L1:mov X,1 ; set X to 1 next:

88

(89)

WHILE Loops

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

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

_while:

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

inc eax ; body of loop

jmp _while ; repeat the loop

(90)

Exercise . . .

while( ebx <= val1)

Implement the following loop, using unsigned 32-bit integers: while( ebx <= val1)

{

ebx = ebx + 5;

g

; val1 = val1 - 1 }

_while:

cmp ebx,val1 ; check loop condition cmp ebx,val1 ; check loop condition ja _endwhile ; false? exit loop

add ebx,5 ; body of loop dec val1

jmp while ; repeat the loop d hil

90

_endwhile:

(91)

Example: IF statement nested in a loop

while(eax < ebx) {

_while: cmp eax, ebx

j d hil

{

eax++;

if (ebx==ecx)

jae _endwhile inc eax

cmp ebx, ecx X=2;

else X=3;

jne _else mov X, 2 jmp while X=3;

}

jmp _while _else: mov X, 3

jmp _while _endwhile:

(92)

92

(93)

Table-driven selection

Step 1: create a table containing lookup values and procedure offsets:

.data

CaseTable BYTE 'A' ; lookup value

procedure offsets:

CaseTable BYTE 'A' ; lookup value

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

BYTE 'B' BYTE 'B'

DWORD Process_B BYTE 'C'

DWORD Process_C BYTE 'D'

DWORD Process D_

(94)

Table-driven selection

Step 2: Use a loop to search the table. When a match is found, we call the procedure offset stored in the current

mov ebx,OFFSET CaseTable ; point EBX to the table

, p

table entry:

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

j p ; p

L2:add ebx,EntrySize ; point to next entry

loop L1 ; repeat until ECX = 0

L3:

required for procedure

94

required for procedure pointers

(95)

Shift and rotate

(96)

96

(97)

0

CF

0

(98)

CF

0

CF

• Operand types:

SHL destination,count SHL reg,imm8

SHL mem,imm8 SHL reg,CL SHL mem,CL

98

(99)

Fast multiplication

Shifting left 1 bit multiplies a number by 2 mov dl,5

shl dl,1

0 0 0 0 1 0 1 0

0 0 0 0 0 1 0 1 = 5

= 10

Before:

After:

, After: 0 0 0 0 1 0 1 0 = 10

Shifting left n bits multiplies the operand by 2n For example 5 * 22 20

mov dl,5

hl dl 2 20

For example, 5 * 22 = 20

shl dl,2 ; DL = 20

(100)

CF

0

CF

n

mov dl,80

Shifting right n bits divides the operand by 2

shr dl,1 ; DL = 40

shr dl,2 ; DL = 10

100

(101)

CF

• An arithmetic shift preserves the number s sign.

mov dl,-80

dl 1 40

sar dl,1 ; DL = -40

(102)

• No bits are lost

CF

mov al,11110000b

rol al,1 ; AL = 11100001b mov dl,3Fh

rol dl 4 ; DL = F3h

102

rol dl,4 ; DL = F3h

(103)

• No bits are lost

CF

mov al,11110000b

ror al,1 ; AL = 01111000b

(104)

• Copies the most significant bit to the Carry flag

CF

clc ; CF = 0

mov bl,88h, ; CF,BL = 0 10001000b; , rcl bl,1 ; CF,BL = 1 00010000b rcl bl,1 ; CF,BL = 0 00100001b

104

(105)

• Copies the least significant bit to the Carry flag

CF

stc ; CF = 1

mov ah,10h ; CF,AH = 00010000 1

(106)

• Syntax: (shift left double)

SHLD d ti ti t

SHLD destination, source, count

106

(107)

SHLD example

Shift wval 4 bits to the left and replace its lowest 4 bits with the high 4 bits of AX:

bits with the high 4 bits of AX:

.data

wval WORD 9BA6h

d 9BA6 AC36

wval AX

Before:

.code

mov ax,0AC36h shld wval ax 4

BA6A AC36

After:

shld wval,ax,4

(108)

• Syntax:

SHRD d ti ti t

SHRD destination, source, count

108

(109)

SHRD example

Shift AX 4 bits to the right and replace its highest 4 bi i h h l 4 bi f DX

bits with the low 4 bits of DX:

DX AX

mov ax,234Bh mov dx,7654h

Before: 7654 234B

DX AX

,

shrd ax,dx,4 After: 7654 4234

(110)

110

(111)

bit to the right:

shr array[esi + 8],1 ; high dword

rcr array[esi + 4],1 ; middle dword, rcr array[esi],1 ; low dword,

(112)

• Factor any binary number into powers of 2.

– For example, to multiply EAX * 36, factor 36 into 32 4 d th di t ib ti t f

+ 4 and use the distributive property of multiplication to carry out the operation:

EAX * 36

= EAX * (32 + 4)

= (EAX * 32)+(EAX * 4)

mov eax,123 mov ebx,eax shl eax 5

= (EAX * 32)+(EAX * 4) shl eax,5 shl ebx,2 add eax,ebx

112

(113)

times.

mov ecx,32,

mov esi,offset buffer L1: shl eax,1

mov BYTE PTR [esi],'0' jnc L2

mov BYTE PTR [esi] '1' mov BYTE PTR [esi], 1 L2: inc esi

(114)

(relative to 1980), month, and day into 16 bits:

DH DL

DH DL

0 1 0

0

0 0 1 1 0 1 1 0 1 0 1 0

Year Month Day

9-15 5-8 0-4

Field:

Bit numbers: 9 15 5 8 0 4

Bit numbers:

114

(115)

Isolating a bit string

mov al,dl ; make a copy of DL and al 00011111b ; clear bits 5-7

and al,00011111b ; clear bits 5-7

mov day,al ; save in day variable mov ax,dx ; make a copy of DX

shr ax,5 ; shift right 5 bits and al 00001111b ; clear bits 4 7

and al,00001111b ; clear bits 4-7

mov month,al ; save in month variable mov al,dh ; make a copy of DX

shr al,1 ; shift right 1 bit mov ah,0 ; clear AH to 0

(116)

Multiplication and division

(117)

MUL r/m32

Implied operands:

(118)

MUL examples

100h * 2000h, using 16-bit operands:

.data

val1 WORD 2000h al2 WORD 100h

The Carry flag indicates whether or not the upper half of the product

val2 WORD 100h .code

mov ax,val1

half of the product

contains significant digits.

mov ax,val1

mul val2 ; DX:AX=00200000h, CF=1

mov eax,12345h

12345h * 1000h, using 32-bit operands:

mov eax,12345h mov ebx,1000h

mul ebx ; EDX:EAX=0000000012345000h, CF=0

118

(119)

• Preserves the sign of the product by sign- extending it into the upper half of the g pp destination register

Example: multiply 48 * 4, using 8-bit operands:p p y , g p mov al,48

mov bl,4,

imul bl ; AX = 00C0h, OF=1

(120)

• Instruction formats:

DIV r/m8 DIV r/m16

Default Operands:

DIV r/m32

120

(121)

DIV examples

Divide 8003h by 100h, using 16-bit operands:

mov dx,0 ; clear dividend, high mov ax,8003h ; dividend, low

mov cx,100h ; divisor

div cx ; AX = 0080h, DX = 3 Same division, using 32-bit operands:

mov edx,0 ; clear dividend, high mov eax,8003h ; dividend, low

mov ecx,100h ; divisor

(122)

division takes place

– fill high byte/word/doubleword with a copy of the low byte/word/doubleword's sign bity g

• For example, the high byte contains a copy of the sign bit from the low byte: g y

1 0 0 0 1 1 1 1

1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1

122

(123)

provide important sign-extension operations:

– CBW (convert byte to word) extends AL into AH

– CWD (convert word to doubleword) extends AX into DX CDQ (convert doubleword to quadword) extends EAX – CDQ (convert doubleword to quadword) extends EAX

into EDX

• For example: o e a ple:

mov eax,0FFFFFF9Bh ; -101 (32 bits) cdq ; EDX:EAX = FFFFFFFFFFFFFF9Bh

101 (64 bi )

; -101 (64 bits)

(124)

• Uses same operands as DIV

Example: 8-bit division of –48 by 5 mov al -48

mov al,-48

cbw ; extend AL into AH mov bl,5,

idiv bl ; AL = -9, AH = -3

124

(125)

IDIV examples

Example: 16-bit division of –48 by 5

mov ax,-48

cwd ; extend AX into DX mov bx,5

idiv bx ; AX = -9, DX = -3

Example: 32-bit division of –48 by 5

mov eax,-48

cdq ; extend EAX into EDX b 5

mov ebx,5

(126)

too large to fit into the destination.

mov ax, 1000h bl 10h mov bl, 10h div bl

126

(127)

Arithmetic expressions

(128)

expressions:

– Learn how compilers do it

T t d t di f MUL IMUL DIV d IDIV – Test your understanding of MUL, IMUL, DIV, and IDIV – Check for 32-bit overflow

Example: var4 = (var1 + var2) * var3 mov eax,var1,

jo TooBig ; check for overflow mov var4,eax ; save product

128

(129)

Implementing arithmetic expressions

Example: eax = (-var1 * var2) + var3 mov eax var1

mov eax,var1 neg eax

mul var2

jo TooBig ; check for overflow add eax,var3

Example: var4 = (var1 * 5) / (var2 – 3) mov eax,var1 o ea , a ; left side; e t s de

mov ebx,5

mul ebx ; EDX:EAX = product mov ebx,var2 ; right side

(130)

Implementing arithmetic expressions

Example: var4 = (var1 * -5) / (-var2 % var3);

mov eax,var2 ; begin right side neg eax

cdq ; sign-extend dividend idiv var3 ; EDX = remainder

mov ebx edx ; EBX = right side mov ebx,edx ; EBX = right side mov eax,-5 ; begin left side

imul var1 u a ; EDX:EAX = left side; : e t s de idiv ebx ; final division

mov var4,eax ; quotient

Sometimes it's easiest to calculate the right-hand term of an expression first

130

expression first.

(131)

Exercise . . .

Implement the following expression using signed 32-bit integers:

eax = (ebx * 20) / ecx

mov eax,20 mul ebx

div ecx

(132)

Exercise . . .

Implement the following expression using signed 32-bit integers. Save and restore ECX and EDX:

eax = (ecx * edx) / eax

push ecx h d push edx

push eax ; EAX needed later mov eax,ecx

mov eax,ecx

mul edx ; left side: EDX:EAX pop ecx ; saved value of EAX div ecx ; EAX = quotient

pop edx ; restore EDX, ECX

132

pop ecx

(133)

Exercise . . .

Implement the following expression using signed 32-bit integers Do not modify any variables other than var3:

integers. Do not modify any variables other than var3:

var3 = (var1 * -var2) / (var3 – ebx) mov eax,var1

mov edx var2 mov edx,var2 neg edx

mul edx ; left side: edx:eax mov ecx,var3

sub ecx,ebx

di i

div ecx ; eax = quotient

(134)

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.

Fetch operands (OF): (memory operand needed) read value from memory E t th i t ti (IE). Execute the

For 5 to be the precise limit of f(x) as x approaches 3, we must not only be able to bring the difference between f(x) and 5 below each of these three numbers; we must be able

[This function is named after the electrical engineer Oliver Heaviside (1850–1925) and can be used to describe an electric current that is switched on at time t = 0.] Its graph

• The DAS (decimal adjust after subtraction) instruction converts the binary result of a SUB or SBB operation to packed decimal format. • The value must be

• Last data pointer stores the memory address of the operand for the last non control instruction the operand for the last non-control instruction.. Last instruction pointer

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

– 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

• Last data pointer stores the memory address of the operand for the last non-control instruction. Last instruction pointer stored the address of the last

– 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

bgez Branch on greater than or equal to zero bltzal Branch on less than zero and link. bgezal Branch on greter than or equal to zero

• The  ArrayList class is an example of a  collection class. • Starting with version 5.0, Java has added a  new kind of for loop called a for each

a single instruction.. Thus, the operand can be modified before it can be modified before it is used. Useful for fast multipliation and dealing p g with lists, table and other

Thus, our future instruction may take advantages from both the Web-based instruction and the traditional way.. Keywords：Traditional Instruction、Web-based Instruction、Teaching media

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

We explicitly saw the dimensional reason for the occurrence of the magnetic catalysis on the basis of the scaling argument. However, the precise form of gap depends

Miroslav Fiedler, Praha, Algebraic connectivity of graphs, Czechoslovak Mathematical Journal 23 (98) 1973,

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

• The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with

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

The min-max and the max-min k-split problem are defined similarly except that the objectives are to minimize the maximum subgraph, and to maximize the minimum subgraph respectively..

Experiment a little with the Hello program. It will say that it has no clue what you mean by ouch. The exact wording of the error message is dependent on the compiler, but it might