74
Assembly 02

Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

Embed Size (px)

Citation preview

Page 1: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

Assembly 02

Page 2: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

2

Outline

• mov Command• Registers• Memory• EFLAGS• Arithmetic

Page 3: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

3

mov Command

• “Moves” data from:• register to register• memory to register• register to memory

• CANNOT move data from memory to memory• (must use register as intermediate step)

• mov command actually copies data, does not move it

Page 4: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

4

mov Command

• Takes two operands: destination and source

mov eax,42

“move (copy) 42 into 32-bit register eax”

destination

source

Page 5: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

5

mov Command

• Source and destination must be same size• 8-bit byte• 16-bit word• 32-bit double-word

Page 6: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

6

mov Command

mnemonic

destination

source notes

mov eax 0x42 source is immediate data

mov ebx edi both are 32-bit registers

mov bx cx both are 16-bit registers

mov dl bh both are 8-bit registers

mov [ebp] edidestination is 32-bit memory data stored at edp

mov edx [esi]source is 32-bit memory data at the address stored in esi

Page 7: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

7

Immediate Data

• CPU does not have to “go anywhere” to get data• Data comes from instruction itself• Only source can be immediate data

mov eax,42

immediate data

Page 8: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

8

mov Command

• Source can be ASCII character(s)• Must enclose ASCII character(s) with single quotes

mov ebx,’ABCD’

Page 9: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

9

mov Command

mov ebx,’ABCD’

After instruction, contents of 32-bit register ebx:

ebx = 0x44434241

‘D’‘C’

’B’

’A’

Page 10: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

10

mov Command

mov ebx,’ABCD’

• x86 architecture is little-endian• LSB stored at lowest address

4344 4142ebx

‘C’‘D’ ‘A’‘B’

Page 11: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

11

Outline

• mov Command• Registers• Memory• EFLAGS• Arithmetic

Page 12: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

12

Registers

• x86 has four 32-bit general purpose registers

eax

ebx

ecx

edx

Page 13: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

13

Registers

• x86 has four 16-bit general purpose registers

ax

bx

cx

dx

Page 14: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

14

Registers

• x86 has eight 8-bit general purpose registers

ah al

bh bl

ch cl

dh dl

Page 15: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

15

Registers

ah al

eax

ax

Page 16: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

16

Registers

mov eax, 0x11111111mov ax, 0x2222

mov ah, 0x33mov al, 0x44

What’s in eax after the instructions execute?Let’s find out..

Page 17: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

17

Registers

mov eax, 0x11111111mov ax, 0x2222mov ah, 0x33mov al, 0x44

eax

Page 18: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

18

Registers

mov eax, 0x11111111mov ax, 0x2222mov ah, 0x33mov al, 0x44

eax 1111 11 11

Page 19: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

19

Registers

mov eax, 0x11111111mov ax, 0x2222mov ah, 0x33mov al, 0x44

eax 1111 22 22

Page 20: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

20

Registers

mov eax, 0x11111111mov ax, 0x2222mov ah, 0x33mov al, 0x44

eax 1111 33 22

Page 21: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

21

Registers

mov eax, 0x11111111mov ax, 0x2222mov ah, 0x33mov al, 0x44

eax 1111 33 44

Page 22: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

22

Registers

mov eax, 0x11111111mov ax, 0x2222mov ah, 0x33mov al, 0x44

eax 0x11113344

Page 23: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

23

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

What’s in eax, ebx, and ecx after these instructions execute?Let’s find out…

Page 24: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

24

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

eax

ebx

ecx

Page 25: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

25

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

eax 67 FE

ebx

ecx

Page 26: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

26

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

eax 67 FE

ebx 67 FE

ecx

Page 27: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

27

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

eax 67 FE

ebx 67 FE

ecx 67

Page 28: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

28

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

eax 67 FE

ebx 67 FE

ecx FE 67

Page 29: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

29

Registers

mov ax, 0x67FEmov bx, axmov cl, bhmov ch, bl

eax

ebx

ecx

0x67FE

0x67FE

0xFE67

Page 30: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

30

Registers

• How to swap contents of eax and ebx?• Could use three mov instructions• Requires a third register

mov ecx, eaxmov eax, ebxmov ebx, ecx

Page 31: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

31

Registers

• Or, use the xchg mnemonic

xchg eax, ebx

Page 32: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

32

Outline

• mov Command• Registers• Memory• EFLAGS• Arithmetic

Page 33: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

33

Memory

• Data stored in memory at 32-bit address• Segment of addressable memory “owned” by program• Operating System controls memory access• Why??

• Use square brackets to get data stored at memory address• Information inside the brackets: “effective address”

mov eax, [ ebx ]

Page 34: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

34

Memory

mov ax, 4mov bx, [1]

address

data

0 0xFFFF

1 0xBEEF

2 0x0ACE

3 0xFEED

4 0xACDC

ax

bx

Page 35: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

35

Memory

mov ax, 4mov bx, [1]

address

data

0 0xFFFF

1 0xBEEF

2 0x0ACE

3 0xFEED

4 0xACDC

4ax

bx

Page 36: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

36

Memory

mov ax, 4mov bx, [1]

address

data

0 0xFFFF

1 0xBEEF

2 0x0ACE

3 0xFEED

4 0xACDC

4ax

0xBEEFbx

Page 37: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

37

Memory

• In assembly, variable means address not data

var: db 0xAB…mov eax varmov ebx [var]

eax

ebx

Page 38: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

38

Memory

• In assembly, variable means address not data

var: db 0xAB…mov eax varmov ebx [var]

eax

ebx

this variable declaration instruction must be declared in .data section of assembly code (more on this later)

Page 39: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

39

Memory

• In assembly, variable means address not data

var: db 0xAB…mov eax varmov ebx [var]

0x0804909eax

ebx

32-bit memory address for var (may change depending on OS)

Page 40: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

40

Memory

• In assembly, variable means address not data• Must use [square brackets] to access data

var: db 0xAB…mov eax varmov ebx [var]

0x0804909eax

0xABebx

value stored at address var

Page 41: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

41

Memory

• Declaring a variable• Variable declaration goes in “section .data”

section .datax: db 0x01y: dw 0x2345z: dd 0x6789ABCD

Page 42: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

42

Memory

• Declaring a variable• Variable declaration goes in “section .data”

x: db 0x01

variable name followed by :

variable typedb for byte

initial value

Page 43: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

43

Memory

• Declaring a variable• Variable declaration goes in “section .data”

y: dw 0x2345

variable name followed by :

variable typedw for word (16-bits)

initial value

Page 44: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

44

Memory

• Declaring a variable• Variable declaration goes in “section .data”

z: dd 0x6789ABCD

variable name followed by :

variable typedd for double word (32-bits)

initial value

Page 45: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

45

Memory

• nasm does not “remember” variable size• You must specify either byte, word, or dword• (when entering an immediate value)

section .datax: db 0x01

…mov [x], byte 0x45mov al, [x] ; examine variable x

Page 46: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

46

Memory

• nasm does not “remember” variable size• You must specify either byte, word, or dword• When accessing registers, register size tells assembler what to do

section .datay: dw 0x2345

…mov [y], word 0x6789mov bx, [y] ; examine variable y

Page 47: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

47

Memory

• nasm does not “remember” variable size• You must specify either byte, word, or dword

section .dataz: dd 0x6789ABCD

…mov [z], dword 0xACEBEEF0mov ecx, [z] ; examine variable z

Page 48: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

48

Outline

• mov Command• Registers• Memory• EFLAGS• Arithmetic

Page 49: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

49

EFLAGS Register

• 32-bits wide• Each bit represents a flag• Bit position determines function• Check flags for various CPU states

• When flag is set, it equals 1• When flag is cleared, it equals 0

Page 50: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

50

EFLAGS Register

• Analogous to red flags on row of rural mailboxes

Page 51: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

51

EFLAGS Register

• Each flag has 2-3 letter symbol (for programmer use)• Some important flags:

OF: overflow flag (bit 11)• set when value too large to fit in operand

ZF: zero flag (bit 6)• set when operand set to zero

CF: carry flag (bit 0)• set when arithmetic or shift “carries” out a bit

Page 52: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

52

Page 53: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

53

EFLAGS Register

• How and when flags are set or cleared depends on instruction!

• Some instructions modify flags, others do not

• Check Appendix A for information!

Page 54: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

54

Outline

• mov Command• Registers• Memory• EFLAGS• Arithmetic

Page 55: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

55

Arithmetic

• add mnemonic• Addition• Straightforward

• mul mnemonic• Multiplication• Nuanced (implicit operand)

Page 56: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

56

Arithmetic (add)

add dst,src

dst = dst + srcdst += src

destination register gets overwritten with destination + source

Page 57: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

57

Arithmetic (add)

mov eax,2mov ebx,3add eax, ebx

eax

ebx

Page 58: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

58

Arithmetic (add)

mov eax,2mov ebx,3add eax, ebx

2eax

ebx

Page 59: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

59

Arithmetic (add)

mov eax,2mov ebx,3add eax, ebx

2eax

3ebx

Page 60: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

60

Arithmetic (add)

mov eax,2mov ebx,3add eax, ebx

5eax

3ebx

Page 61: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

61

Arithmetic (mul)

• mul mnemonic for multiplication• Multiplication slightly more complicated• Why? Because products can be VERY LARGE!!

• Multiplication uses an implicit operand• Assumption made by instruction• Does not / cannot be changed

Page 62: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

62

Arithmetic (mul)

mul ebx

• Destination operand is implicit• Not explicitly defined in instruction, but assumed to be present

• Destination operand always ‘A’ register (eax, ax, al)

eax *= ebx

Page 63: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

63

Arithmetic (mul)

mov eax, 2mov ebx, 4mul ebx

eax

ebx

Page 64: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

64

Arithmetic (mul)

mov eax, 2mov ebx, 4mul ebx

2eax

ebx

Page 65: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

65

Arithmetic (mul)

mov eax, 2mov ebx, 4mul ebx

2eax

4ebx

Page 66: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

66

Arithmetic (mul)

mov eax, 2mov ebx, 4mul ebx

8eax

4ebx

remember, eax is implicit operand

Page 67: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

67

Arithmetic (mul)

• When you multiply two large 32-bit numbers, you’ll need 64-bits to store the product

e.g., 231 * 231= 262

You can’t store 262 in 32 bits…

Page 68: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

68

Arithmetic (mul)

• To get around this, x86 use the ‘D’ register• ‘D’ register holds the high-order bits

• After multiplication, check the CF (carry flag) to see if ‘D’ register is used…• If CF is set, D register holds high-order bits

Page 69: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

69

Arithmetic (mul)

mov eax, 0xFFFFFFFFmov ebx, 0x3B72mul ebx

eax

ebx

edx

0CF

Page 70: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

70

Arithmetic (mul)

mov eax, 0xFFFFFFFFmov ebx, 0x3B72mul ebx

0xFFFFFFFFeax

ebx

edx

0CF

Page 71: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

71

Arithmetic (mul)

mov eax, 0xFFFFFFFFmov ebx, 0x3B72mul ebx

0xFFFFFFFFeax

0x3B72ebx

edx

0CF

Page 72: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

72

Arithmetic (mul)

mov eax, 0xFFFFFFFFmov ebx, 0x3B72mul ebx

0xFFFFC48Eeax

0x3B72ebx

0x3B71edx

1CF

Page 73: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

73

Arithmetic (mul)

mov eax, 0xFFFFFFFFmov ebx, 0x3B72mul ebx

0xFFFFC48Eeax

0x3B72ebx

0x3B71edx

1CF

carry flag (CF) bit of EFLAGS register is set

final product is 0x3B71FFFFC48E

edx eax

Page 74: Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1

74

Arithmetic

• Division works the same way (implicit operand)• See Appendix A for more