debugging assembly program

  • Follow


Here is a simple helloworld program :

------------------------------------------------------------------------
------------------------------------------------------------------------

section .text				;section declaration

			;we must export the entry point to the ELF linker or
    global _start	;loader. They conventionally recognize _start as
their
			;entry point. Use ld -e foo to override the default.

_start:

;write our string to stdout

        mov     edx,len ;third argument: message length
        mov     ecx,msg ;second argument: pointer to message to write
        mov     ebx,1   ;first argument: file handle (stdout)
        mov     eax,4   ;system call number (sys_write)
        int     0x80	;call kernel



;and exit

	mov	ebx,0	;first syscall argument: exit code
        mov     eax,1   ;system call number (sys_exit)
        int     0x80	;call kernel

section .data				;section declaration

msg     db      "Hello, world!",0xa	;our dear string
len     equ     $ - msg                 ;length of our dear string


------------------------------------------------------------------------
------------------------------------------------------------------------

How do I debug this program ? Is there any IDE available for the
same ? How do I compute the length of the string dynamically ?

0
Reply crypter 9/23/2008 3:34:30 AM

On Mon, 22 Sep 2008 20:34:30 -0700 (PDT)
crypter  <spamtrap@crayne.org> wrote:

> How do I debug this program ?

With gdb. However, this debugger needs a little bit of help to work
well with assembly language. I place the following commands in .gdbinit
[note the leading dot] in my home directory.

set disassembly-flavor intel
define a32n
ni
x /i $pc
printf "eax=0x%.8x ebx=0x%.8x ecx=0x%.8x
edx=0x%.8x\n",$eax,$ebx,$ecx,$edx
printf "esi=0x%.8x edi=0x%.8x ebp=0x%.8x
esp=0x%.8x\n",$esi,$edi,$ebp,$esp
end
define a32i
si
x /i $pc
printf "eax=0x%.8x ebx=0x%.8x ecx=0x%.8x
edx=0x%.8x\n",$eax,$ebx,$ecx,$edx
printf "esi=0x%.8x edi=0x%.8x ebp=0x%.8x
esp=0x%.8x\n",$esi,$edi,$ebp,$esp
end
define a32r
x /i $pc
printf "eax=0x%.8x ebx=0x%.8x ecx=0x%.8x
edx=0x%.8x\n",$eax,$ebx,$ecx,$edx
printf "esi=0x%.8x edi=0x%.8x ebp=0x%.8x 
esp=0x%.8x\n",$esi,$edi,$ebp,$esp
end

> How do I compute the length of the string dynamically

You scan the string for the end of string terminator. For example, here
is a string length subroutine which works for null terminated strings:

;---------------------------------------------------------------------------
;entry	[esi]	asciiz string
;return eax	string length
strlen:
	push	edi
	push	ecx
	xor	eax,eax	;clear search byte
	mov	edi,esi	;current search position
	mov	ecx,255	;max length
	repnz scasb
	mov	eax,edi	;address of null byte +1
	sub	eax,esi	;start of string
	dec	eax	;don't count terminator
	pop	ecx
	pop	edi
	ret

-- 
Chuck 
http://www.pacificsites.com/~ccrayne/charles.html

0
Reply Charles 9/23/2008 4:56:50 AM


"crypter" <spamtrap@crayne.org> wrote in message 
news:74cce31f-6b77-4432-a62e-fcd1ae670d7d@79g2000hsk.googlegroups.com...
> Here is a simple helloworld program :
>
> ------------------------------------------------------------------------
> ------------------------------------------------------------------------
>
> section .text ;section declaration
>
> ;we must export the entry point to the ELF linker or
>    global _start ;loader. They conventionally recognize _start as
> their
> ;entry point. Use ld -e foo to override the default.
>
> _start:
>
> ;write our string to stdout
>
>        mov     edx,len ;third argument: message length
>        mov     ecx,msg ;second argument: pointer to message to write
>        mov     ebx,1   ;first argument: file handle (stdout)
>        mov     eax,4   ;system call number (sys_write)
>        int     0x80 ;call kernel
>
>
>
> ;and exit
>
> mov ebx,0 ;first syscall argument: exit code
>        mov     eax,1   ;system call number (sys_exit)
>        int     0x80 ;call kernel
>
> section .data ;section declaration
>
> msg     db      "Hello, world!",0xa ;our dear string
> len     equ     $ - msg                 ;length of our dear string
>
>
> ------------------------------------------------------------------------
> ------------------------------------------------------------------------
>
> How do I debug this program ?
You should run the assembler with the -g option to generate debug info.
 gas -g -o foo foo.s
You start the gnu debugger
 gdb foo

> Is there any IDE available for the same ?
Sorry, gdb ist a command line program.

> How do I compute the length of the string dynamically ?
You will have to write a code fragment that reads each byte and check 
for the trailing 0 byte. To reuse the code you should prefer a 
function. Even if this implementation may fit your needs in the code 
above, you should check, if the interface is correctly designed, i.e. C 
style functions return their values in EAX.

; entry:
;    ecx - address of string
; return:
;    edx - length of string
; changed registers:
;    none, except return register
stringlen:
    mov edx,0
    push eax
    push ecx
_len1:
    mov al,[ecx]
    or al,al
    jz _len2
    inc edx
    inc ecx
    jmp _len1
_len2:
    pop ecx
    pop eax
    ret

/Helge

0
Reply Helge 9/23/2008 4:57:38 AM

On Sep 22, 8:34�pm, crypter  <spamt...@crayne.org> wrote:
> Here is a simple helloworld program :
....
> How do I debug this program ? Is there any IDE available for the
> same ? How do I compute the length of the string dynamically ?

If it was a DOS application, you'd have a variety of options:
DEBUG.COM/EXE (from DOS/Windows; for 16-bit real mode apps only)
WD.EXE (from Open Watcom C/C++; there're 16-bit and 32-bit versions
available)
RHGDB.EXE (from DJGPP/DOS GXX port; 32-bit DPMI only)
etc

Likewise there exist a number of debuggers for win32 applications:
WD (again)
TD.EXE or whatever's the name (Borland's Turbo Debugger -- used to be
freely available with the command line 32-bit C/C++ compiler v 5.5(?)
for Windows)
WinDbg.exe (MS debugging tools, etc)
etc

All of the above (except DEBUG.COM/EXE and maybe RHGDB.EXE) are
capable of symbolic debugging (that is, they show you the assembly
source, not just the disassembly, and they support variables (global/
local)). There're other debuggers available, including shareware and
commercial ones (OllyDbg, Turbo Debugger, Code View, etc).

I don't know what's available for Linux (besides GDB) as I rarely
program for Linux.

Alex

0
Reply Alexei 9/23/2008 6:36:21 AM

Helge Kruse wrote:
> 
>> Is there any IDE available for the same ?
> Sorry, gdb ist a command line program.

Alternatively, he can have a try about kdbg from KDE.

Regards,
Jike

0
Reply Jike 9/23/2008 7:57:18 AM

crypter  <spamtrap@crayne.org> writes:
.....
> How do I debug this program ? Is there any IDE available for the
> same ? How do I compute the length of the string dynamically ?

Adding to people's comments about gdb, there are a range of 
front ends around gdb which make it more convenient to use:

- The flashiest is 'ddd', which I've never found does anything 
but slow me down. Seems very popular though.
- Then there's xxgdb, which looks clunky, but will only slow
you down a little.
- Gdb itself has a '-tui' switch which will give you a simple
text mode console 'GUI' interface. Very early 90s, but works well.
- The final one I've tried, and now always use is emacs: M-x gdb , 
and you'll have gdb running in an emacs buffer. Unbounded scrollback, 
and you can set the keybindings to be whatever are familiar to you 
(for some odd reason, I still use Borlands Turbo Debugger keys!)


Phil
-- 
The fact that a believer is happier than a sceptic is no more to the 
point than the fact that a drunken man is happier than a sober one. 
The happiness of credulity is a cheap and dangerous quality.
-- George Bernard Shaw (1856-1950), Preface to Androcles and the Lion

0
Reply Phil 9/23/2008 7:58:59 AM

crypter wrote:

.....
> How do I debug this program ? Is there any IDE available for the
> same ?

Jeff Owens has a suite of tools (written in Nasm) which includes "AsmBug"...

http://linuxasmtools.net/

Another alternative to gdb...

http://ald.sourceforge.net/

Or this one...

http://modest-proposals.com/Furball.htm

Best,
Frank

0
Reply Frank 9/23/2008 12:29:24 PM

"crypter" asked
[..code]
> How do I debug this program ?

Beside other anwers:
if it's a windoze PE or DLL I'd use either RosAsm-disass and/or Ollydebug,
for DOS: any newer version of debug.com (like FreeDos7+) may do it,
and if it's a L'unix task, search for for several available tools there.

> Is there any IDE available for the same ?

depends on the OS...

> How do I compute the length of the string dynamically ?

Even I think runtime calculations should/can be avoided:

MOV esi,[source]  ;strptr
XOR ecx,ecx       ;clear count
MOV al,0
;DEC ecx           ;max is -1 (0ffff_ffff)
CLD               ;if not assumed to be "up"
REPnZ SCASb       ;find the '00h'
jnz error_N       ;couldn't find a 00h
NEG ecx           ;ecx=count (0-iterations)
                  ;ecx is string size (including the 00h)
;DEC ecx           ;STR-Size w/o the Zero (if ecx weren't init to -1)
;JS  error_0       ;it is an empty string

disadvantge: it may take quite long or invoke an exception
             if no Zero-byte is found within the given range.
__
wolfgang


0
Reply Wolfgang 9/23/2008 6:02:00 PM

On Sep 23, 12:57�am, "Helge Kruse"  <spamt...@crayne.org> wrote:
> "crypter" <spamt...@crayne.org> wrote in message
>
> news:74cce31f-6b77-4432-a62e-fcd1ae670d7d@79g2000hsk.googlegroups.com...
>
> > Here is a simple helloworld program :
>
> > ------------------------------------------------------------------------
> > ------------------------------------------------------------------------
>
> > section .text ;section declaration
>
> > ;we must export the entry point to the ELF linker or
> > � �global _start ;loader. They conventionally recognize _start as
> > their
> > ;entry point. Use ld -e foo to override the default.
>
> > _start:
>
> > ;write our string to stdout
>
> > � � � �mov � � edx,len ;third argument: message length
> > � � � �mov � � ecx,msg ;second argument: pointer to message to write
> > � � � �mov � � ebx,1 � ;first argument: file handle (stdout)
> > � � � �mov � � eax,4 � ;system call number (sys_write)
> > � � � �int � � 0x80 ;call kernel
>
> > ;and exit
>
> > mov ebx,0 ;first syscall argument: exit code
> > � � � �mov � � eax,1 � ;system call number (sys_exit)
> > � � � �int � � 0x80 ;call kernel
>
> > section .data ;section declaration
>
> > msg � � db � � �"Hello, world!",0xa ;our dear string
> > len � � equ � � $ - msg � � � � � � � � ;length of our dear string
>
> > ------------------------------------------------------------------------
> > ------------------------------------------------------------------------
>
> > How do I debug this program ?
>
> You should run the assembler with the -g option to generate debug info.
> �gas -g -o foo foo.s
> You start the gnu debugger
> �gdb foo
>
> > Is there any IDE available for the same ?
>
> Sorry, gdb ist a command line program.
>
> > How do I compute the length of the string dynamically ?
>
> You will have to write a code fragment that reads each byte and check
> for the trailing 0 byte. To reuse the code you should prefer a
> function. Even if this implementation may fit your needs in the code
> above, you should check, if the interface is correctly designed, i.e. C
> style functions return their values in EAX.
>
> ; entry:
> ; � �ecx - address of string
> ; return:
> ; � �edx - length of string
> ; changed registers:
> ; � �none, except return register
> stringlen:
> � � mov edx,0
> � � push eax
> � � push ecx
> _len1:
> � � mov al,[ecx]
> � � or al,al
> � � jz _len2
> � � inc edx
> � � inc ecx
> � � jmp _len1
> _len2:
> � � pop ecx
> � � pop eax
> � � ret
>
> /Helge

I guess i tried with nasm -g option but the gdb doesn't debug it.

0
Reply crypter 9/24/2008 4:33:34 AM

On Sep 23, 2:02�pm, "Wolfgang Kern"  <spamt...@crayne.org> wrote:
> "crypter" asked
> [..code]
>
> > How do I debug this program ?
>
> Beside other anwers:
> if it's a windoze PE or DLL I'd use either RosAsm-disass and/or Ollydebug,
> for DOS: any newer version of debug.com (like FreeDos7+) may do it,
> and if it's a L'unix task, search for for several available tools there.
>
> > Is there any IDE available for the same ?
>
> depends on the OS...
>
> > How do I compute the length of the string dynamically ?
>
> Even I think runtime calculations should/can be avoided:
>
> MOV esi,[source] �;strptr
> XOR ecx,ecx � � � ;clear count
> MOV al,0
> ;DEC ecx � � � � � ;max is -1 (0ffff_ffff)
> CLD � � � � � � � ;if not assumed to be "up"
> REPnZ SCASb � � � ;find the '00h'
> jnz error_N � � � ;couldn't find a 00h
> NEG ecx � � � � � ;ecx=count (0-iterations)
> � � � � � � � � � ;ecx is string size (including the 00h)
> ;DEC ecx � � � � � ;STR-Size w/o the Zero (if ecx weren't init to -1)
> ;JS �error_0 � � � ;it is an empty string
>
> disadvantge: it may take quite long or invoke an exception
> � � � � � � �if no Zero-byte is found within the given range.
> __
> wolfgang



Thanks to all for all your inputs.. vil try those.

0
Reply crypter 9/24/2008 4:35:50 AM

"crypter" <spamtrap@crayne.org> wrote in message 
news:7de0e0a9-c956-445a-95db-939e1762c0ce@e53g2000hsa.googlegroups.com...
> On Sep 23, 12:57 am, "Helge Kruse"  <spamt...@crayne.org> wrote:
>> You should run the assembler with the -g option to generate debug info.
>> gas -g -o foo foo.s
>> You start the gnu debugger
>> gdb foo
>
> I guess i tried with nasm -g option but the gdb doesn't debug it.
>
I dont know how NASM creates debugging info, I assumed it's with the -g 
argument as with the GNU tools. Please look at the NASM documentation to see 
how to create debuggin information.

Regards,
Helge

0
Reply Helge 9/24/2008 6:51:08 AM

crypter wrote:

.....
> I guess i tried with nasm -g option but the gdb doesn't debug it.

What I do, which seems to work...

  section .text ;section declaration
  ;we must export the entry point to the ELF linker or
     global _start ;loader. They conventionally recognize _start as
  their
  ;entry point. Use ld -e foo to override the default.
  _start:

         nop ; "parking place" for gdb
realstart:

  ;write our string to stdout
         mov     edx,len ;third argument: message length
         mov     ecx,msg ;second argument: pointer to message to write
         mov     ebx,1   ;first argument: file handle (stdout)
         mov     eax,4   ;system call number (sys_write)
         int     0x80 ;call kernel
  ;and exit
         mov ebx,0 ;first syscall argument: exit code
         mov     eax,1   ;system call number (sys_exit)
         int     0x80 ;call kernel
  section .data ;section declaration
  msg     db      "Hello, world!",0xa ;our dear string
  len     equ     $ - msg                 ;length of our dear string

Assemble with "nasm -f elf32 -g hw.asm", link with "ld -o hw hw.o" - do 
not use the "-s" switch to ld!

gdb hw
break realstart
run
step
.....

I don't know why the "nop" helps, but it seems to...

Best,
Frank

0
Reply Frank 9/25/2008 7:23:47 AM

11 Replies
96 Views

(page loaded in 0.173 seconds)

Similiar Articles:


















7/15/2012 3:47:44 PM


Reply: