Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 38 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
38
Dung lượng
567,71 KB
Nội dung
Assembly language programming By xorpd Basic Assembly The Stack xorpd.net Objectives We learn about the stack data structure We study the x86 stack implementation and instructions We see simple examples of using the stack Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out PUSH Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out PUSH Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out PUSH Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out POP Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out POP Stack Abstract idea for storing data Two operations are allowed: PUSH and POP The last element pushed is the first element to be popped LIFO - Last In First Out POP Example – Exchanging values Exchanging two values: push push pop pop eax ecx eax ecx Example – Exchanging values Exchanging two values: push push pop pop eax ecx eax ecx eax ecx Example – Exchanging values Exchanging two values: push push pop pop eax ecx eax ecx eax ecx esp 12345678 aabbccdd 001ff704 Addresses increase ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? esp ?? ?? ?? Example – Exchanging values Exchanging two values: push push pop pop ?? eax ecx eax ecx ?? ?? ?? ?? ?? eax ecx esp 12345678 aabbccdd 001ff700 ?? ?? 78 esp 56 34 12 ?? ?? ?? ?? Example – Exchanging values Exchanging two values: push push pop pop ?? eax ecx eax ecx ?? ?? ?? dd esp cc eax ecx esp 12345678 aabbccdd 001ff6fc bb aa 78 56 34 12 ?? ?? ?? ?? Example – Exchanging values Exchanging two values: push push pop pop ?? eax ecx eax ecx ?? ?? ?? dd cc eax ecx esp aabbccdd aabbccdd 001ff700 bb aa 78 esp 56 34 12 ?? ?? ?? ?? Example – Exchanging values Exchanging two values: push push pop pop ?? eax ecx eax ecx ?? ?? ?? dd cc eax ecx esp aabbccdd 12345678 001ff704 bb aa 78 56 34 12 ?? esp ?? ?? ?? Example – Saving and restoring Example: We want to keep the ecx register unchanged push ecx inc ecx pop ecx Example – Saving and restoring Example: We want to keep the ecx register unchanged push ecx inc ecx pop ecx Some calculation that changes ecx Example – Saving and restoring Example: We want to keep the ecx register unchanged push ecx inc ecx pop ecx ecx esp 00030200 001ff704 Addresses increase ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? esp ?? ?? ?? Example – Saving and restoring Example: We want to keep the ecx register unchanged push ecx inc ecx pop ecx ?? ?? ?? ?? ?? ?? ?? ecx esp 00030200 001ff700 ?? 00 esp 02 03 00 ?? ?? ?? ?? Example – Saving and restoring Example: We want to keep the ecx register unchanged push ecx inc ecx pop ecx ?? ?? ?? ?? ?? ?? ?? ecx esp 00030201 001ff700 ?? 00 esp 02 03 00 ?? ?? ?? ?? Example – Saving and restoring Example: We want to keep the ecx register unchanged push ecx inc ecx pop ecx ?? ?? ?? ?? ?? ?? ?? ecx esp 00030200 001ff704 ?? 00 02 03 00 ?? esp ?? ?? ?? Example – Saving and restoring (cont.) Keeping a few registers and then restoring them: push push push ecx eax ebx ; some code pop pop pop ebx eax ecx Note the push and pop order Summary A stack is an abstract idea for storing data Only PUSH and POP Last In First Out x86 stack: - ESP points to the “top” of the stack PUSH decreases esp and writes to the stack POP reads from the stack and increases esp Examples: Exchanging values Saving and restoring PUSH esp POP + ...Objectives We learn about the stack data structure We study the x86 stack implementation and instructions We see simple examples of using the stack Stack Abstract idea for storing... (Extended Stack Pointer) esp sp At the moment your code begins to run, esp already contains an address of a location in memory called the stack ESP and the stack are set up automatically by the. .. system … 00 13 2a de 4f 11 00 00 ff ff … esp There are some special instructions that deal with ESP and the stack PUSH PUSH arg Push onto the stack Two forms: arg is of size 16 bit: