Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.

Similar presentations


Presentation on theme: "Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1."— Presentation transcript:

1 Assembly 03

2 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1

3 inc Command inc increments the operand by one mov eax, 0 inc eax 2 eax

4 inc Command inc increments the operand by one mov eax, 0 inc eax 3 0 eax

5 inc Command inc increments the operand by one mov eax, 0 inc eax 4 1 eax

6 inc Command inc increments the operand by one mov eax, 0 inc eax 5 2 eax

7 inc Command inc increments the operand by one mov eax, 0 inc eax 6 3 eax

8 dec Command dec decrements the operand by one mov eax, 7 dec eax 7 eax

9 dec Command dec decrements the operand by one mov eax, 7 dec eax 8 eax 7

10 dec Command dec decrements the operand by one mov eax, 7 dec eax 9 eax 6

11 dec Command dec decrements the operand by one mov eax, 7 dec eax 10 eax 5

12 dec Command dec decrements the operand by one mov eax, 7 dec eax 11 eax 4

13 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 12

14 movsx Command movsx -> mov with sign extension movsx preserves the signed (negative) value when moving: 8bits -> 16 bits 8 bits -> 32 bits 16 bits -> 32 bits 13

15 movsx Command mov ax, -42; mov ebx, eax 14 eax ebx ax

16 movsx Command mov ax, -42; mov ebx, eax 15 eax ebx 16-bit 2’s complement interpreted as -42 0xFFD6 ax

17 movsx Command mov ax, -42; mov ebx, eax 16 eax 0x0000FFD6 ebx 0xFFD6 ax 32-bit 2’s complement interpreted as 65,494

18 movsx Command Now use the movsx command.. mov ax, -42; movsx ebx, ax 17 ebx ax eax ax Note: we are extending from 16 to 32 bits

19 movsx Command mov ax, -42; movsx ebx, ax 18 ebx ax eax 0xFFD6 ax

20 movsx Command mov ax, -42; movsx ebx, ax 19 0xFFFFFFD6 ebx ax eax 0xFFD6 ax 32-bit 2’s complement interpreted as -42

21 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 20

22 jmp Command jmp ->(always) “jump to label” mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 21 label

23 jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 22 1 eax ebx

24 jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 23 1 eax 1 ebx

25 jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 24 1 eax 1 ebx jump to skip

26 jmp Command mov eax,1 mov ebx,1 jmp skip mov ebx,100 skip: add eax,ebx 25 2 eax 1 ebx

27 jnz Command jnz -> “jump if not zero” Checks EFLAGS ZF (zero flag, bit #6) Previous instruction may modify ZF bit Note: Not all instructions modify ZF bit! If ZF clear Jump to label If ZF set Continue with next instruction 26

28 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 27 eax ZF (bit #6 of EFLAGS)

29 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 28 3 eax 0 ZF (bit #6 of EFLAGS)

30 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 29 2 eax 0 ZF (bit #6 of EFLAGS)

31 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 30 2 eax 0 ZF (bit #6 of EFLAGS) 1) ZF is clear 2) jump to loop label

32 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 31 1 eax 0 ZF (bit #6 of EFLAGS)

33 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 32 1 eax 0 ZF (bit #6 of EFLAGS) 1) ZF is clear 2) jump to loop label

34 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 33 0 eax 1 ZF (bit #6 of EFLAGS) result of instruction sets ZF

35 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 34 0 eax 1 ZF (bit #6 of EFLAGS) 1) ZF is set 2) execution continues

36 jnz Command mov eax, 3; loop: dec eax; jnz loop; mov eax, 0xBEEF; 35 0xBEEF eax 1 ZF (bit #6 of EFLAGS) ZF unchanged after mov command…Why?

37 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 36

38 nasm Assembly Program Sections 1..data 2..bss 3..text 37

39 nasm Assembly Program Sections.data Declare data items (variables) in memory Data items can be initialized with value Data items stored in memory with executable 38

40 nasm Assembly Program Sections.bss Declare data buffers (variables) in memory Data buffers NOT initialized with a value Great for large data buffers (e.g., 1,000 bytes) Data buffers NOT stored with executable More efficient than.data section Variables not loaded in memory with executable 39

41 nasm Assembly Program Sections.text Instructions (mnemonics and operands) No data items defined in.text (normally) Contains labels for locations 40

42 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 41

43 Labels “Bookmark” for program code Place to jump to Name for procedure Label must start with a letter, underscore ‘_’, period ‘.’, or question mark ‘?’ Label is case sensitive Label ends with colon ‘:’ 42

44 Labels _start is a special kind of Label _start similar to main() in C/C++ _start must go at beginning of.text section Required by Linux to load executable 43

45 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 44

46 String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 45

47 String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 46 Label

48 String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 47 db - “declare byte” - interpret as 8-bits

49 String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 48 - hard-coded string of characters - can use double or single quotes - assembler decides how much memory to set aside

50 String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 49 comma concatenates substrings

51 String Variables E.g., EatMsg: db “Eat at Joe’s!”, 10 50 decimal number for end of line (EOL) character (basically a newline)

52 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 51

53 equ Command equ -> “equate” Way to associate a value with a label equ is an assembly-time calculation Command is executed during assembly Similar to #define in C / C++ 52

54 equ Command.data SIZE: equ 10;.text _start mov eax, 10; mov eax, SIZE; 53 these instructions are the same!

55 equ Command Why use equ? Increased code readability E.g., SIZE vs. 10 Ability to clearly define constants Easier to maintain code What if SIZE changes from 10 to 12? Only need to modify one line of code 54

56 Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 55

57 $ Token In x86 assembly language, $ token means “here” EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 56

58 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 57 EatLen equates to…

59 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 58 …“here” minus EatMsg

60 $ Token EatMsg is a Label, which is just an address $ or here is also an address Current address of instruction in memory Since assembler sets aside bytes.. we can determine the string length.. 59

61 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 60 LabelAddressByte EatMsg:1008E 1009a 1010t 1011 1012a 1013t 1014 1015J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022

62 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 61 LabelAddressByte EatMsg:1008E 1009a 1010t 1011 1012a 1013t 1014 1015J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 EatMsg = 1008

63 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 62 LabelAddressByte EatMsg:1008E 1009a 1010t 1011 1012a 1013t 1014 1015J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 String requires 14 bytes of memory

64 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 63 LabelAddressByte EatMsg:1008E 1009a 1010t 1011 1012a 1013t 1014 1015J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 $ (here) is now address 1022 of memory

65 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 64 LabelAddressByte EatMsg:1008E 1009a 1010t 1011 1012a 1013t 1014 1015J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 $ = 1022 EatMsg = 1008 EatLen = $ - EatMsg = 1022 – 1008 = 14

66 $ Token EatMsg: db “Eat at Joe’s!”, 10 EatLen: equ $ - EatMsg 65 LabelAddressByte EatMsg:1008E 1009a 1010t 1011 1012a 1013t 1014 1015J 1016o 1017e 1018‘ 1019s 1020! 1021\n 1022 EatLen = 14

67 $ Token Now EatLen is flexible EatLen will be the correct size, regardless of EatMsg 66


Download ppt "Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1."

Similar presentations


Ads by Google