Running nasm and gdb

来源:互联网 发布:淘宝入驻平台 编辑:程序博客网 时间:2024/06/17 11:59

转载:http://www.csee.umbc.edu/portal/help/nasm/nasm.shtml#gdb

Specifically for nasm on Linux, IA-32, with gcc

Contents

  • assembling and running nasm code
  • running the debugger, gdb
  • hello.asm
  • nasmdoc.txt manual
  • assembling and running nasm

    There are many ways to run nasm. This section chooses touse only one way for one system. Modify to suit your needs.This is for nasm running on Linux on an Intel IA-32 computer,e.g. 386, 486, pentium. gcc, the "C" compilation system andlibraries are used. The next section will cover gdb, thedebugger that comes with gcc.   Use your favorite editor to create a nasm assembly language file.e.g. hello.asm shown below.Type the command:   nasm -f elf -l hello.lst  hello.asmIf your program had no assembly time errors, nothing displays.If you had assembly time errors, fix them and repeat the command.Two files are created by the command  hello.o  and  hello.lstVerify this by typing the command   ls -lType the command:   gcc -o hello  hello.oIf your program had no link time errors, nothing displays.If you had link time errors, fix the  .asm  file and repeat the command.There is now an executable file   helloVerify this by typing the command   ls -lExecute (run) your program by typing the command:   helloThe output should be displayed.That is all there is to do, assuming your output is what youexpected. Most of the time, the output will be wrong.Start by finding the first error in your output, then lookin the  .asm  file to check the code that was supposed togenerate the good output. You may also want to check the  .lstfile in the same area. The  .lst file shows addresses.instructions and data as numbers. This is harder to read, yetmay explain the error.If you can not find the error (bug) then use the next section,gdb, to help debug your program.

    running the debugger, gdb

    The gdb debugger is designed for interactive use.gdb is started with the command   gdb For this session, the intarith.asm  from the samples is bring used.The commands for a sample session are shown, as typed, then explained.gdb intarithbreak mainrunset disassembly-flavor inteldisassemble mainx/90xb maininfo registersprint/x $espprint/x $eaxnextiprint/x $espprint/x $eaxnextinfo floatinfo stackqyFor all of the samples in this WEB page, "main" is used becauseit is the standard gcc name for the main program. The debuggerworks for "C" programs, best if the  -g3  option is used.At the time this WEB page was prepared nasm would not adddebugging information, even with the  -g  option.The first command to gdb,  when the (gdb)  prompt appears is   break mainThis sets a breakpoint on the address of "main" the start ofthe program being debugged.The next command runs the program until the breakpoint is reached   runFor use with nasm, it is best to set the flavor to intel   set disassembly-flavor intelThen, the disassembly can be seen   disassemble mainTo see the raw bytes in storage that are the program   x/90xb mainThe number 90 is just a sample. By looking at the disassembly,the number of bytes  main  could be determined.To see what is in the integer registers   info registersNote that both hexadecimal and integer values are shownTo look at specific registers, use  print/x or print/d witha dollar sign in front of the register name.   print/x $esp   print/x $eaxTo step one instruction   nextiThen look at registers again   print/x $esp   print/x $eaxIf there were line number debug information in the executable file,then  "next"  would setp one line, rather than one instruction.For this case, "next" runs the program to completion.   nextFor demonstration, the floating point registers may be displayed   info floatThe stack can be displayed   info stackTo quit gdb, the single letter "q" is the complete command   qAnd, gdb requires the confirmation to quit, using a "y".   yThe output from the above, slightly edited to fit on linesand with a blank line before each command,is intarith.gdbthe source file is intarith.asmthe assembler list file is intarith.lstNote that the assembler list file is the most useful file to havewhen running the debugger.>gdb intarith > intarith.gdbGNU gdb Red Hat Linux (5.2-2)Copyright 2002 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome 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) break mainBreakpoint 1 at 0x8048370(gdb) runStarting program: /afs/umbc.edu/users/s/q/squire/home/cs313/intarith Breakpoint 1, 0x08048370 in main ()(gdb) set disassembly-flavor intel(gdb) disassemble mainDump of assembler code for function main:0x8048370 :mov    eax,0x50x8048375 :mov    ds:0x8049624,eax0x804837a :push   ds:0x80496240x8048380 :push   ds:0x80494e80x8048386 :push   ds:0x80494e40x804838c :push   0x80495020x8048391 :push   0x80494ec0x8048396 :call   0x8048298 0x804839b :add    esp,0x14End of assembler dump.(gdb) x/90xb main0x8048370 :   0xb8  0x050x000x000x000xa30x240x960x8048378 : 0x04  0x080xff0x350x240x960x040x080x8048380 :0xff  0x350xe80x940x040x080xff0x350x8048388 :0xe4  0x940x040x080x680x020x950x040x8048390 :0x08  0x680xec0x940x040x080xe80xfd0x8048398 :0xfe  0xff0xff0x830xc40x140xa10xe40x80483a0 : 0x94  0x040x080x030x050xe80x940x040x80483a8 :0x08  0xa30x240x960x040x080xff0x350x80483b0 :0x24  0x960x040x080xff0x350xe80x940x80483b8 :0x04  0x080xff0x350xe40x940x040x080x80483c0 :0x68  0x080x950x040x080x680xec0x940x80483c8 :0x04  0x08(gdb) info registerseax            0x11ecx            0x42130f281108545320edx            0xbffffc0c-1073742836ebx            0x4213030c1108542220esp            0xbffffb9c0xbffffb9cebp            0xbffffbd80xbffffbd8esi            0x400130201073819680edi            0xbffffc04-1073742844eip            0x80483700x8048370eflags         0x296662cs             0x2335ss             0x2b43ds             0x2b43es             0x2b43fs             0x00gs             0x00fctrl          0x37f895fstat          0x00ftag           0xffff65535fiseg          0x00fioff          0x00foseg          0x00fooff          0x00fop            0x00xmm0           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm1           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm2           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm3           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm4           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm5           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm6           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}xmm7           {f = {0x0, 0x0, 0x0, 0x0}}{f = {0, 0, 0, 0}}mxcsr          0x1f808064orig_eax       0xffffffff-1(gdb) print/x $esp$1 = 0xbffffb9c(gdb) print/x $eax$2 = 0x1(gdb) nexti0x08048375 in main ()(gdb) print/x $esp$3 = 0xbffffb9c(gdb) print/x $eax$4 = 0x5(gdb) nextSingle stepping until exit from function main, which has no line number information.0x42017589 in __libc_start_main () from /lib/i686/libc.so.6(gdb) info float  R7: Empty   0x00000000000000000000  R6: Empty   0x00000000000000000000  R5: Empty   0x00000000000000000000  R4: Empty   0x00000000000000000000  R3: Empty   0x00000000000000000000  R2: Empty   0x00000000000000000000  R1: Empty   0x00000000000000000000=>R0: Empty   0x00000000000000000000Status Word:         0x0000                                                                   TOP: 0Control Word:        0x037f   IM DM ZM OM UM PM                       PC: Extended Precision (64-bits)                       RC: Round to nearestTag Word:            0xffffInstruction Pointer: 0x00:0x00000000Operand Pointer:     0x00:0x00000000Opcode:              0x0000(gdb) info stack#0  0x42017589 in __libc_start_main () from /lib/i686/libc.so.6(gdb) qThe program is running.  Exit anyway? (y or n) y   

    hello.asm complete program

      The nasm source code is hello.asm  This demonstrates basic text output to a screen.;  hello.asm  a first program for nasm for Linux, Intel, gcc;; assemble:nasm -f elf -l hello.lst  hello.asm; link:gcc -o hello  hello.o; run:        hello ; output is:Hello World SECTION .data; data sectionmsg:db "Hello World",10; the string to print, 10=crlen:equ $-msg; "$" means "here"; len is a value, not an addressSECTION .text; code section        global main; make label available to linker main:; standard  gcc  entry pointmovedx,len; arg3, length of string to printmovecx,msg; arg2, pointer to stringmovebx,1; arg1, where to write, screenmoveax,4; write command to int 80 hexint0x80; interrupt 80 hex, call kernelmovebx,0; exit code, 0=normalmoveax,1; exit command to kernelint0x80; interrupt 80 hex, call kernel   

    Last updated 10/10/03

    原创粉丝点击