-
Introduction
-
The following is designed familiarize the reader with programming in x86 (at&t
Style, that produced by GCC) assembly under Linux and how to interface assembly
And higher-level language code (I. e. c). The tutorial will also briefly cover
Debugging your assembly using GDB.
This tutorial requires the following:
- An i386 Family PC running Linux
- GCC, the gnu c-Compiler
- GDB, the GNU Debugger command line Debugger
The tutorial was developed on and tested with GCC version 2.95.4 and GDB 19990928 under Linux kernel 2.4.9
I highly recommend working through this tutorial
""
And "GDB"
Documentation close at hand.
-
Source code
-
The process begins with a source code program. For example, hello. c
/* hello.c */
#include
main()
{
printf("hello/n").
}
Which, when compiled and executed, prints a message
Linuxbox> gcc-O hello. c
Linuxbox>./Hello
Hello
The actual compilation process can be viewed by including-V
Option
Linuxbox> gcc-v-o hello. c
Reading specs from/usr/lib/GCC-lib/i386-linux/2.95.4/specs
GCC version 2.95.4 20010902 (Debian prerelease)
/Usr/lib/GCC-lib/i386-linux/2.95.4/cpp0-Lang-C-v-d1_gnuc __= 2
-D1_gnuc_minor __= 95-d1_elf _-dunix-D _ i386 _-dlinux-d1_elf __
-D _ UNIX _-D _ i386 _-D _ Linux _-D _ Unix-D _ Linux-asystem (POSIX)
-Acpu (i386)-amachine (i386)-di386-D _ i386-D _ i386 _ Hello. c
/Tmp/cccgcfmg. I
Gnu cpp version 2.95.4 20010902 (Debian prerel.pdf) (i386 Linux/Elf)
# Include "..." search starts here:
# Include <...> search starts here:/usr/local/include
/Usr/lib/GCC-lib/i386-linux/2.95.4/include/usr/include
End of search list.
The following default directories have been omitted from the search
Path:/usr/lib/GCC-lib/i386-linux/2.95.4/.../../include/g ++-3
/Usr/lib/GCC-lib/i386-linux/2.95.4/.../i386-linux/include
End of omitted list./usr/lib/GCC-lib/i386-linux/2.95.4/PC3
/Tmp/cccgcfmg. I-quiet-dumpbase hello. C-version-O/tmp/cci5cjce. s
Gnu c version 2.95.4 20010902 (Debian prerelease) (i386-linux) compiled
By gnu c version 2.95.4 20010902 (Debian prerelease). As-v-QY-o
/Tmp/cc2lti5k. O/tmp/cci5cjce. s
GNU extends er version 2.11.90.0.31 (i386-linux) using BFD version
2.11.90.0.31/usr/lib/GCC-lib/i386-linux/2.95.4/collect2-M elf_i386
-Dynamic-linker/lib/ld-linux.so.2-O hello/usr/lib/crt1.o
/Usr/lib/crti. O/usr/lib/GCC-lib/i386-linux/2.95.4/crtbegin. o
-L/usr/lib/GCC-lib/i386-linux/2.95.4/tmp/cc2lti5k. O-lgcc-LC-lgcc
/Usr/lib/GCC-lib/i386-linux/2.95.4/crtend. O/usr/lib/crtn. o
The process:
- Preprocess the source-codeCPP
. (The results of this may be
Viewed with-E
Option. I. e.Gcc-e hello. c
)
- Compile the CodeCC
Into assembly code. (This Asembly may be
Viewed using-S
Option.Gcc-s hello. cGenerates hello. s)
- The Assembly Language is
Note that the Code usesAt&t syntax
Of developing an assembly program under Linux is somewhat different
From development under NT. In order to accommodate Object Oriented ages
Which require the compiler to create constructor and destructor methods which
Execute before and after the execution of "Main", the GNU Development Model
Embeds user code within a wrapper of system code.
In other words, the user's "Main" is treated as a function call.
An advantage of this is that user is not required to initialize segment
Registers, though user code must obey some function requirements.
-
The Code
-
The following is the Linux version of the average temperature program. It will
Be referred to as "average. s". Note: assembly language programs shold
Use the ". s" suffix.
/* linux version of AVTEMP.ASM CS 200, fall 1998 */
.data /* beginning of data segment */
/* hi_temp data item */
.type hi_temp,@object /* declare as data object */
.size hi_temp,1 /* declare size in bytes */
hi_temp:
.byte 0x92 /* set value */
/* lo_temp data item */
.type lo_temp,@object
.size lo_temp,1
lo_temp:
.byte 0x52
/* av_temp data item */
.type av_temp,@object
.size av_temp,1
av_temp:
.byte 0
/* segment registers set up by linked code */
/* beginning of text(code) segment */
.text
.align 4 /* set 4 double-word alignment */
.globl main /* make main global for linker */
.type main,@function /* declare main as a function */
main:
pushl %ebp/* function requirement */
movl %esp,%ebp /* function requirement */
movb hi_temp,%al
addb lo_temp,%al
movb $0,%ah
adcb $0,%ah
movb $2,%bl
idivb %bl
movb %al,av_temp
leave/* function requirement */
ret/* function requirement */
-
Assembly instructions
This code may be assembled with the following command:
As-A -- maid-O average. O average. s
The "-a" option prints a memory listing during assembly. This output
Gives the location variables and Code with respect to the beginnings
Of the data and code segments. "-- maid"
Places debugging information in the executable (used by GDB ).
"-O" specifies average. O as the output file name (the default is A. Out,
Which is confusing since
File is not executable .)
The object file (average. O) can then be linked to the Linux wrapper code
In order to create an executable. These files are crt1.o, crti. O and
Crtn. O. crt1.o and crti. o provide initialization code and crtn. o does cleanup.
These shoshould all be located in "/usr/lib" be may be elsewere On some systems.
They, and their source, might be located by executing the following find
Command:
Find/-name "CRT *"-print
The link command is the following:
LD-M elf_i386-static/usr/lib/crt1.o/usr/lib/crti. O-LC average. O/usr/lib/crtn. o
"-M elf_i386" instructs the linker to use the ELF File Format. "-static"
Cause static rather than Dynamic Linking to occur. And "-LC" links in
Standard C libraries (libc. a). It might be necessary to include
"-I/libdirectory" in the invocation for LD to find the C library.
It will be necessary to change the mode of the resulting object file
"Chmod + X./A. Out ".
It shoshould now be possible to execute the file. But, of course,
There will be no output.
I recommend placing the above COMMANDS IN A makefile
.
-
Debugging
-
The "-- maid" option given to the aggreger allows the assembly program
To be debugged under GDB.
The first step is to invoke GDB:
GDB./A. Out
GDB shoshould start with the following message:
[bjorn@pomade src]$ gdb ./a.out
GNU gdb 4.17
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb)
The "l" command will list the program sourcecode.
(gdb) l
1 /* linux version of AVTEMP.ASM CS 200, fall 1998 */
2 .data /* beginning of data segment */
3
4 /* hi_temp data item */
5 .type hi_temp,@object /* declare as data object */
6 .size hi_temp,1 /* declare size in bytes */
7 hi_temp:
8 .byte 0x92 /* set value */
9
10 /* lo_temp data item */
(gdb)
The first thing to do is set a breakpoint so it will be possible to step
through the code.
(gdb) break main
Breakpoint 1 at 0x80480f7
(gdb)
This sets a breakpoint at the beginning of main. Now run the program.
(gdb) run
Starting program: /home/bjorn/src/./a.out
Breakpoint 1, main () at average.s:31
31 movb hi_temp,%al
Current language: auto; currently asm
(gdb)
values in registers can be checked with either "info registers"
(gdb) info registers
eax 0x8059200 134582784
ecx 0xbffffd94 -1073742444
edx 0x0 0
ebx 0x8097bf0 134839280
esp 0xbffffdd8 0xbffffdd8
ebp 0xbffffdd8 0xbffffdd8
esi 0x1 1
edi 0x8097088 134836360
eip 0x80480f7 0x80480f7
eflags 0x246 582
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x2b 43
gs 0x2b 43
(gdb)
...or "p/x $eax" which prints the value in the EAX register in hex. The "e"
in front of the register name indicates a 32 bit register. The Intel x86
family has included "extended" 32 bit registers since the 80386. These E
registers are to the X registers as the L and H are to the X registers.
Linux also uses a "flat" and protected memory model rather that segmentation,
thus the EIP stores the entire current address.
(gdb) p/x $eax
$4 = 0x8059200
(gdb)
The "p" command prints, "/x" indicates the output should be in hexadecimal.
type "s" or "step" to step to the next instruction.
(gdb) step
32 addb lo_temp,%al
(gdb)
notice that 92H has been loaded into the least significant bit of the EAX
register (ie. the AL register) by the movb instruction.
(gdb) p/x $eax
$6 = 0x8059292
(gdb)
And we continue stepping through the program....
(gdb) s
33 movb $0,%ah
(gdb) s
34 adcb $0,%ah
(gdb) s
35 movb $2,%bl
(gdb) s
36 idivb %bl
(gdb) s
37 movb %al,av_temp
(gdb) s
38 leave
and if we examine the EAX register and the variable av_temp after
the final movb instruction, we see that they are set to the correct
value, 72H.
(gdb) p/x $eax
$9 = 0x8050072
(gdb) p/x av_temp
$10 = 0x72
(gdb)
Note that during stepping the listed instruction is the one about to be
executed.