打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Running nasm and gdb 调试nasm汇编程序

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 0x05 0x00 0x00 0x00 0xa3 0x24 0x960x8048378 : 0x04 0x08 0xff 0x35 0x24 0x96 0x04 0x080x8048380 :0xff 0x35 0xe8 0x94 0x04 0x08 0xff 0x350x8048388 :0xe4 0x94 0x04 0x08 0x68 0x02 0x95 0x040x8048390 :0x08 0x68 0xec 0x94 0x04 0x08 0xe8 0xfd0x8048398 :0xfe 0xff 0xff 0x83 0xc4 0x14 0xa1 0xe40x80483a0 : 0x94 0x04 0x08 0x03 0x05 0xe8 0x94 0x040x80483a8 :0x08 0xa3 0x24 0x96 0x04 0x08 0xff 0x350x80483b0 :0x24 0x96 0x04 0x08 0xff 0x35 0xe8 0x940x80483b8 :0x04 0x08 0xff 0x35 0xe4 0x94 0x04 0x080x80483c0 :0x68 0x08 0x95 0x04 0x08 0x68 0xec 0x940x80483c8 :0x04 0x08(gdb) info registerseax 0x1 1ecx 0x42130f28 1108545320edx 0xbffffc0c -1073742836ebx 0x4213030c 1108542220esp 0xbffffb9c 0xbffffb9cebp 0xbffffbd8 0xbffffbd8esi 0x40013020 1073819680edi 0xbffffc04 -1073742844eip 0x8048370 0x8048370eflags 0x296 662cs 0x23 35ss 0x2b 43ds 0x2b 43es 0x2b 43fs 0x0 0gs 0x0 0fctrl 0x37f 895fstat 0x0 0ftag 0xffff 65535fiseg 0x0 0fioff 0x0 0foseg 0x0 0fooff 0x0 0fop 0x0 0xmm0 {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 0x1f80 8064orig_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 pointmov	edx,len		; arg3, length of string to printmov	ecx,msg		; arg2, pointer to stringmov	ebx,1		; arg1, where to write, screenmov	eax,4		; write command to int 80 hexint	0x80		; interrupt 80 hex, call kernelmov	ebx,0		; exit code, 0=normalmov	eax,1		; exit command to kernelint	0x80		; interrupt 80 hex, call kernel

    Go to top

    Last updated 10/10/03

    本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    【热】打开小程序,算一算2024你的财运
    ld
    NASM x86汇编入门指南
    xcode反汇编调试iOS模拟器程序
    汇编语言学习篇1——DOSBox、汇编语言、汇编器的认识
    HowTo: Inline Assembly & SSE: Vector normaliz...
    Linux环境下的堆栈
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服