Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions.

Similar presentations


Presentation on theme: "Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions."— Presentation transcript:

1 Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions

2 Data Transfer Instructions MOV is for moving data between:  Memory  Register  Immediate (constant) Almost all combinations, except:  Memory to Memory!

3 MOV Instruction.data count BYTE 100 wVal WORD 2.code mov bl,count mov ax,wVal mov count,al mov al,wVal; error mov ax,count; error mov eax,count; error Move from source to destination. Syntax: MOV destination,source No more than one memory operand permitted CS, EIP, and IP cannot be the destination No immediate to segment moves

4 Your turn....data bVal BYTE 100 bVal2 BYTE ? wVal WORD 2 dVal DWORD 5.code mov ds,45; a. mov esi,wVal; b. mov eip,dVal; c. mov 25,bVal; d. mov bVal2,bVal; e. Explain why each of the following MOV statements are invalid:

5 Memory to Memory? Must go through a register ….data Var1 WORD? Var2 WORD ?.code MOV AX, var1 MOV var2, AX

6 MOV Instruction Format

7 Instruction Operand Notation

8 Zero or Sign Extension What happens to ECX if – 1 is moved from CX?  Are the higher 16 bits of ECX all 0?  What number does ECX represent now? The solution: MOVZX and MOVSX  MOVZX always fills higher bits with 0.  MOVSX fills higher bits by “ sign extension ”.

9 Zero Extension mov bl,10001111b movzx ax,bl ; zero-extension When you copy a smaller value into a larger destination, the MOVZX instruction fills (extends) the upper half of the destination with zeros. The destination must be a register.

10 MOVZX Instruction Format

11 Sign Extension mov bl,10001111b movsx ax,bl ; sign extension The MOVSX instruction fills the upper half of the destination with a copy of the source operand's sign bit. The destination must be a register.

12 MOVSX Instruction Format

13 XCHG XCHG for exchange data between:  Register, register  Register, memory  Memory, register (again, no memory to memory)

14 Direct-Offset Operands Adding a displacement (or offset) to a variable name: arrayB BYTE 10h, 20h, 30, 40h, 50h … MOV AL, arrayB; AL=10h MOV AL, [arrayB+1]; AL=20h MOV AL, arrayB+1; Is it valid?

15 Your turn... Write a program that rearranges the values of three doubleword values in the following array as: 3, 1, 2..data arrayD DWORD 1,2,3 Step 2: Exchange EAX with the third array value and copy the value in EAX to the first array position. Step1: copy the first value into EAX and exchange it with the value in the second position. mov eax,arrayD xchg eax,[arrayD+4] xchg eax,[arrayD+8] mov arrayD,eax

16 Evaluate this.data myBytes BYTE 80h,66h,0A5h How about the following code. Is anything missing? movzx ax,myBytes mov bl,[myBytes+1] add ax,bx mov bl,[myBytes+2] add ax,bx; AX = sum Yes: Move zero to BX before the MOVZX instruction.

17 Addition and Subtraction ADD X, Y X := X + Y SUB X, Y X := X – Y

18 INC, DEC, NEG INC X X := X + 1 or X++ DEC X X := X – 1 or X-- NEG X X := –X

19 Expression Example: X=(A + B) * (D – E) MOVEAX, A ADDEAX, B MOVECX, D SUBECX, E IMULEAX, ECX MOVX, EAX

20 Flags Affected Flags (register) tell us whether any of the following conditions occur:  Overflow,  Carry,  Zero, Sign … etc. Used for decision in branch.  Loop (discussed next)  If … then … else

21 Zero and Sign Zero Flag ZF=1 if the instruction produce 0. MOV CX, 1 SUB CX, 1; CX=0, ZF=1 Sign Flag SF=1 if the instruction produce a negative number. MOV CX, 0 SUB CX, 1; CX=-1, SF=1 ADD CX, 2; CX=1, SF=0

22 Carry (Unsigned Arithmetic) The Carry flag is set when the result of an operation generates an unsigned value that is out of range (too big or too small for the destination operand). Example: MOV AL, 0FFh ADD AL, 1; CF = 1, AL=00 MOVAX, 00FFh ADDAX, 1; CF = 0, AX=0100h

23 Overflow (Signed Arithmetic) The Overflow flag is set when the signed result of an operation is invalid or out of range. Example: MOV AL, +127 ADD AL, 1; OF = 1 MOVAL, -128 SUBAL, 1; OF = 1

24 Detecting Carry Detecting Carry is easy.  Adding two N-bit numbers result in an (N+1)-bit number. Example: 0 0 0 0 0 1 0 0 +1 1 1 1 1 1 1 1 10 0 0 0 0 0 1 1 CF is ignored for signed arithmetic. For example, the above is 4 + (-1) in decimal

25 Detecting Overflow Carry isn ’ t meaningful for signed arithmetic. For example, adding any two negative numbers always produces carry. Detecting Overflow:  Compare CF and the bit carried into MSB (Most Significant Bit).

26 Overflow in Positive Numbers Carry never happens. Overflow occurs if MSB becomes 1 01111111 (127) 00000001 (1) + 01111111 (127) 00000001 (1) Observation:  MSB=1 indicates a negative number.  But, we ’ re adding two positive numbers … ?!

27 Overflow in Negative Numbers Carry always happens. Overflow occurs if MSB becomes 0 10000000 (-128) 11111111 (-1) + 11111111 (-1) 11111111 (-1) Observation:  MSB=0 indicates a positive number.  But, we ’ re adding two negative numbers … ?!

28 Detecting Overflow Overflow: CF  MSB ?  Doesn ’ t work if adding a positive number to a negative number (or vice versa)! Overflow: (CF  MSB) and not the case of (positive+negavive) positive+negavive:  Overflow never happens.  Carry happens when carry-in to MSB Overflow: CF  (carry-in to MSB)

29 Flags Affect in ADD, SUB Carry: unsigned arithmetic out of range Overflow: signed arithmetic out of range Sign: result is negative Zero: result is zero Auxiliary Carry: carry from bit 3 to bit 4 Parity: sum of 1 bits is an even number

30 LAHF/SAHF LAHF: load the low byte of EFLAGS register into AH. SAHF: store the low byte of EFLAGS register into AH.

31 Data Related Operators Who are they?  OFFSET, PTR, TYPE, LENGTHOF, SIZEOF They are only understood by the assembler. They are not instructions!

32 Operand Sizes Operands may have the size of 1 byte, 2 bytes, or 4 bytes. Most of time, we can tell the size from the register names or the variable definition. For examples: Var1BYTE “Hello” MOV ECX, 13 MOV AL, Var1

33 PTR But sometimes we want to override the default. myDoubleDWORD12345678h MOV AL, myDouble ; error MOV AL, BYTE PTR myDouble MOV AX, WORD PTR myDouble MOV AX, WORD PTR [myDouble+2] MOV EAX, myDouble

34 OFFSET OFFSET returns the distance in bytes, of a label from the beginning of its enclosing segment Assume that the data segment begins at 00404000h:.data bVal BYTE ? wVal WORD ? dVal DWORD ? dVal2 DWORD ?.code mov esi,OFFSET bVal ; ESI = 00404000 mov esi,OFFSET wVal ; ESI = 00404001 mov esi,OFFSET dVal ; ESI = 00404003 mov esi,OFFSET dVal2 ; ESI = 00404007

35 TYPE TYPE returns the size (in bytes) of each element..data var1 BYTE ? var2 WORD ? var3 DWORD ? var4 QWORD ?.code mov eax,TYPE var1; 1 mov eax,TYPE var2; 2 mov eax,TYPE var3; 4 mov eax,TYPE var4; 8

36 LENGTHOF LENGTHOF returns the number of elements..data byte1 BYTE 10,20,30; 3 array1 WORD 30 DUP(?),0,0; 32 array2 WORD 5 DUP(3 DUP(?)); 15 array3 DWORD 1,2,3,4; 4 digitStr BYTE "12345678",0; 9.code mov ecx,LENGTHOF array1; 32

37 SIZEOF SIZEOF returns the size of the variable (the whole array).  SIZEOF = LENGTHOF * TYPE.dataSIZEOF byte1 BYTE 10,20,30; 3 array1 WORD 30 DUP(?),0,0; 64 array2 WORD 5 DUP(3 DUP(?)); 30 array3 DWORD 1,2,3,4; 16 digitStr BYTE "12345678",0; 9.code mov ecx,SIZEOF array1; 64

38 Indirect Operands An indirect operand holds the address of a variable, usually an array or string. It can be dereferenced (just like a pointer)..data val1 BYTE 10h,20h,30h.code mov esi,OFFSET val1 mov al,[esi]; dereference ESI (AL = 10h) inc esi mov al,[esi]; AL = 20h inc esi mov al,[esi]; AL = 30h

39 Array Sum Example.data arrayW WORD 1000h,2000h,3000h.code mov esi,OFFSET arrayW mov ax,[esi] add esi,2; or: add esi,TYPE arrayW add ax,[esi] add esi,2; increment ESI by 2 add ax,[esi]; AX = sum of the array

40 Indexed Operands arrayW WORD 1000h,2000h,3000h.code mov esi,0 mov ax,[arrayW + esi] ; AX = 1000h mov ax,arrayW[esi]; alternate format add esi,2 add ax,[arrayW + esi]

41 Pointers.data arrayW WORD 1000h,2000h,3000h ptrW DWORD arrayW.code mov esi,ptrW mov ax,[esi]; AX = 1000h

42 Implementation of Loops JMP instruction: Unconditional Branch. LOOP instruction:  Step 1: Set ECX to n for a loop of n iterations.  Step 2: Use LOOP instruction at the end of loop.  Hidden action: DEC ECX

43 Example: Summation For I := 10 downto 1 {Sum := Sum+I} MOVECX, 10 MOVEAX, 0 L1:ADDEAX, ECX LOOP L1

44 Your turn What will be the final value of AX? mov ax,6 mov ecx,4 L1: inc ax loop L1 How many times will the loop execute? mov ecx,0 X2: inc ax loop X2 10 4,294,967,296 (=2 32 )

45 Copying a String.data source BYTE "This is the source string",0 target BYTE SIZEOF source DUP(0),0.code mov esi,0; index register mov ecx,SIZEOF source; loop counter L1: mov al,source[esi]; get char from source mov target[esi],al; store it in the target inc esi; move to next character loop L1; repeat for entire string

46 Nested Loop.data count DWORD ?.code mov ecx,100; set outer loop count L1: mov count,ecx; save outer loop count mov ecx,20; set inner loop count L2:.. loop L2; repeat the inner loop mov ecx,count; restore outer loop count loop L1; repeat the outer loop


Download ppt "Outline Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions."

Similar presentations


Ads by Google