38
Assembly Programming under Linux Dhananjai M. Rao ([email protected] ) CSA Department Miami University Oxford, OH 45056 Table of Contents Assembly Programming under Linux ................................................................................. 1 Introduction ..................................................................................................................... 2 Accessing a Linux server ............................................................................................ 3 X-Server .......................................................................................................................... 4 Starting Xming on Windows computers in SEAS labs.............................................. 4 Caution: Performance of X-based Graphics ............................................................... 5 Starting X on Macintosh computers in SEAS labs ..................................................... 5 PuTTY............................................................................................................................. 7 Shell Commands ........................................................................................................... 14 Copying Files From/To the Linux Machine ................................................................. 16 Emacs ............................................................................................................................ 19 Editing ....................................................................................................................... 20 Assembling, Linking, and Running .............................................................................. 21 Assembling ............................................................................................................... 22 Linking ...................................................................................................................... 23 Running ..................................................................................................................... 23 Using GNU Debugger (gdb) ......................................................................................... 24 Starting GDB ............................................................................................................ 24 Using GDB................................................................................................................ 25 Shortcuts for setting breakpoints .............................................................................. 26 Using Eclipse for Assembly Programming................................................................... 27 Compiling and Running non-MPI C Programs ............................................................ 33 Compiling using ICC ................................................................................................ 34 Running ..................................................................................................................... 35 Compiling and Running MPI-based C Programs ......................................................... 36 Working with PBSpro ............................................................................................... 37 Working/Managing software Packages on Redhawk cluster.................................... 38

Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

  • Upload
    others

  • View
    21

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux

Dhananjai M. Rao

([email protected])

CSA Department

Miami University

Oxford, OH 45056

Table of Contents Assembly Programming under Linux ................................................................................. 1

Introduction ..................................................................................................................... 2

Accessing a Linux server ............................................................................................ 3 X-Server .......................................................................................................................... 4

Starting Xming on Windows computers in SEAS labs.............................................. 4

Caution: Performance of X-based Graphics ............................................................... 5 Starting X on Macintosh computers in SEAS labs ..................................................... 5

PuTTY............................................................................................................................. 7

Shell Commands ........................................................................................................... 14 Copying Files From/To the Linux Machine ................................................................. 16

Emacs ............................................................................................................................ 19 Editing ....................................................................................................................... 20

Assembling, Linking, and Running .............................................................................. 21

Assembling ............................................................................................................... 22

Linking ...................................................................................................................... 23 Running ..................................................................................................................... 23

Using GNU Debugger (gdb) ......................................................................................... 24

Starting GDB ............................................................................................................ 24 Using GDB................................................................................................................ 25

Shortcuts for setting breakpoints .............................................................................. 26 Using Eclipse for Assembly Programming................................................................... 27

Compiling and Running non-MPI C Programs ............................................................ 33 Compiling using ICC ................................................................................................ 34 Running ..................................................................................................................... 35

Compiling and Running MPI-based C Programs ......................................................... 36 Working with PBSpro ............................................................................................... 37

Working/Managing software Packages on Redhawk cluster.................................... 38

Page 2: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 2

Introduction

Linux (pronounced lee-nux) is a free operating system that is distributed by several

different vendors and organizations. In some sense it is similar to the Mac OS-X, that is,

it is very different from the Windows OS. However, Linux provides many advanced

software tools and a congenial environment for C and Assembly language programming.

Furthermore, Linux typically runs as a server which eases remote access from any

computer on the Internet. Consequently, you will have ready, consistent access to the

development environment and your files whether you are in-class, in laboratories, on-

campus, or off-campus. Last, but not the least, almost all High Performance Computing

(HPC) platforms run Linux. Consequently, you typically have no choice but to work with

Linux for HPC needs.

Luckily Linux has been primarily developed to aid programmers and developers, aka “the

smart people”. In other words, it is not meant for casual users. This feature is a double

edged sword. Linux provides powerful software tools that are primarily executed from a

simple command prompt and not from a GUI. Therefore, there is an initial learning curve

for users transitioning from a Windows-based environment to a Linux environment.

However, once you gain familiarity with Linux you will notice that your overall

development and deployment process actually proceeds much faster than in any other

programming environment.

This document has been designed to provide a detailed description of accessing a Linux

server and using some of its features. It is not meant to serve as an exhaustive guide to

using and programming with Linux. For help on commands and options refer to the man

(ual) pages available on the Linux machine. Furthermore, consult with your instructor for

any site specific details and additional directions if you experience problems or for

troubleshooting tips.

Page 3: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 3

Accessing a Linux server

Most Linux installations adhere to a standard convention for accessing a Linux server via

the Internet or a local area network (LAN). Accessing a Linux server is relatively

straightforward and can be done using freely available software. The figure below

presents a conceptual view of the aforementioned configuration and the steps involved

are as follows:

The basic details of logging on to the Linux server and utilizing its services are as

1. Start X-Server (Xming) on your computer.

2. SSH to the Linux server and login using PuTTY.

3. Use shell commands to:

a. Create a working directory (to ease file organization) the first time.

b. Change current working directory to your course directory.

4. Type your assembly program using emacs

a. Emacs is a general purpose, graphical text editor that also provides a

specialized environment for debugging programs using the GNU debugger

(gdb).

5. Assemble your source code using the GNU assembler (as)

a. Use emacs to fix any syntax errors in your program.

6. Link your source code using the GNU linker (ld) to generate the final executable

7. Run the executable from the shell prompt

8. Test, troubleshoot, and bug fix your programs

a. You will need to run your program several times, thoroughly test your

programs, and fix any semantic errors (aka bugs) in your program.

b. For troubleshooting and debugging your program, you can run the GNU

debugger (gdb) in emacs and step through each instruction in your

assembly program verifying its operation(s).

A detailed description of each of the aforementioned steps is presented in the following

subsections.

Linux Server

(easlnx01.eas.muohio.edu)

Your computer that is

running an X-Server!

(Windows/Mac/Linux/Unix)

X Secure connection via the Internet

established using SSH

Page 4: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 4

X-Server

In order to leverage the graphical capabilities of the Linux and use graphical editors you

need to have a piece of software called the X-Server running on your local machine. An

X server is a generic concept (like a web browser) and several different companies and

open source projects provide different implementations of an X server. The X-Server is

already installed on all SEAS machines including machines in the class rooms and

laboratories. All of the SEAS computers typically have an X-Server called Xming

installed on them. If you would like to install an X-Server on your personal machine you

may do so. However, note that SEAS or MU is not responsible or liable for any

damages, either express or implied, by installing or using software on your personal

computers. There are free X-Servers available too

(http://sourceforge.net/projects/xming, http://www.cygwin.com/).

Starting Xming on Windows computers in SEAS labs

In order to start the Xming X-server on Windows machines simply navigate the Start

menu options as shown below:

Note: The University and I are not liable for any damages (physical,

emotional, psychological, economic, or academic) caused by

installing any software or its derivatives on your personal computer.

Run Xming X-

Server by

clicking on the

above program!

Page 5: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 5

On successfully starting up the XMing X-server, you should see an X-icon in the system

task tray at the bottom right hand corner of your screen as shown below:

Caution: Performance of X-based Graphics

The X protocol that enables graphical displays on remote computers is an efficient

protocol that has been in long use for more than 30 years now. Consequently, it has been

highly streamlined and optimized for slow and low bandwidth networks. Nevertheless, X

does place a load on the network and therefore on slower networks you will find a

significant lag in performance of graphical applications. If you do so, then you can

always fall back on the non-graphical versions of various programs without much loss of

functionality.

Starting X on Macintosh computers in SEAS labs

All of the SEAS Mac computers have an X server already installed on them. In order to

start the X-server on Mac machines you need to perform the following tasks:

1. Click (left mouse button) on an empty area of the desktop to highlight the

“Finder” menu. From the finder menu, select the “Go→Utilities” option as

shown in the screenshot below:

Page 6: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 6

2. In the “Utilities” box, scroll to highlight the “X11” icon and double click on it as

shown in the snapshot below:

3. On double clicking the “X11” icon, the X server will start up and you should see a

shell window as shown below. The “$” prompt is called the shell prompt. All

operations you will be performing will be done by typing appropriate commands

at the shell prompt.

4. In addition, ensure that there is an “X” icon in the system menu (on the bottom of

your screen) as shown below:

Indicates X server is

running!

Page 7: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 7

PuTTY

PuTTY is a freeware program that is widely used to establish a secure shell (SSH) for

accessing remote Unix / Linux servers. All the SEAS computers have PuTTY already

installed on them. If you would like to install PuTTY on your personal machine then you

can download it for free from http://www.chiark.greenend.org.uk/~sgtatham/putty/.

On the SEAS computers, you can run PuTTY by navigating the start menu as shown

below:

Note: PuTTY uses port number 22 for connecting to the server. If

your firewall or virus scanner blocks port 22 you will not be able to

connect to the server. If you have problems connecting to the server,

first check your firewall and virus scanner settings.

Run PuTTY by

clicking on this link.

Page 8: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 8

On clicking the above link, you will see a popup dialog as shown below. Click the “Run”

button.

On running PuTTY you will see the following dialog box. Type in the name of the Linux

sever, namely easlnx01.eas.muohio.edu, in the “Host Name (or IP

address)” entry as shown below:

Page 9: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 9

Next, ensure that you configure PuTTY to translate all characters using UTF-8 encoding.

This option is necessary (even though both Linux and Windows are fully Unicode

compliant) because some of the Unicode glyphs on Windows are not fully compatible

with the standards. In order to configure PuTTY to use UTF-8 for translation, you need to

perform the following steps:

1. Click on the “Translation” option in the Category tree (to the left hand side

of the PuTTY dialog box, as shown below)

2. From the list of selection options, select UTF-8 as shown in the screenshot below:

Page 10: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 10

Now, click on the X11 option on the menu tree on the left hand side. Then check the

“Enable X11 forwarding” option in the check box to the right as shown below. This

ensures that the X protocol will tunnel over the secure (encrypted) link to your machine

so that you will be able to enjoy the graphical capabilities of Linux on your local PC.

The above steps complete the core configuration of PuTTY to access our Linux server.

Now, you may save your configuration for future use (and therefore you don’t have to

repeat the same steps again) as indicated below:

Page 11: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 11

You may save your current configuration as a separate session in PuTTY through the

following steps:

1. First click on the “Session” option on the Category tree (on the left hand

side) as shown below.

2. Then type out a session name for your session. Typically the short name of the

Linux server (namely easlnx01 in this case) works best.

3. Click the Save button to save this configuration with the given session name.

Now a new entry should appear in the list of Saved Sessions in the PuTTY

Configuration window.

4. Now connect to the server by clicking on the Open button at the bottom of the

PuTTY Configuration window.

Note: Once you have saved a configuration as a session, you can

directly connect to the Linux server by simply double clicking on the

corresponding session name in the list of Saved Sessions in the

PuTTY Configuration window. You don’t have to repeat the

configuration options each time you launch PuTTY.

1

2

3

4

Page 12: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 12

Now, click the “Open” button (or double click a saved session) to connect to the server.

On clicking the “Open” button you may see a dialog as shown below (this dialog box is

shown the first time you connect to a server). Click “yes” button to continue:

Now you should see the following terminal show up on your screen, prompting to enter

your user name and password.

1. Enter your MU id and press the ENTER key

2. Enter your MU password (when you type your password you will not see

any characters on the screen. This is normal operation) and press ENTER

key.

3. On entering the correct user name and password you will be dropped to a

shell prompt. The shell prompt may look a little different than the one

shown in the figure below:

This conceptual area is referred to as the shell

prompt or $ prompt. You will run various

programs by typing at the shell prompt.

Page 13: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 13

These steps complete the process of configuring PuTTY and using it to log onto a Linux

server. PuTTY runs the secure shell (SSH) protocol to interact with the Linux server.

Furthermore, PuTTY arranges the graphical display from the remote Linux server to be

routed to the X-Server running locally on your computer.

Now you are successfully logged into the Linux server (easlnx01) and you should be

able to type out various commands for developing, debugging, and running programs at

the shell prompt.

Note that many users often incorrectly assume that when graphical displays

appear on their computer the program is running on their computer.

However, such is not the case when running with X and Linux. The

programs are actually running on the Linux server. Only the display is routed

to your machine!

Page 14: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 14

Shell Commands

Almost all the operations that will be required for this class will be performed via shell

commands. This includes changing or navigating directories, creating and deleting files,

and logging out of the server. Some of the commonly used Shell commands and a few

examples of their usage are listed in the table below:

Command Description Example usage

exit Log out of the Linux box $ exit

cd Change directory $ cd m:

$ cd /usr/X11R6/bin

$ cd ..

pwd Show present working

directory

$ pwd

ls List files. $ ls

$ ls -l

$ ls -l ../*.s

$ ls -l ../a?b*.s

$ ls -l ../directory

mkdir Make new directory $ mkdir csa-470

rmdir Remove empty directory $ rmdir csa-570

cp Copy file or files. You can

copy entire directories

recursively as well.

$ cp ../a.s .

$ cp ../a.s bak.s

$ cp ../*.s .

$ cp ../a?b*.s .

$ cp -r ../directory .

mv Move file or files. You can

move directories as well.

$ mv ../a.s .

$ mv ../a.s bak.s

$ mv ../*.s .

$ mv ../a?b*.s .

rm Remove files and directories. $ rm ../a.s

$ rm a.s bak.s

$ rm ../*.s

$ rm ../a?b*.s

$ rm –rf directory

less View contents of the file

(scroll using arrow keys and

press q to quit).

$ less hello.s

emacs Editor and development

environment.

$ emacs&

icc Runs the Intel C compiler

program(s)

$ icc –g –Wall hello.c –o hello

mpicc Runs the Intel C compiler

with options to link in the

MPI library as well.

$ mpicc –g –Wall hello.c –o hello

gdb Debugger (Typically run

from within emacs)

$ gdb hello

Page 15: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 15

man Display manual page for a

command (use cursor keys to

navigate and q to quit)

$ man ls

$ man man

as Assembler (Convert

assembly source to object

file)

$ as –-gstabs+ hello.s –o hello.o

$ as –-help

ld Linker (Convert one or more

object files into a single

executable)

$ ld –e main hello.o –o hello.exe

–lkernel32

$ ld –-help

module Runs the module

management program to load

and unload modules on the

cluster.

$ module avail

$ module load java

clear Clears the current terminal $ clear

objdump Display information about an

object or executable file

$ objdump --help

$ objdump –s hello.exe

objcopy Copy parts of an object or

executable file to another file

possibly in different format.

$ objcopy --help

$ objcopy –j .text –O binary

hello.exe hello.sys

Most of these commands are going to be frequently used in this class. Consequently, you

need to get conversant with them. Spend time to try out each one of the commands

yourself.

The man (manual) command is your never failing friend! The manuals

contain all the information for each of the aforementioned shell

commands and include references to other related commands that can be

used. If you are not sure how to use a command, the first thing to do is

to look up the man pages. You may also find the man pages online

(http://www.linuxmanpages.com/).

Page 16: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 16

Copying Files From/To the Linux Machine

In order to copy files to/from the Linux machine you can use a freely available secure

copy program called WinSCP. Like most other programs WinSCP is just a fancy GUI

around the text-mode command called scp (short for secure copy). WinSCP is already

preinstalled on all SEAS computers. For your personal use, you can freely download and

install WinSCP from http://winscp.net/eng/index.php. In order to run WinSCP first

launch the program by navigating the start menu as shown below:

On running WinSCP you may be prompted with the following message box. Click the

Run button to continue running WinSCP.

Run WinSCP by clicking on

this menu option.

Page 17: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 17

When WinSCP starts running, it will first display the dialog box shown below. In this

dialog box you must enter the following information:

a. Enter the host name of the Linux server, i.e., easlnx01.eas.muohio.edu

b. Enter your Miami University login-id in the User name field.

c. Then click the Login button.

On clicking the Login button, you will be prompted to enter your MU password as shown

below. Enter your password and then click the OK button.

a

b

c

Page 18: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 18

On successfully logging in to the Linux server, WinSCP will display a file browser

window as shown below. The window consists of two halves. The left hand side window

displays the files on your local computer. The right hand side window displays files on

the remove Linux box. You can now drag-and-drop file(s) from one machine to another

and the files will be copied for you.

This side shows the files on

your local computer. You can

change the drive to M drive

using the combo box on top.

This side shows the files on the

remote computer, that is the

Linux server.

Drag-and-drop files from one side to another

to copy files from one machine to another.

Page 19: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 19

Emacs

As stated in the GNU manuals, emacs is an acronym for extensible, customizable, self-

documenting real-time display editor. Simply put emacs is essentially a text editor with

added support for running other programs thereby converting it into an Integrated

Development Environment (IDE). In this class, you will be using emacs to edit program

(C/Java/Shell) source files. In addition, we will also be using emacs for debugging

purposes by running the GNU debugger (gdb) inside emacs. This section will cover the

basics of editing files using emacs. The process of debugging assembly programs will

be covered in the next section.

The editing functionality of emacs is typical to any other text editor (such as notepad

in Windows) that it permits manipulation of characters in a file using the keyboard.

Cursor keys are used for moving the cursor to the appropriate location in the source file

and typing the desired data. In emacs, multiple files can be opened in separate

workspaces called buffers. Each buffer is independent of others can be separately

opened, closed, or modified. You may view multiple buffers at the same time which

provides a convenient mechanism for copying data across files or viewing different parts

of a single file. Emacs is typically run from the shell prompt by simply typing emacs

followed by the ampersand (&) symbol to run emacs in the background (as shown in

earlier section). On running emacs from the shell prompt, you should see a window as

shown below.

Minibuffer

Page 20: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 20

Editing

Once in emacs (you move your mouse over the emacs window and left click), you can

either use the menus on the top (using the mouse and clicking on them) or use shortcut

keys to perform the same operation. I encourage you to work with the shortcut keys for

the following reasons:

Although they are a little harder to learn, they provide the fastest way to perform

the tasks.

There isn’t a menu option for all of the commands and you have to use the

shortcut key bindings for them.

emacs can run in non-graphical environments (without an X-Server) where the

graphical main menu is not available. In such environments, knowing the

shortcut keys will significantly ease effective use of emacs.

This document will present some of the commonly used shortcut bindings in emacs.

Ensure you try and memorize these shortcuts. Usage of the main menu is pretty

straightforward as they operate as standard menus in any graphical environment. A

detailed documentation about emacs and the keybindings is available for free, online at:

http://www.gnu.org/software/emacs/manual/emacs.html

Action Keystrokes

Save file Ctrl-X Ctrl-S

Exit emacs Ctrl-X Ctrl-C

Open new file Ctrl-X Ctrl-F

Split screen mode Ctrl-X 2

Single buffer mode Ctrl-X 1

Switch between split screen sections Ctrl-X O

Switch between buffers Ctrl-X Ctrl-B

A quick reference card for many of the shortcut keys that can be used in emacs is

available off Blackboard website.

Note: If a command needs additional input (such as file

names) etc. emacs will prompt for input in the minibuffer

(bottom of window)

Page 21: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 21

Assembling, Linking, and Running

Once you have typed out your assembly source code in emacs and saved it to disk, you

are now ready to assemble and link your assembly program to generate the final

executable. Once you have successfully generated the executable you may run it from

the shell prompt or debug it using the GNU debugger (gdb). Note that this process

typically proceeds in an iterative manner as illustrated in the figure shown below:

1. The first step involves running emacs (a text editor), typing your assembly

program into an emacs buffer, and saving it (using a .s extension) to a file.

2. Next you run the assembler (as) on the assembly file created earlier. If the

assembler generates errors, you need to suitably modify your program using

emacs. The assembler generates an object file (named using a .o extension)

3. Once you have assembled your program successfully, you next link it using the

ld program. ld generates the final binary for execution.

4. Run the binary from the shell prompt and verify your program is working

correctly. You may need the use of the GNU debugger (gdb) for debugging your

assembly source. If you find any logic errors you need to repeat steps 1 through

4.

A brief description of how to assemble, link, and execute your programs are presented in

the following subsections.

Assembly Source

Typed in emacs

and saved to disk.

Assemble by running

the as at the shell

prompt to generate the

object code.

Link the generated

object code by running

ld from the shell

prompt to generate

final executable

Fix code for syntax errors

Fix code for linker errors

Run the generated

binary executable and

verify functionality. Fix code for logic errors

(May need gdb)

Page 22: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 22

Assembling

Assembling is the process of compiling your assembly source code (.s file) into an

intermediate, binary object code (.o file). This task is performed by running the GNU

assembler as from the shell prompt. The assembly source file to be compiled and the

destination object file name must be specified as parameters to as when it is run. An

example of running as on an assembly source file named hello.s to generate the object

code in a file named hello.o is illustrated below:

$ as –-gdwarf2 hello.s –o hello.o (and press ENTER)

The “—-gdwarf2” parameter is an option to the assembler that instructs it to place

debugging information into the object code in a special format. This option is needed in

order to debug (or troubleshoot) assembly programs (if necessary) using the GNU

debugger (gdb). Details of using gdb are presented in a different section.

Note that the assembler generates suitable errors when it encounters incorrect assembly.

If the assembler reports errors you have to appropriately modify the source code in

emacs and run the assembler again on the modified code as shown in the screenshot

below:

Note: Assembly source files are named with the extension

“.s” while the object files are typically named with the

extension “.o”

Page 23: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 23

Linking

Once the assembler has successfully compiled your source without any errors, it

generates the object code in the file specified as the argument along with parameter “-o”

when the assembler was run. Typically, object files are named using the extension “.o”

(example: hello.o or example.o). The object file is an intermediate binary

representation and cannot be executed. It needs to be linked to generate the final

executable.

The GNU linker’s executable name is ld. The linking process is used to combine

multiple object files together along with any libraries that maybe needed to generate the

final executable. In most of our assembly programs we will be using a single object file

and will not be needed additional libraries. Consequently, the parameters that you

provide to ld will be rather straightforward as shown below:

$ ld hello.o –o hello (and press ENTER)

The above shell command illustrates ld being run to link an object file named hello.o

to produce the final executable to be stored in a file named hello. Note that executable

files don’t have an extension under Linux! The linker may generate additional errors in

case the assembly source was not in-order. Consequently, it may force further changes to

the source code to resolve linker errors.

Running

Once you have successfully assembled and linked your program to produce the final

executable, it is now time to run your program. You run your program by simply typing

the final executable name at the shell prompt. Assuming that your final executable name

was hello, you can run it by simply typing hello at the shell prompt as shown below:

$ ./hello (and press ENTER) Hello World!

$

Programs seldom run correctly the first time they are run. You may have to revise your

source, re-assemble or re-compile, re-link, and re-test your program several times before

you arrive at the desired solution. Often you may need to use the GNU debugger (gdb)

to help troubleshoot your assembly program.

Note: Under Linux operating system, executable files

typically don’t have any file name extensions!

Page 24: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 24

Using GNU Debugger (gdb)

This section presents a quick overview of using the Gnu Debugger (gdb) for debugging

and troubleshooting both vanilla C and MPI-based C programs. This document is not

meant to serve as a complete reference to gdb but rather as quick start guide. For

detailed information and documentation on various features of gdb please refer to the

GNU manuals that are freely available online at:

http://www.gnu.org/software/gdb/documentation/.

Starting GDB

Once your programs have been successfully compiled (with –-gstabs2 for as or with

–g option for icc) and linked you can use the GNU Debugger (gdb) for debugging your

program. Although, gdb can be run from the shell prompt, it is much more convenient

and easier to run gdb from within emacs. In order to use gdb in emacs, you need to

perform the following steps:

1. Launch emacs and open the source file in emacs.

2. Split the emacs window to display two buffers (using Ctrl+X 2 keystrokes)

3. Now, start gdb by typing ESC-X (in one of the buffers) and gdb (in minibuffer)

and press ENTER key.

4. Now, you will be prompted in the minibuffer on how to run gdb (like: Run gdb

(like this): gdb ). Type in the name of the executable (say hello) and

press ENTER key.

5. At the end of successfully completing the above operations, your emacs window

should like the snapshot shown below.

Page 25: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 25

Using GDB

Synonymous to the working of the shell, all actions with gdb are performed by

typing suitable commands at the gdb prompt. Some of the commands supported by

gdb and their use are presented in the table below:

GDB

Command

Action Example Commands

Command Description b Set

breakpoint

(gdb)b hello.s:6

(gdb)b hello.c:6 Set breakpoint at line 6 of source

file hello.s/.c (gdb)b substr Set breakpoint at first instruction

of substr function/label. info

break List

breakpoints

(gdb) info break Lists the currently set breakpoints.

d Delete

breakpoint

(gdb) d 1 Delete breakpoint number 1.

run Start program (gdb)run Start running currently loaded

executable. n Execute next

instruction

(gdb)n Execute the next instruction

(typically used after a breakpoint is

hit) c Continue

execution

(gdb) c Continue execution (until next

breakpoint is hit) print Display data

corresponding

to the

expression

specified.

(gdb) print i Print current value of symbol i (gdb) print

(char) i Interpret value of symbol i as a

character and print it as a character. (gdb) print

(char *) i Interpret value of symbol i as

address to a (null terminated)

string and print the string. (gdb) print 0x10

+ 0x20 Evaluate expression and print

result. info Provide

requested

information

(gdb) info reg List values of all registers. (gdb) info reg

eax Print content of eax (or ax)

register. (gdb) info stack Display call stack (gdb) info

symbol 0x402031 Display symbol associated with

address 0x402031. (gdb) info line

10 Display core address for source

code file line number 10. q Quit GDB (gdb) q Stop running program and exit.

Page 26: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 26

Shortcuts for setting breakpoints

One of the easy ways to set a break point in gdb via emacs is to do the following:

1. Setup emacs in split window mode, with gdb running in one buffer and the

corresponding source file in the other buffer (as illustrated in the snapshot

above).

2. Move the cursor to the appropriate line in the C (or assembly) source file

where you would like to set a breakpoint.

3. Press Ctrl+X SPACEBAR (in the source file buffer) to set a breakpoint.

On successfully setting a breakpoint gdb will display a suitable message (such as:

Breakpoint 1 at 0x40100f: file hello.s, line 10) in the gdb buffer

indicating the breakpoint has been set.

Page 27: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 27

Using Eclipse for Assembly Programming

In order to use Eclipse for assembly programming you must be running Eclipse

version 3.2 (Europa) or higher with the C++ programmer plug-in installed in Eclipse.

You can download the C++ programmer plug-in for Linux from the Eclipse website.

Eclipse is a general purpose programming environment that can be used for programming

in a variety of languages such as: C, C++, and assembly. For each one of these languages

the steps to follow in Eclipse are a bit different. This document illustrates the steps

required to configure Eclipse for assembly language programming:

1. On running Eclipse, you will be prompted to select a workspace as shown below.

Typically, the default workspace is sufficient and you can use it. Optionally, you

can click the browse button to choose an alternative directory for storing your

files.

2. Create a new C project from Eclipse’s main menu options “File → New → C

Project” as shown below:

Page 28: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 28

3. In the project dialog, first enter the project name (for example: Assembly1) and

click the Next button.

Page 29: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 29

4. Now, click the Advanced Settings button in the dialog.

Page 30: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 30

5. In the dialog box that pops up on clicking the Advanced settings button,

perform the following operations:

a. First expand the menu tree labeled C/C++ Build.

b. Next click on the Settings menu

c. On the dialog box that pops up on the right hand side, click on the

General option under GCC Assembler.

d. Type –-gstabs2 in the Assembler flags field as shown below. This flag

instructs the assembler to include debugging information in the executable

to aid debugging assembly programs.

a

b

c

d

Page 31: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 31

6. Next, you need to setup the linker options to ignore linking with all default

libraries (as the assembly programs we are going to develop do not depend on any

libraries) as shown below:

a. First expand the menu tree labeled C/C++ Build.

b. Next click on the Settings menu

c. On the dialog box that pops up on the right hand side, click on the

General option under GCC Linker.

d. Check the following checkboxes

i. Do not use standard start files (-nostartfiles)

ii. Do not use default libraries (-nodefaultlibs)

iii. No startup or default libs (-nostdlib)

iv. No shared libraries (-static).

e. Next click the OK button.

a

b

c

d

Page 32: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 32

7. Once the Advanced Settings dialog box disappears click the Finish button to

create a project for assembly programming. Now the project is ready for assembly

programming.

Once a project has been created you may create an assembly source code in the project

using the File → New → File option. Ensure that the assembly source code file is

named with a “.s” extension (example: hw1.s, hello.s, assembly.s). Next you

may run your assembly program via Eclipse’s main “Run” menu. Optionally you may set

a breakpoint and run the program in debug mode to troubleshoot bugs in your assembly

program.

You have to follow the above 7 steps each time you start a new

assembly program in Eclipse. Ensure that you follow all of the seven

steps. You can always change the properties of the project at any time by

right clicking on the project and selecting the “Properties…” option.

Page 33: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 33

Compiling and Running non-MPI C Programs

Once you have typed out your C program source code in emacs and saved it to disk, you

are now ready to compile it to obtain the final executable. Once you have successfully

generated the executable you may run it from the shell prompt. Optionally you can use

the executable to debug and troubleshoot semantic errors via the GNU debugger (gdb).

Note that this process typically proceeds in an iterative manner as illustrated in the figure

shown below:

1. The first step involves running emacs (a text editor), typing your program into an

emacs buffer, and saving it (using a suitable extension such as: .c) to a file.

2. Next you run the Intel C compiler (icc) on the C source file created earlier. If

the compiler generates errors, you need to suitably modify your program using

emacs. The compiler typically generates the final executable. However, you may

also use it to generate an intermediate object file (named using a “.o” extension).

3. Run the binary from the shell prompt and verify your program is working

correctly. You may need the use of the GNU debugger (gdb) for debugging your

assembly source. If you find any logic errors you need to repeat steps 1 through

3.

A brief description of how to use icc presented in the following subsections. The use of

gdb is illustrated in further down in this document.

C Source

Typed in emacs

and saved to disk.

Compile by running

icc (or mpicc) at the

shell prompt to get

executable file

Run the executable

file from the shell

prompt and verify the

functionality via

through testing

Fix code for syntax errors

Fix code for semantic

errors

Optionally Run the

generated binary

executable via gdb. Fix code for logic errors

Page 34: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 34

Compiling using ICC

Compiling is the process of translating your C language source code into a binary that

can be readily executed by the microprocessor. This task is performed by running the

Intel C compiler called icc from the shell prompt. The C source file to be compiled and

the destination executable file name must be specified as command-line parameters to

gcc. An example of running icc on a C source file named hello.c to generate the

final executable file named hello is illustrated below:

$ icc –g –O3 –Wall hello.c –o hello (and press ENTER)

All options to icc begin with a dash (-) character. The options used in the above

example command line are:

The “-g” is an option to gcc that instructs it to place debugging information into

the executable file. This option is needed in order to debug (or troubleshoot)

programs (if necessary) using the GNU debugger (gdb). Details of using gdb

are presented in a different section.

The “-O3” (capital O) option instructs icc to perform aggressive optimizations

to your program. If you are trying to bug fix your program then do not use this

option. Use this option only after you have thoroughly verified correct operation

of your program for performance measurements.

The “-Wall” generates all the warnings about your source code. It is always a

good idea to compile your program with the –Wall option because many of these

warnings flag potential problems with your program. Ideally your program should

compile without generating a single warning.

The “-o” option indicates that the output from icc, namely the executable file,

must be placed in a file named hello.

Note that the compiler generates suitable errors. If the compiler reports errors you have

to appropriately modify the source code in emacs and run the compiler again on the

modified source code.

Note: C language source files are named with the extension “.c”.

Intermediate object files (if any) are typically named with the extension

“.o”. Final executables do not have any extensions associated with

them.

Page 35: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 35

Generating & Linking Object files

Any serious program will consist of multiple source files that have to combined together

to form a final executable. Furthermore, they may involve the use of external, third party

libraries. In order to effectively handle multiple source files, icc is often used to

generate intermediate binary files called object files as shown below:

$ icc –g –Wall hello.c –o hello.o (and press ENTER)

Object files are not final executable files and cannot be run until they are linked. Linking

is the process of combining one or more object files along with necessary libraries to

generate the final executable. It is performed using icc by simply specifying only the

object files as command-line parameters as shown below. Note that linking is internally

performed by icc even if you don’t take the intermediate step of generate object files.

$ icc part1.o part2.o part3.o –o full (and press ENTER)

Running

Once you have successfully compiled and linked your program to produce the final

executable, it is now time to run your program. You run your program by simply typing

the final executable name at the shell prompt. Assuming that your final executable name

was hello, you can run it by simply typing hello at the shell prompt as shown below:

$ ./hello (and press ENTER) Hello World!

$

Programs seldom run correctly the first time they are run. You may have to revise your

source, or re-compile, re-link, and re-test your program several times before you arrive at

the desired solution. Often you may need to use the GNU debugger (gdb) to help

troubleshoot your assembly program.

Page 36: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 36

Compiling and Running MPI-based C Programs

Once you have typed out your MPI-based C program source code in emacs and saved it

to disk, you are now ready to compile it to obtain the final executable. Once you have

successfully generated the executable you may run it from the shell prompt using PBSpro

script. Optionally you can use the executable to debug and troubleshoot semantic errors

via the GNU debugger (gdb). Note that this process typically proceeds in an iterative

manner as illustrated in the figure shown below:

1. The first step involves running emacs (a text editor), typing your program into an

emacs buffer, and saving it (using a suitable extension such as: .c) to a file.

2. Next you run the mpicc command (that internally just runs the Intel C compiler

icc with suitable command line parameters to include the MPI library) on the C

source file created earlier. If the compiler generates errors, you need to suitably

modify your program using emacs. The compiler typically generates the final

executable. However, you may also use it to generate an intermediate object file

(named using an “.o” extension).

3. Create a script for PBSpro (the cluster management software system) to run your

program using a given number of compute nodes on the Redhawk cluster.

4. Submit a job to run your program via the PBSpro on the cluster. The outputs from

your program will be either displayed on the screen (in interactive mode) or

dumped to a file in offline mode. Prefer the offline mode over interactive mode.

MPI/C Source

Typed in emacs

and saved to disk.

Compile by running

mpicc at the shell

prompt to get

executable file

Create a PBS script to

run the MPI program

(on a given number of

compute nodes)

Fix code for syntax errors

Fix code for semantic

errors

Submit the job via

PBSpro (using qsub

command) and verify

program operation. Fix code for logic errors

Page 37: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 37

A brief description of how to use icc was presented earlier. Using mpicc and options

to mpicc are identical to icc because mpicc merely calls icc internally with

additional parameters. The command line for compiling an MPI program using mpicc is

shown below for completeness:

$ mpicc –g –O3 –Wall hello.c –o hello (and press ENTER)

The above command is compiling a source file named hello.c (that uses MPI library)

into a binary executable called hello (note no extensions are used) with debugging

information (because –g flag has been specified), fully optimizations (because –O3

option has been specified), and mpicc will report detailed warnings (if any) regarding

the source program (since –Wall option has been specified)

Working with PBSpro

Refer to the excellent online documentation provided by Miami University’s Research

Computing Services (RCS) regarding the usage of PBS via the following URL: Working

with batch jobs on the Redhawk Cluster. However, for completeness a short PBS script is

shown below and the operation of each line is annotated in the table further below.

#!/bin/bash

#PBS -N hello

#PBS -l walltime=1:00:00

#PBS -l mem=2GB

#PBS -l nodes=3:ppn=1

#PBS -S /bin/bash

cd /home/raodm/hackArea/mpi/

mpiexec -n 3 ./hello

Here is a brief description of the operations performed by each line of the above script:

Line# Script commands Description

1 #!/bin/bash -l Tells PBS that the script follows bash shell

conventions

2 #PBS -N hello Name of this PBS script is hello.

3 #PBS -l walltime=1:00:00 Tell PBS we need 1 hour of run time

4 #PBS -l mem=2GB Tell PBS our program will need a total (all

processes added up) 2 GB of main memory to run.

5

#PBS -l nodes=3:ppn=1 Tell PBS we need 3 compute nodes with one

processor reserved for our job on each node (ppn =

processors per node)

6 #PBS -S /bin/bash Tell PBS we are using the bash shell.

7

cd /home/raodm/mpi/ Change current working directory from home

directory (default) to the directory where our

program is stored.

8 mpiexec -n 3 ./hello Use mpiexec command to run 3 instances of the

MPI-based hello program

Page 38: Assembly Programming under Linux - newrandom.compublic.newrandom.com/college/fall08/csa278/handouts/LinuxEviron… · Assembly Programming under Linux D. M. Rao Page 3 Accessing a

Assembly Programming under Linux D. M. Rao

Page 38

Here are a few commonly used PBS commands that you will find useful when working

on the Redhawk cluster.

Command Description Example usage

qsub Submit a new job to the PBS

system.

$ qsub hello.sh

qsub -I Submit a new interactive job

to the PBS system

$ qsub –I hello.sh

qstat Show status of jobs currently

running on the cluster.

$ qstat

$ qstat –u raodm

qdel Delete (or abort) a currently

running job on the cluster

given its job ID. You may

have to use qstat to

determine job ID

$ qdel 12345

Working/Managing software Packages on Redhawk cluster

Refer to the excellent online documentation provided by Miami University’s Research

Computing Services (RCS) regarding management and working with software packages

already installed on the Redhawk cluster via the following URL: Working with software

on the RedHawk Cluster.