Summer 2004IntroductionLaboratory DescriptionHow to Get StartedA Quick Primer on Intel Architecture Assembly InstructionsSetjmp Description (Taken from the Visual C++ Help system)Return ValueParameterRemarksLongjmp Description (Also Taken from the Visual C++ Help sysLaboratory HintsHelpful ResourcesQuestion 1Question 2Question 3Question 4Question 5Question 6COM S 414 Operating Systems Laboratory Summer 2004 Introduction The goal of this laboratory is to give you an opportunity to examine and ultimately understand the C library functions setjmp and longjmp. In so doing, you will explore a number of important systems components by disassembling and then commenting the assembly instructions in a simple C program using Visual C++ in Windows NT. At the end of this laboratory you will have a basic understanding of the Intel 80x86 architecture and the C programming language. Laboratory Description You are going to document the assembly instructions generated by Visual C++ for setjmp.c. To do this you should copy the code disassembly window from Visual C++ corresponding to setjmp and longjmp. (That is, you should copy the _setjmp3 and _longjmp code into a text editor. Be sure that all disassembly view options are turned on except symbol names and code bytes. See the hint section for an explanation on why you are copying _setjmp3 and _longjmp rather than setjmp and longjmp.) You should comment the assembly statements in the two functions on line-by-line basis. Be sure to add comments for the individual assembly instructions coupled with a broader description of what blocks of code are doing. Finally, answer the questions in Part B of this handout. How to Get Started • Download the code at: www.cs.cornell.edu/cs415/f02/setjmp.zip • Open Microsoft Visual Studio .NET C++. • Unzip the "setjmp.zip" file in the directory for your project. • Open the Visual Studio .NET command prompt and switch to your project directory. • ONLY DO THIS STEP IF YOU ARE NOT WORKING IN THE UNDERGRADUATE LAB. If the computer you are using does not have Visual Studio .NET installed in the usual place (c:\Program Files\Microsoft Visual Studio), then change the line VISUALSTUDIO = c:\Program Files\Microsoft Visual Studio .NET in "Makefile" to point to the correct location. • Run “nmake /f makefile” from the .NET prompt to build setjmp.exe. It should compile and link with a few warnings but no errors. If you execute setjmp.exe it will print out a number of variable names and their values. • Open up Visual Studio .NET. • Choose File->Open->Project and select your setjmp.exe file • Read through the Visual C++ help file entries for setjmp and longjmp. They are included here, in the laboratory handout, for your convenience. Even so, you shouldpractice using the help system if you are not already familiar with its operation. Experiment by entering the Help->Search section of Visual Studio. Type in a C library function (like setjmp, printf, scanf, or whatever your favorite C library call happens to be) and read through the corresponding entries. • Take a look at the source code for setjmp.c. You will see that it is a very simple program. Variables labeled v1, v2, and v3 are initialized with a series of values. Setjmp is called to save state information in a jmp_buf data stucture entitled mark. Setjmp always returns 0 when it called upon to store state information and returns a non-zero value when it is jumped to through a longjmp system call. Variable values are modified and printed next. Longjmp is called which returns us directly to the previous setjmp call where we print the final state of the v1, v2, and v3 values and then the program exits. • Start the Visual C++ debugger by selecting Debug->Step Into. • You will be prompted to create a setjmp text solution file. You can safely click the save button here and ignore the resulting setjmp.sln. • Choose Debug->Windows->Disassembly. You will see the 80x86 assembly instructions that correspond to the setjmp.c program complete with all library files that are called from setjmp.c either directly or indirectly. Assembly instructions appear in gray prefixed by their addresses in memory. The corresponding lines of C code appear in black and are prefixed by their line numbers if any. The current state of the user-accessible registers can be viewed through Debug->Windows->Registers. All disassembly viewing options should be on except symbol names and code bytes. Symbol names must be off or you will not see the stack manipulation that you need to become familiar with. Code bytes are harmless, but are not necessary for us. You can change your viewing options by right clicking on the disassembly window. • You can step through the individual assembly instructions using F10 to step over function calls and F11 to step into them. You can safely ignore library function calls other than setjmp, longjmp, and library functions called by setjmp and longjmp, but stepping through them may help you to get a feel for how the Visual C++ compiler has converted C instructions into assembly code and how the stack frame, local variables, and parameter passing works. • Next, cut and paste the disassembly for the _setjmp3 and _longjmp functions into a text file and start to think about how to comment the assembly code for those two functions ONLY. No other functions need to be commented, but calls to other functions and the recording of return values from those other functions do need to be commented. • The Registration, TryLevel, UnwindFunc, and UnwindData sections of the setjmp jmp_buf structure are there to handle C++ exception code properly (see the hint section for more detail). Similarly, global_unwind2, rt_probe_read4, local_unwind2, and NLG_notify exist for the same reason. You can label individual assembly instructions surrounding this code, but you are not responsible for understanding what it does in a global sense. That information is beyond the scope of the course. • Once you have a good understanding of how things work you can begin commenting the assembly code in earnest. Comment every assembly line. You should also indicate what clumps of instructions are used. Commented code should look something like this example://////////////////// Call the printf function passing passing in v1 and a text string as the arguments **Put the first local variable (located one word below the stack
View Full Document