Lecture Computer organization and assembly language: Chapter 15 - Dr. Safdar Hussain Bouk

pptx
Số trang Lecture Computer organization and assembly language: Chapter 15 - Dr. Safdar Hussain Bouk 17 Cỡ tệp Lecture Computer organization and assembly language: Chapter 15 - Dr. Safdar Hussain Bouk 290 KB Lượt tải Lecture Computer organization and assembly language: Chapter 15 - Dr. Safdar Hussain Bouk 0 Lượt đọc Lecture Computer organization and assembly language: Chapter 15 - Dr. Safdar Hussain Bouk 1
Đánh giá Lecture Computer organization and assembly language: Chapter 15 - Dr. Safdar Hussain Bouk
4.6 ( 18 lượt)
Nhấn vào bên dưới để tải tài liệu
Để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions Lecture 14: Review Instruction Execution in Assembly Assembly Language Examples: Control Flow – JMP Instruction – LOOP Instruction – LOOP Example – Summing an Integer Array Lecture Outline STACK Operations – Runtime Stack – PUSH Operation – POP Operation – PUSH and POP Instructions – Using PUSH and POP – Related Instructions Runtime Stack • Imagine a stack of Plates . . . – Plates are only added/removed to/from the top – LIFO (Last In First Out) structure 10 9 8 7 6 5 4 3 2 1 Top Bottom Runtime Stack • Managed by the CPU, using two registers – SS (stack segment) – ESP (stack pointer) * Memory Stack 00000FF0 00000FF4 00000FF8 00000FFC ESP 00001000 * SP in Real-address mode 00000006 PUSH Operation (1 of 3) • A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location pointed to by the stack pointer. BEFORE AFTER 00000FF0 Memory Stack 00000FF0 00000FF4 00000FF8 00000FF8 ESP 00000FFC 00000FFC ESP 00001000 00000FF4 00000006 00001000 000000A5 00000006 PUSH Operation (1 of 3) • A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location pointed to by the stack pointer. AFTER 00000FF0 00000FF4 00000FF8 ESP 00000FFC 00001000 000000A5 00000006 PUSH Operation (1 of 3) • A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location pointed to by the stack pointer. AFTER 00000FF0 00000FF4 00000FF8 ESP 00000FFC 00001000 000000A5 00000006 PUSH Operation (2 of 3) • Same stack after pushing one more integer: 00000FF0 00000FF4 00000FF8 ESP 00000FFC 00001000 00000001 000000A5 00000006 PUSH Operation (3 of 3) • Same stack after pushing one more integer: • The area in the direction ESP grows, is always available (unless the stack has overflowed). 00000FF0 00000FF4 ESP 00000FF8 00000FFC 00001000 00000002 00000001 000000A5 00000006 POP Operation • Copies value at stack[ESP] into a register or variable. • Adds n to ESP, where n is either 2 or 4. – value of n depends on the attribute of the operand receiving the data BEFORE 00000FF0 ESP 00000FF4 00000FF8 00000FFC 00001000 AFTER 00000FF0 00000002 00000001 000000A5 00000006 00000FF4 ESP 00000FF8 00000FFC 00001000 00000001 000000A5 00000006 PUSH and POP Instructions • PUSH syntax: – PUSH r/m16 – PUSH r/m32 – PUSH imm32 • POP syntax: – POP r/m16 – POP r/m32 Using PUSH and POP Save and restore registers when they contain important values. PUSH and POP instructions occur in the opposite order. push esi push ecx push ebx mov mov mov ……… esi,OFFSET dwordVal ecx,LENGTHOF dwordVal ebx,TYPE dwordVal pop ebx pop ecx pop esi ; push registers ; display some memory ; restore registers Example: Nested Loop Remember the nested loop we created on page 129? It's easy to push the outer loop counter before entering the inner loop: mov ecx,100 L1: push ecx ; set outer loop count ; begin the outer loop ; save outer loop count mov ecx,20 L2: ; ; loop L2 ; set inner loop count ; begin the inner loop pop ecx loop L1 ; repeat the inner loop ; restore outer loop count ; repeat the outer loop Related Instructions • PUSHFD and POPFD – push and pop the EFLAGS register • PUSHAD pushes the 32-bit general-purpose registers on the stack – order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI • POPAD pops the same registers off the stack in reverse order – PUSHA and POPA do the same for 16-bit registers Summary • STACK Operations – Runtime Stack – PUSH Operation: • Decrement n bytes from ESP and store n bytes – POP Operation: • Read n bytes from location (ESP) and Increment n – PUSH and POP Instructions – Using PUSH and POP – Related Instructions Reference Most of the Slides are taken from Presentation: Chapter 5 Assembly Language for Intel-Based Computers, 4th Edition Kip R. Irvine (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.