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

Outline

  • Upload
    forest

  • View
    20

  • Download
    0

Embed Size (px)

DESCRIPTION

Outline. Data Transfer Instructions Arithmetic Instructions Data-Related Operations and Directives Indirect Addressing JMP and LOOP Instructions. Data Transfer Instructions. MOV is for moving data between: Memory Register Immediate (constant) Almost all combinations, except: - PowerPoint PPT Presentation

Citation preview

Page 1: Outline

Outline

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

Page 2: Outline

Data Transfer Instructions

MOV is for moving data between: Memory Register Immediate (constant)

Almost all combinations, except: Memory to Memory!

Page 3: Outline

MOV Instruction

.datacount BYTE 100wVal WORD 2.code

mov bl,countmov ax,wValmov count,al

mov al,wVal ; errormov ax,count ; errormov 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

Page 4: Outline

Your turn . . .

.databVal BYTE 100bVal2 BYTE ?wVal WORD 2dVal 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:

Page 5: Outline

Memory to Memory?

Must go through a register…

.dataVar1 WORD?Var2 WORD ?.code

MOV AX, var1MOV var2, AX

Page 6: Outline

MOV Instruction Format

Page 7: Outline

Instruction Operand Notation

Page 8: Outline

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

Page 9: Outline

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.

Page 10: Outline

MOVZX Instruction Format

Page 11: Outline

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.

Page 12: Outline

MOVSX Instruction Format

Page 13: Outline

XCHG

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

Page 14: Outline

Direct-Offset Operands

Adding a displacement (or offset) to a variable name:

arrayB BYTE 10h, 20h, 30, 40h, 50h…

MOV AL, arrayB ; AL=10hMOV AL, [arrayB+1] ; AL=20hMOV AL, arrayB+1 ; Is it valid?

Page 15: Outline

Your turn. . .Write a program that rearranges the values of three doubleword values in the following array as: 3, 1, 2.

.dataarrayD 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,arrayDxchg eax,[arrayD+4]

xchg eax,[arrayD+8]mov arrayD,eax

Page 16: Outline

Evaluate this.datamyBytes BYTE 80h,66h,0A5h

• How about the following code. Is anything missing?

movzx ax,myBytesmov bl,[myBytes+1]add ax,bxmov bl,[myBytes+2]add ax,bx ; AX = sum

Yes: Move zero to BX before the MOVZX instruction.

Page 17: Outline

Addition and Subtraction

ADD X, YX := X + Y

SUB X, YX := X – Y

Page 18: Outline

INC, DEC, NEG

INC XX := X + 1 or X++

DEC XX := X – 1 or X--

NEG XX := –X

Page 19: Outline

Expression

Example: X=(A + B) * (D – E)

MOV EAX, AADD EAX, BMOV ECX, DSUB ECX, EIMUL EAX, ECXMOV X, EAX

Page 20: Outline

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

Page 21: Outline

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

Page 22: Outline

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, 0FFhADD AL, 1 ; CF = 1, AL=00MOV AX, 00FFhADD AX, 1 ; CF = 0, AX=0100h

Page 23: Outline

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

MOV AL, -128

SUB AL, 1 ; OF = 1

Page 24: Outline

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 11 0 0 0 0 0 0 1 1

CF is ignored for signed arithmetic. For example, the above is 4 + (-1) in decimal

Page 25: Outline

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

Page 26: Outline

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…?!

Page 27: Outline

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…?!

Page 28: Outline

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)

Page 29: Outline

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

Page 30: Outline

LAHF/SAHF

LAHF: load the low byte of EFLAGS register into AH.

SAHF: store the low byte of EFLAGS register into AH.

Page 31: Outline

Data Related Operators

Who are they? OFFSET, PTR, TYPE, LENGTHOF, SIZEOF

They are only understood by the assembler. They are not instructions!

Page 32: Outline

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:

Var1 BYTE “Hello”

MOV ECX, 13

MOV AL, Var1

Page 33: Outline

PTR

But sometimes we want to override the default.

myDouble DWORD 12345678h

MOV AL, myDouble ; error

MOV AL, BYTE PTR myDouble

MOV AX, WORD PTR myDouble

MOV AX, WORD PTR [myDouble+2]

MOV EAX, myDouble

Page 34: Outline

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:.databVal BYTE ?wVal WORD ?dVal DWORD ?dVal2 DWORD ?

.codemov esi,OFFSET bVal ; ESI = 00404000mov esi,OFFSET wVal ; ESI = 00404001mov esi,OFFSET dVal ; ESI = 00404003mov esi,OFFSET dVal2 ; ESI = 00404007

Page 35: Outline

TYPE TYPE returns the size (in bytes) of each element.

.datavar1 BYTE ?var2 WORD ?var3 DWORD ?var4 QWORD ?

.codemov eax,TYPE var1 ; 1mov eax,TYPE var2 ; 2mov eax,TYPE var3 ; 4mov eax,TYPE var4 ; 8

Page 36: Outline

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

Page 37: Outline

SIZEOF SIZEOF returns the size of the variable (the whole

array). SIZEOF = LENGTHOF * TYPE

.data SIZEOFbyte1 BYTE 10,20,30 ; 3array1 WORD 30 DUP(?),0,0 ; 64array2 WORD 5 DUP(3 DUP(?)) ; 30array3 DWORD 1,2,3,4 ; 16digitStr BYTE "12345678",0 ; 9

.codemov ecx,SIZEOF array1 ; 64

Page 38: Outline

Indirect Operands

An indirect operand holds the address of a variable, usually an array or string. It can be dereferenced (just like a pointer).

.dataval1 BYTE 10h,20h,30h.codemov esi,OFFSET val1mov al,[esi] ; dereference ESI (AL = 10h)inc esimov al,[esi] ; AL = 20hinc esimov al,[esi] ; AL = 30h

Page 39: Outline

Array Sum Example

.data

arrayW WORD 1000h,2000h,3000h

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

Page 40: Outline

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]

Page 41: Outline

Pointers

.data

arrayW WORD 1000h,2000h,3000h

ptrW DWORD arrayW

.code

mov esi,ptrW

mov ax,[esi] ; AX = 1000h

Page 42: Outline

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

Page 43: Outline

Example: Summation

For I := 10 downto 1 {Sum := Sum+I}

MOVECX, 10

MOVEAX, 0

L1: ADD EAX, ECX

LOOP L1

Page 44: Outline

Your turn

What will be the final value of AX?

mov ax,6mov ecx,4

L1:inc axloop L1

How many times will the loop execute? mov ecx,0

X2:inc axloop X2

10

4,294,967,296 (=232)

Page 45: Outline

Copying a String

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

.codemov esi,0 ; index registermov ecx,SIZEOF source ; loop counter

L1:mov al,source[esi] ; get char from sourcemov target[esi],al ; store it in the targetinc esi ; move to next characterloop L1 ; repeat for entire string

Page 46: Outline

Nested Loop

.datacount DWORD ?.code

mov ecx,100 ; set outer loop countL1:

mov count,ecx ; save outer loop countmov ecx,20 ; set inner loop count

L2: ..loop L2 ; repeat the inner loopmov ecx,count ; restore outer loop countloop L1 ; repeat the outer loop