Chapter 3: Your First Programs


In this chapter you will learn the process for writing and building Linux assembly-language programs. In addition, you will learn the structure of assembly-language programs, and a few assembly-language commands. As you go through this chapter, you may want to refer also to Appendix B and Appendix F.

These programs may overwhelm you at first. However, go through them with diligence, read them and their explanations as many times as necessary, and you will have a solid foundation of knowledge to build on. Please tinker around with the programs as much as you can. Even if your tinkering does not work, every failure will help you learn.

Entering in the Program

Okay, this first program is simple. In fact, it's not going to do anything but exit! It's short, but it shows some basics about assembly language and Linux programming. You need to enter the program in an editor exactly as written, with the filename exit.s. The program follows. Don't worry about not understanding it. This section only deals with typing it in and running it. In the Section called Outline of an Assembly Language Program we will describe how it works.

  #PURPOSE:  Simple program that exits and returns a  #          status code back to the Linux kernel  #  #INPUT:    none  #  #OUTPUT:   returns a status code. This can be viewed  #          by typing  #  #          echo $?  #  #          after running the program  #  #VARIABLES:  #          %eax holds the system call number  #          %ebx holds the return status  #  .section .data  .section .text  .globl _start _start:  movl $1, %eax      # this is the linux kernel command                     # number (system call) for exiting                     # a program  movl $0, %ebx      # this is the status number we will                     # return to the operating system.                     # Change this around and it will                     # return different things to                     # echo $?  int $0x80          # this wakes up the kernel to run                     # the exit command 

What you have typed in is called the source code. Source code is the human-readable form of a program. In order to transform it into a program that a computer can run, we need to assemble and link it.

The first step is to assemble it. Assembling is the process that transforms what you typed into instructions for the machine. The machine itself only reads sets of numbers, but humans prefer words. An assembly language is a more human-readable form of the instructions a computer understands. Assembling transforms the human-readable file into a machine-readable one. To assembly the program type in the command

 as exit.s -o exit.o 

as is the command which runs the assembler, exit.s is the source file, and -o exit.o tells the assemble to put its output in the file exit.o. exit.o is an object file. An object file is code that is in the machine's language, but has not been completely put together. In most large programs, you will have several source files, and you will convert each one into an object file. The linker is the program that is responsible for putting the object files together and adding information to it so that the kernel knows how to load and run it. In our case, we only have one object file, so the linker is only adding the information to enable it to run. To link the file, enter the command

 ld exit.o -o exit 

ld is the command to run the linker, exit.o is the object file we want to link, and -o exit instructs the linker to output the new program into a file called exit.[1] If any of these commands reported errors, you have either mistyped your program or the command. After correcting the program, you have to re-run all the commands. You must always re-assemble and re-link programs after you modify the source file for the changes to occur in the program. You can run exit by typing in the command

 ./exit 

The ./ is used to tell the computer that the program isn't in one of the normal program directories, but is the current directory instead[2]. You'll notice when you type this command, the only thing that happens is that you'll go to the next line.

That's because this program does nothing but exit. However, immediately after you run the program, if you type in

 echo $? 

It will say 0. What is happening is that every program when it exits gives Linux an exit status code, which tells it if everything went all right. If everything was okay, it returns 0. UNIX programs return numbers other than zero to indicate failure or other errors, warnings, or statuses. The programmer determines what each number means. You can view this code by typing in echo $?. In the following section we will look at what each part of the code does.

[1]If you are new to Linux and UNIX , you may not be aware that files don't have to have extensions. In fact, while Windows uses the .exe extension to signify an executable program, UNIX executables usually have no extension.

[2]. . refers to the current directory in Linux and UNIX systems.




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net