西游记黎山老母第23集:Running nasm and gdb 调试nasm汇编程序

来源:百度文库 编辑:九乡新闻网 时间:2024/05/01 15:57:08

Running nasm and 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 commandls -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 commandls -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 commandgdb 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 isbreak 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 reachedrunFor use with nasm, it is best to set the flavor to intelset disassembly-flavor intelThen, the disassembly can be seendisassemble mainTo see the raw bytes in storage that are the programx/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 registersinfo 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 $espprint/x $eaxTo step one instructionnextiThen look at registers againprint/x $espprint/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 displayedinfo floatThe stack can be displayedinfo stackTo quit gdb, the single letter "q" is the complete commandqAnd, 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/intarithBreakpoint 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 0x80482980x804839b :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 floatR7: Empty 0x00000000000000000000R6: Empty 0x00000000000000000000R5: Empty 0x00000000000000000000R4: Empty 0x00000000000000000000R3: Empty 0x00000000000000000000R2: Empty 0x00000000000000000000R1: Empty 0x00000000000000000000=>R0: Empty 0x00000000000000000000Status Word: 0x0000TOP: 0Control Word: 0x037f IM DM ZM OM UM PMPC: 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.asmThis 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 WorldSECTION .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 sectionglobal main; make label available to linkermain:; 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

    Go to top

    Last updated 10/10/03