tutorial f 03

21
Lab Tutorial The Lab Environment The purpose of the first part of the tutorial is to introduce you to the Linux workstation environment, which you will be using for all of the labs assignment for this term. It also contains a brief introduction to do the following: (i) editing your assembly language program code via the VI Editor, (ii) building (translating) your assembly code (source program) into machine (object) code via the NASM assembler, (iii) linking and loading the machine codes that you wish to run, and (iv) run your (machine code) program under system debug facilities (gdb and its graphical interface ddd). Using the Linux Workstations The laboratory environment runs the Linux Redhat 9 platform and the NASM assembler. To be compatible with the laboratory environment, if you do some of your laboratory assignments are home, your home machine should be compatible with the above platform. 1- Login and Logout (Getting Started) You can log into a lab machine on the visual login screen if you are physically in the lab. You should use your own username and password that you obtained from the help desk in room H960. Upon logging into your account, by default, you will be working on your own home directory (with the same name as your username) that keeps your own files. When you finish your work and wish to leave, you should log off from the computer. Do not shut down the system. To log off from your computer, simply click once on the Main Menu Button ( ) on the Panel and drag your mouse cursor to the Log out button that appears in the sub-window.

Upload: nilofer-varis

Post on 02-Jan-2016

11 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Tutorial f 03

Lab Tutorial The Lab Environment

The purpose of the first part of the tutorial is to introduce you to the Linux workstation environment, which you will be using for all of the labs assignment for this term. It also contains a brief introduction to do the following: (i) editing your assembly language program code via the VI Editor, (ii) building (translating) your assembly code (source program) into machine (object) code via the NASM assembler, (iii) linking and loading the machine codes that you wish to run, and (iv) run your (machine code) program under system debug facilities (gdb and its graphical interface ddd).

Using the Linux Workstations

The laboratory environment runs the Linux Redhat 9 platform and the NASM assembler. To be compatible with the laboratory environment, if you do some of your laboratory assignments are home, your home machine should be compatible with the above platform.

1- Login and Logout (Getting Started)

You can log into a lab machine on the visual login screen if you are physically in the lab. You should use your own username and password that you obtained from the help desk in room H960. Upon logging into your account, by default, you will be working on your own home directory (with the same name as your username) that keeps your own files.

When you finish your work and wish to leave, you should log off from the computer. Do not shut down the system. To log off from your computer, simply click once on the Main Menu Button ( ) on the Panel and drag your mouse cursor to the Log out button that appears in the sub-window.

Page 2: Tutorial f 03

When the confirmation dialog appears, select the Logout option and click the Yes button. If you want to save the configuration of your Panel, as well as any programs which are running, check the Save current setup option, as well.

2- Open a terminal window (Doing Work)

You can open a working window by clicking click once on the Main Menu Button ( ) on the Panel and drag your mouse cursor to the system tools then clicks on Terminal.

Page 3: Tutorial f 03

Alternately you can also click once on Terminal Button ( ) located on the Panel.

The terminal window is used to enter command line instructions to Linux. Editing/managing files and launching other applications are a few of the functions that can be performed using the command line. (Note: Many of the command line instructions have an associated icon on the desktop, or item in the "Programs" menu. Feel free to explore alternative methods, and determine which method you prefer).

Try to perform the following functions using command line instructions:

a) The 'ls' command (listing all files in the present directory)

The first command you will use upon logging into your account is ‘ls’. 'ls' is a shell command that will list the contents of the current directory with just the information about all the file names in that directory.

b) 'cd' command (change directory)

'cd' means 'change directory'. Typing: “cd [directory name]” will get you into the specified directory.

Typing “cd ..” will get you out of the current directory and back to the root. If you just type “cd”, you will return to your home directory. c) The ‘cp’ command (copy file)

Page 4: Tutorial f 03

'cp' is for copying files from one place to another. This can be used to make a duplicate of some file under a different name. As an example, suppose you wish to make a backup from hello.asm in the current directory to the comp228 directory that is located in the parent directory. This can be accomplished as follows:

d) Create a new directory (make directory)

'mkdir' is the command for making (sub)directories under the current directory. This enables one to structure his file storages into directory folders under different names. For example, you can make a subdirectory named comp228 under your home directory for saving all your files created for the course as follows:

'rmdir' is the reversal of 'mkdir'- it removes (destroys) the directory named in the command. It should be pointed out that in order to remove a directory, the directory has to be empty. This may be useful when that directory is deemed useful to you and you wish to destroy it.

e) Editing a file (vi editor)

A file can be created and/or edited using the standard vi editor available on Linux. This section gives a brief introduction to the use of vi. If you are familiar with other editors, you can certainly use them for creating a text file. Working with 'vi': Suppose we wish to create a new file that stores the assembly language program that we have written and call this file tut1.asm. This can be accomplished as follows:

Page 5: Tutorial f 03

The system will display a new window with the name 'tut1.asm' and [new file] at the bottom.

This window allows you enter your program code line by line. To start writing, you should press ESC and the 'i' key (i for insert). Failing to do so may sometimes cause omission of the first characters that you entered.

The insertion allows you to type in your program codes line by line. Corrections may be made by backspace. Insertion is terminated by pressing ESC ESC. To save the file properly, you can press ESC, followed by the colon key ':' and 'w' (write). Saving the file and quitting vi can be accomplished by pressing ESC, followed by the colon key ':' and wq (write, quit). To quit without saving, press ESC, ':' and 'q'. Vi may protest if you have written something and you don't want to save it. If you press ESC ':' 'q!', vi will accept it and not save your changes. If the named file already exists, then vi will simply open the existing file for editing:

This command will open the hello.asm file.

Page 6: Tutorial f 03

B: The NASM Assembler

The purpose of this part of the tutorial is to introduce you to the NASM assembler. The following steps will take you through the complete procedure for translating an assembly program. In your laboratory assignments, you will be repeating steps #4-6 quite often. So it is a good idea to familiarize with the process.

1. Get a copy of the hello.asm files from the course home page.

2. Make a directory for comp228 course in your home directory by using mkdir command: #mkdir comp228

3. Make hello directory inside comp228 directory by typing:

#cd comp228

#mkdir hello

4. Copy hello.asm file to the hello directory by using the ‘cp’ command. Building an executable Building an executable involves a two-step process of (i) assembling (translating the symbolic code into machine language code) and (ii) linking (finalizing addresses to load the machine language code into memory for execution). Translation is accomplished by:

Page 7: Tutorial f 03

$ nasm -f elf hello.asm # this generates an (output) file with default name hello.o

Page 8: Tutorial f 03

Linking is accomplished by: $ ld -o hello hello.o # this generates an executable image named hello

As you can see from the above, your directory now contains three files: hello.asm containing the original symbolic source that you have entered via vi; hello.o that is generated by the NASM assembler (translator); hello that is the executable memory image that is generated by the linker (ld).

Run the program

Only executable images can be run. Hence to run the hello program, one can do the following:

The kernel will automatically execute the hello program until it stops (and returns control to the kernel).

Debugging NASM Assembly Language in LINUX

For this course, you will use ‘ddd’ to debug assembly language programs. ‘ddd’ is a graphical version of ‘gdb’. Using a debugger is more important for assembly language programming than for a high-level programming language because printing out values from an assembly language program is non-trivial. Assembly language programs are often unstructured and unstructured changes of control flow can occur. Adding debugging code to an assembly language program might itself introduce new errors, or make new bugs surface without actually injecting new errors in the code. We will summarize some of the more useful commands for assembly language debugging below.

Starting and stopping programs We are using the following example for this part of the tutorial.

Page 9: Tutorial f 03

;test1.asm

global _start ; must be declared for linker! segment .data L1 dd 1234h L2 dd 0 segment .text _start: ; must tell linker where our entry point is! mov eax,[L1] ;Instruction 1 assigns the content of num1 to eax mov ebx,4 ;Instruction 2 assigns the sum of content of eax ;and 4 and store in eax ali: add ebx,eax ;Instruction 3 assigns the content of eax to num2 mov [L2],ebx exit: mov eax,1 ; system call number (sys_exit) int 0x80 ; later...

After creating the above file using vi, you could proceed to assemble and link it using the following commands already introduced to you earlier.

After you have assembled and linked your program using nasm and ld, you can invoke the debugger using the LINUX command: ddd ./test1

Page 10: Tutorial f 03

At this point “ddd” might complain that "no debugging symbols found". Just ignore this particular comment.

The first few steps change some of the ddd setting to the ones that are appropriate, as follows. Select the view tab in the Menu Bar. This opens a sub-window. Deselect the “Source Window” and select “Machine Code Window” in the sub-window.

Selecting the “Machine Code Window” will open a “command” window for use during the debug session.

Page 11: Tutorial f 03

Now go to the “status” tab in the menu bar and select “registers” in the sub-window that pops up. The following windows will be open.

Since the GNU assembler uses AT&T style syntax, ‘ddd’ also uses AT&T style syntax instead of Intel-style syntax. Most importantly, in AT&T style syntax the instruction for

Page 12: Tutorial f 03

loading register EAX with the constant 1 looks like

mov 0x1, %eax In Intel-style syntax, the order of the source and destination are reversed. Since we are using Intel-style syntax for this course we need to change the ddd styl. To change the AT&T style syntax to Intel-style syntax you can do as follow:

Go to the Edit GDB setting and change disassembly flavor from “att” to “intel”.

then select close. Note: do not select apply because in some Linux version this option has a problem. Just you need to close the debugger setting windows. And then save your new options.

You can now run your program inside ‘ddd’ by selecting ‘run’ from the ‘program’ tab.

Page 13: Tutorial f 03

or you can select “run” from the “command” window:

However, the program will just behave exactly as it did when it ran under the Linux shell. The point of using a debugger is to set "breakpoints" where the execution of the program is paused. The pause returns you to the ‘ddd’ and you can use various ddd/gdb commands to examine the contents of the registers and/or memory. It is common to have breakpoints set at the beginning of a program, at the end of a program, at the top of a loop and anywhere you think an program error exists in your program. When using ‘ddd’ with C/C++ programs, you can set breakpoints by the line number of the statement

Page 14: Tutorial f 03

in the source file. However, NASM doesn't generate the line number information for ‘ddd’. So you must set the breakpoints according to addresses relative to labels in the assembly language program.

For example:

break *_start+5

It will stop the execution of the program 5 bytes after the address labeled by _start. However,

break *_start does not stop the program before the execution of the first instruction. A workaround is to add a new label after the first label in the beginning of your program and set a break point on it:

break “second label”

Click this button to set or unset a break point

Page 15: Tutorial f 03

Using “display windows” to keep track of program variable To add a new variable to display windows we can do as follow:

• First, select display windows from Data menu bar. • Next, type your variable name in “Display expression” text combo box. • Then, select display button from “New Dependent Display” windows to add your

variable.

After execution of your program has been halted by a breakpoint, you can resume execution of the program in several ways. The "cont" (continue) command resumes execution after the breakpoint. The execution continues until the next breakpoint is reached or the program terminates. Alternatively the single step commands "stepi" or "nexti" command may be used to execute a single machine instruction after the breakpoint. The difference between "stepi" and "nexti" arises when the next instruction is a function call. The "nexti" command will continue execution until the return from the function call (which might be never) is performed. On the other hand, the "stepi" command will enter the function and step a single instruction there. The command "where" will show where the execution of a program has paused. If you are using ‘ddd’, you can use the following windows to achieve the same.

Add your variable name here

Page 16: Tutorial f 03

Now, if you press run in the ‘command’ window, program will pause at the first break point. At this time, you cannot see anything in the “Machine Code Windows”. Now you can use ‘nexti’ or ‘stepi’ button from ‘command’ windows to step through the execution of the code.

By pressing ‘nexti’ button you can see the assembler dump of your code in the “Machine Code Windows”.

Page 17: Tutorial f 03

Now, If you look to the dump of assembler code of test1.asm in “Machine Code Windows” you can see:

0x08048080 <_start+0>: mov eax,ds:0x804909c

This line is the disassembling of “mov eax,L1”. 0X804909c is the memory address of L1. If you want to check the content of this memory location, In ddd, select “memory” from “data” tab.

GDB console

Add your variable here for display.

Command windows

Registers Windows Machine Code Windows

Display windows

Break point

Page 18: Tutorial f 03

After memory windows open you can type the memory address in the “from” filed. Because ‘L1’ is hexadecimal, you need select ‘hex’ from first combo box and ‘word’ from second combo box then you can press ‘display’ or ‘print’ to see the content.

Examining register and memory contents Using a combination of breakpoints and single-stepping, you should be able to trace through the sections of your code that you suspect to be buggy. This allows you to look at the contents of the registers and memory when the execution of the program has halted. In ‘ddd’ you can keep the registers windows open all the time and you can always keep the trace of registers.

Add your address or your variable here to print or display its memory contain

Page 19: Tutorial f 03

Sometimes, as you are tracing your program, you are really interested in the contents of a specific register. In ‘ddd’ you can use display windows by selecting ‘display’ from ‘data’ tab to keep the trace of some specific register or memory. Use "undisplay" to remove an item on this list. A very good use of the display command is to have the next instruction of the program printed whenever the program is halted.

Command Summary We've only covered a handful of useful ddd commands in this tutorial to get you started using ‘ddd’. The ddd debugger itself has a nice help feature. Simply type click "help" to get there.

Page 20: Tutorial f 03

Appendix A

Useful Things to Know Case Sensitivity in Linux When you are using Linux, especially with the Command Line, all files are case sensitive. So if you want to access a file called myFile.txt, you MUST type it in with the same capital letters, otherwise myfile.txt will not be found. File Name Completion If you know the partial name of your file but are not sure of the exact name, you can use the Linux File Name completion to find out what it is. You must be in the command line in the folder where the file is located. Type in the first few characters of the name and hit the TAB key. If your file is called myFile, you only need to type myF TAB and Linux will find it. Daily Backups of Files

If you accidentally overwrite, delete or otherwise change a file which you do not want to remove, you can find it. Our systems back up all of the files in your home directory once a day. You can get to those files by doing the following:

With the File Manager

1.Open up your home directory by clicking on the "Home" icon on the desktop.

2.In the "Location" bar, type in "file:/home/day_old/yourUserName" (substitute yourUserName for your user name).

3.All of the files here are the ones that are a day old. You can move/copy/etc these into your /home directory.

With the Command Line

1.Go to the command line and type in cd to go to your home directory. 2.Type cd day_old to go to the day_old directory. 3.Go to your folder (cd yourUserName). 4.You can copy/move/etc files and folders from here to your /home

directory.

To check the last-updated date, go to the day_old directory (see above), and type cat LASTUPDATED_ON and hit enter. This will print out the date and time when the

Page 21: Tutorial f 03

day_old directory was last updated (usually within 24 hours). Pasting To paste text, you can highlight the text that you want to copy, go to the place you wish to paste to, and hit the middle-button on the mouse (click on the scroll-wheel). Problems If you encounter problem with the system, do NOT reboot the computer. Contact the professor or programmer on duty. Finishing Up When you finish your work, please remember to logout (use the K-menu at the bottom left of the screen and choose logout). Never shut off the computer.