Lab 8 The Stack (LIFO) Structure

Similar documents
Computer Systems Lecture 9

LAB 4 Extending Signed and Unsigned Numbers

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

LAB 5 Arithmetic Operations Simple Calculator

Lab Instructor : Jean Lai

SOEN228, Winter Revision 1.2 Date: October 25,

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Lecture 23: Pointer Arithmetic

Assembly Language Lab # 9

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

1 Unit 8 'for' Loops

The x86 Instruction Set

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

2003 LXI H, 42F2H ; this instruction store 42F2 in to the HL pair POP H ; store data from top of the stack to HL pair

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

pointers + memory double x; string a; int x; main overhead int y; main overhead

Systems Architecture The Stack and Subroutines

Dr. Md. Humayun Kabir CSE Department, BUET

Chapter 1: Object-Oriented Programming Using C++

Memory and Pointers written by Cathy Saxton

Objects and streams and files CS427: Elements of Software Engineering

OBJECT ORIENTED PROGRAMMING

CSC 222: Computer Programming II. Spring 2004

Computer Programming

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

ECE250: Algorithms and Data Structures C++ Language Tutorial

Segmentation in Assembly Language Programming

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

printf("this program adds the value 10 to a given integer number.\n\n");

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Introduction to the C programming language

Pointers II. Class 31

Homework Assignment #2 (revised)

Introduction to the C programming language

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

C++ Structures Programming Workshop 2 (CSCI 1061U)

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

Why VC++ instead of Dev C++?

2.2: Bitwise Logical Operations

LAB 3 Memory Addresses

EE 109 Lab 8a Conversion Experience

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

Procedure-Calling Conventions October 30

CSC 2400: Computer Systems. Using the Stack for Function Calls

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

1. Which of the following best describes the situation after Line 1 has been executed?

Project Identity Shampoo-Conditioner Identification Device/ Medicine Reminder Week 2: 1/28/07-2/3/07 Karla Sittnick

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

Run Time Environment

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Chapter 17 - Notes Recursion

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Computer Systems Organization V Fall 2009

Exercise 1.1 Hello world

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

Computer Programming

Week 3: Pointers (Part 2)

Getting started with C++ (Part 2)

C++ For Science and Engineering Lecture 15

UW CSE 351, Winter 2013 Midterm Exam

Homework #3 CS2255 Fall 2012

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Subprograms, Subroutines, and Functions

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Lab # 02. Basic Elements of C++ _ Part1

Pointers, Arrays and C-Strings

Assignment 3. Problem Definition:

Computer Programming. Dr. Fahad Computer Science Deptt.

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

Getting Started with Visual Studio

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

16.317: Microprocessor Systems Design I Fall 2015

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

Programming in C/C Lecture 2

Summer 2003 Lecture 14 07/02/03

CS103L SPRING 2018 UNIT 7: FILE I/O

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Module 07: Programming in C++

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

DELHI PUBLIC SCHOOL TAPI

Ch. 11: References & the Copy-Constructor. - continued -

Subprograms: Local Variables

Intel assembly language using gcc

CMSC 313 Lecture 12 [draft] How C functions pass parameters

1 Short Answer (7 Points Each)

Transcription:

Lab 8 The Stack (LIFO) Structure Objective: The stack segment in memory is where the 80x86 maintains the stack. The stack stores important information about program including local variables, subroutine information, and temporary data. The 80x86 controls its stack via the ESP (stack pointer) registers. The 80x86 decrements the stack pointer by the size of the data you are pushing and then it copies the data to memory where ESP is then pointing. POP instruction copies the data from memory location ESP before adjusting the value in ESP. Recall the declared variable and its size. Generate random numbers (4 bytes) between 0 and 100. Generate random even or odd numbers (4 bytes) between 0 and 100. Practice push and pop instructions and observe the changes of ESP value. 1. Type and explain Example 1 (StackApp.cpp). Compile, Build, and Run the program. Example 1 illustrates: Random number generation Push to store an object into the top of the stack data structure Pop to remove an object from the top of the stack data structure Observe and record the register of ESP before and after push or pop Explain each line of Example 1 (StackApp.cpp) and describe the output of each in your lab report Based on the output screen, fill the following table (see the table example in 4) 2. Modify Example 2 (StackAppTemp.cpp) to do: Random even numbers generation (for example generate 10 even numbers) Push each even number onto the top of the stack data structure Pop to remove an object from the top of the stack data structure Observe and record the register of ESP before and after push or pop Based on the output screen, fill the following table (see the table example in 4) 3. Modify Example 2 (StackAppTemp.cpp) to do: Random odd numbers generation (for example generate 10 odd numbers) Push each odd number onto the top of the stack data structure Pop to remove an object from the top of the stack data structure Observe and record the register of ESP before and after push or pop Based on the output screen, fill the following table (see the table example in 4) 4. Based on the output screen, fill the following table, for example Instruction Stack pointer ESP after push or pop Integer pushed or popped PUSH 3bfb10 16 3bfb0c 73 3bfb08 79 3bfb04 7 3bfb00 98 3bfafc 91

POP 3bfb00 91 3bfb04 98 Written exercises: 1. Explain the Programs (each block of the program). 2. Include the sample source code and the outputs in your lab report. 3. Include the modified program and outputs in your lab report 4. The list of numbers stored onto stack 5. The list of values popped off the stack 6. The relationship between stack pointer ESP with the instruction push (or pop) 7. Fill the table above. 8. Summary. Example 1 /*********************************************************** File name: StackApp.cpp Method: Embedding an in-line assembly language module in a C program A. Random number generation B. Push to store an object into the top of the stack data structure C. Pop to remove an object from the top of the stack data structure D: Observe the register of ESP before and after push or pop std::cout<<hex is used to display hex information on the console exit(0) to terminate a C/C++ program system ("pause") is used to hold console *************************************************************/ #include <stdio.h> #include <iostream> #include <time.h> using namespace std; #define size 6 int main() int i; //used for the index int arr_data[size]; // data to store onto stack int arr_pop[size]; // data to popped off from the stack int stack_addr_push[size]; // the list of ESP after push int stack_addr_pop[size]; // the list of ESP after pop int espb, espa; // to hold ESP value

int temp =0; srand(time(0)); //randomize seed arr_data[i]= rand()%100; // random number 0-100 cout << "Generated random numbers stored at the array"<< endl; cout << arr_data[i] << " "; cout << "====================PUSH======================"<<endl; mov espb, ESP cout << "The hexidecimal value of ESP before PUSH is " <<hex<< espb <<endl; temp= arr_data[i]; mov eax, temp; push eax; mov espa, esp; stack_addr_push[i] = espa; cout << "Push the number "<<dec<< arr_data[i] << " onto the stack" << endl; cout << "The hexidecimal value of ESP after PUSH is " <<hex<< espa <<endl; cout <<"\n"; cout << "====================POP======================"<<endl; mov espb, ESP cout << "The hexidecimal value of ESP before POP is " <<hex<< espb <<endl; pop eax; mov espa, esp;

mov temp, eax; arr_pop[i] = temp; stack_addr_pop[i] = espa; cout << " The number "<<dec<< arr_pop[i] << " popped off from the stack" << endl; cout << "The hexidecimal value of ESP after POP is " <<hex<< espa <<endl; cout << "============================================="<<endl; cout << "The data stored onto the stack in order are"<< endl; cout <<dec<< arr_data[i] << " "; cout << "The ESP addresses in order after stored each number onto stack are" << endl; cout <<hex<< stack_addr_push[i] << " "; cout <<endl <<endl;; cout << "============================================="<<endl; cout << "The values popped off from the stack in order"<< endl; cout <<dec<< arr_pop[i] << " "; cout << "The ESP addresses in order after pop integer off stack"<< endl; cout <<hex<< stack_addr_pop[i] << " "; system ("pause"); exit(0); return 0;

The output from StackApp.cpp

Example 2 /*********************************************************** File name: StackAppTemp.cpp Method: Embedding an in-line assembly language module in a C program A. Random number generation B. Find even numbers C. Push to store even numbers into the top of the stack data structure D. Pop to remove an object from the top of the stack data structure E: Observe the register of ESP before and after push or pop std::cout<<hex is used to display hex information on the console exit(0) to terminate a C/C++ program system ("pause") is used to hold console *************************************************************/ #include <stdio.h> #include <iostream> #include <time.h> using namespace std; #define size 6 int main() int i; //used for the index int arr_data[size]; // data to store onto stack int arr_pop[size]; // data to popped off from the stack int stack_addr_push[size]; // the list of ESP after push int stack_addr_pop[size]; // the list of ESP after pop int espb, espa; // to hold ESP value int temp =0; int num; // even number int count =0; srand(time(0)); //randomize seed // while( count<size) num =rand()%100; if (num%2 == 0 ) arr_data[count]= num ; // eve random number 0-100 count++; cout << "Generated even random numbers stored at the array"<< endl; for (i=0; i< count; i++) cout << arr_data[i] << " "; cout << "====================PUSH======================"<<endl; for (i=0; i< count; i++)

mov espb, ESP cout << "The hexidecimal value of ESP before PUSH is " <<hex<< espb <<endl; temp= arr_data[i]; mov eax, temp; push eax; mov espa, esp; stack_addr_push[i] = espa; cout << "Push the number "<<dec<< arr_data[i] << " onto the stack" << endl; cout << "The hexidecimal value of ESP after PUSH is " <<hex<< espa <<endl; cout <<"\n"; cout << "====================POP======================"<<endl; mov espb, ESP cout << "The hexidecimal value of ESP before POP is " <<hex<< espb <<endl; pop eax; mov espa, esp; mov temp, eax; arr_pop[i] = temp; stack_addr_pop[i] = espa; cout << " The number "<<dec<< arr_pop[i] << " popped off from the stack" << endl; cout << "The hexidecimal value of ESP after POP is " <<hex<< espa <<endl; cout << "============================================="<<endl; cout << "The data stored onto the stack in order are"<< endl; cout <<dec<< arr_data[i] << " "; cout << "The ESP addresses in order after stored each number onto stack are" << endl;

cout <<hex<< stack_addr_push[i] << " "; cout <<endl <<endl;; cout << "============================================="<<endl; cout << "The values popped off from the stack in order"<< endl; cout <<dec<< arr_pop[i] << " "; cout << "The ESP addresses in order after pop integer off stack"<< endl; cout <<hex<< stack_addr_pop[i] << " "; system ("pause"); exit(0); return 0;