Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1."— Presentation transcript:

1 Assembly 02

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

4 mov Command Takes two operands: destination and source mov eax,42 “move (copy) 42 into 32-bit register eax” 3 destination source

5 mov Command Source and destination must be same size 8-bit byte 16-bit word 32-bit double-word 4

6 mov Command 5

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 6 immediate data

8 mov Command Source can be ASCII character(s) Must enclose ASCII character(s) with single quotes mov ebx,’ABCD’ 7

9 8 mov Command mov ebx,’ABCD’ After instruction, contents of 32-bit register ebx: ebx = 0x44434241 ‘D’ ‘C’ ’B’ ’A’

10 mov Command mov ebx,’ABCD’ x86 architecture is little-endian LSB stored at lowest address 9 43444142 ebx ‘C’‘D’‘A’‘B’

11 Outline mov Command Registers Memory EFLAGS Arithmetic 10

12 Registers x86 has four 32-bit general purpose registers 11 eax ebx ecx edx

13 Registers x86 has four 16-bit general purpose registers 12 ax bxbx cxcx dxdx

14 Registers x86 has eight 8-bit general purpose registers 13 ahal bhbl chcl dhdl

15 Registers 14 ahal eax ax

16 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 What’s in eax after the instructions execute? Let’s find out.. 15

17 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 16 eax

18 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 17 eax 11

19 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 18 eax 11 22

20 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 19 eax 11 3322

21 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 20 eax 11 3344

22 Registers mov eax, 0x11111111 mov ax, 0x2222 mov ah, 0x33 mov al, 0x44 21 eax 0x11113344

23 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl What’s in eax, ebx, and ecx after these instructions execute? Let’s find out… 22

24 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl 23 eax ebx ecx

25 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl 24 eax 67FE ebx ecx

26 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl 25 eax 67FE ebx 67FE ecx

27 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl 26 eax 67FE ebx 67FE ecx 67

28 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl 27 eax 67FE ebx 67FE ecx FE67

29 Registers mov ax, 0x67FE mov bx, ax mov cl, bh mov ch, bl 28 eax ebx ecx 0x67FE 0xFE67

30 Registers How to swap contents of eax and ebx? Could use three mov instructions Requires a third register mov ecx, eax mov eax, ebx mov ebx, ecx 29

31 Registers Or, use the xchg mnemonic xchg eax, ebx 30

32 Outline mov Command Registers Memory EFLAGS Arithmetic 31

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 ] 32

34 Memory mov ax, 4 mov bx, [1] 33 ax bx

35 Memory mov ax, 4 mov bx, [1] 34 4 ax bx

36 Memory mov ax, 4 mov bx, [1] 35 4 ax 0xBEEF bx

37 Memory In assembly, variable means address not data var: db 0xAB … mov eax var mov ebx [var] 36 eax ebx

38 Memory In assembly, variable means address not data var: db 0xAB … mov eax var mov ebx [var] 37 eax ebx this variable declaration instruction must be declared in.data section of assembly code (more on this later)

39 Memory In assembly, variable means address not data var: db 0xAB … mov eax var mov ebx [var] 38 0x0804909 eax ebx 32-bit memory address for var (may change depending on OS)

40 Memory In assembly, variable means address not data Must use [square brackets] to access data var: db 0xAB … mov eax var mov ebx [var] 39 0x0804909 eax 0xAB ebx value stored at address var

41 Memory Declaring a variable Variable declaration goes in “section.data” section.data x: db 0x01 y: dw 0x2345 z: dd 0x6789ABCD 40

42 Memory Declaring a variable Variable declaration goes in “section.data” x: db 0x01 41 variable name followed by : variable type db for byte initial value

43 Memory Declaring a variable Variable declaration goes in “section.data” y: dw 0x2345 42 variable name followed by : variable type dw for word (16- bits) initial value

44 Memory Declaring a variable Variable declaration goes in “section.data” z: dd 0x6789ABCD 43 variable name followed by : variable type dd for double word (32-bits) initial value

45 Memory nasm does not “remember” variable size You must specify either byte, word, or dword (when entering an immediate value) section.data x: db 0x01 … mov [x], byte 0x45 mov al, [x] ; examine variable x 44

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.data y: dw 0x2345 … mov [y], word 0x6789 mov bx, [y] ; examine variable y 45

47 Memory nasm does not “remember” variable size You must specify either byte, word, or dword section.data z: dd 0x6789ABCD … mov [z], dword 0xACEBEEF0 mov ecx, [z] ; examine variable z 46

48 Outline mov Command Registers Memory EFLAGS Arithmetic 47

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 48

50 EFLAGS Register Analogous to red flags on row of rural mailboxes 49

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 50

52 51

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! 52

54 Outline mov Command Registers Memory EFLAGS Arithmetic 53

55 Arithmetic add mnemonic Addition Straightforward mul mnemonic Multiplication Nuanced (implicit operand) 54

56 Arithmetic (add) add dst,src dst = dst + src dst += src destination register gets overwritten with destination + source 55

57 Arithmetic (add) mov eax,2 mov ebx,3 add eax, ebx 56 eax ebx

58 Arithmetic (add) mov eax,2 mov ebx,3 add eax, ebx 57 2 eax ebx

59 Arithmetic (add) mov eax,2 mov ebx,3 add eax, ebx 58 2 eax 3 ebx

60 Arithmetic (add) mov eax,2 mov ebx,3 add eax, ebx 59 5 eax 3 ebx

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 60

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 61

63 Arithmetic (mul) mov eax, 2 mov ebx, 4 mul ebx 62 eax ebx

64 Arithmetic (mul) mov eax, 2 mov ebx, 4 mul ebx 63 2 eax ebx

65 Arithmetic (mul) mov eax, 2 mov ebx, 4 mul ebx 64 2 eax 4 ebx

66 Arithmetic (mul) mov eax, 2 mov ebx, 4 mul ebx 65 8 eax 4 ebx remember, eax is implicit operand

67 Arithmetic (mul) When you multiply two large 32-bit numbers, you’ll need 64- bits to store the product e.g., 2 31 * 2 31 = 2 62 You can’t store 2 62 in 32 bits… 66

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 67

69 Arithmetic (mul) mov eax, 0xFFFFFFFF mov ebx, 0x3B72 mul ebx 68 eax ebx edx 0 CF

70 Arithmetic (mul) mov eax, 0xFFFFFFFF mov ebx, 0x3B72 mul ebx 69 0xFFFFFFFF eax ebx edx 0 CF

71 Arithmetic (mul) mov eax, 0xFFFFFFFF mov ebx, 0x3B72 mul ebx 70 0xFFFFFFFF eax 0x3B72 ebx edx 0 CF

72 Arithmetic (mul) mov eax, 0xFFFFFFFF mov ebx, 0x3B72 mul ebx 71 0xFFFFC48E eax 0x3B72 ebx 0x3B71 edx 1 CF

73 Arithmetic (mul) mov eax, 0xFFFFFFFF mov ebx, 0x3B72 mul ebx 72 0xFFFFC48E eax 0x3B72 ebx 0x3B71 edx 1 CF carry flag (CF) bit of EFLAGS register is set final product is 0x3B71FFFFC48E edxeax

74 Arithmetic Division works the same way (implicit operand) See Appendix A for more 73


Download ppt "Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1."

Similar presentations


Ads by Google