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;