6

I have this source code:

;  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 section
msg:    db "Hello World",10 ; the string to print, 10=cr
len:    equ $-msg       ; "$" means "here"
                ; len is a value, not an address

    SECTION .text       ; code section
        global main     ; make label available to linker 
main:               ; standard  gcc  entry point

    mov edx,len     ; arg3, length of string to print
    mov ecx,msg     ; arg2, pointer to string
    mov ebx,1       ; arg1, where to write, screen
    mov eax,4       ; write command to int 80 hex
    int 0x80        ; interrupt 80 hex, call kernel

    mov ebx,0       ; exit code, 0=normal
    mov eax,1       ; exit command to kernel
    int 0x80        ; interrupt 80 hex, call kernel

This code is taken from here.

I am running ubuntu 12.04 32-bit on VirtualBox for learning purposes.

Steps I follow are:

  • nasm -f elf -g -F stabs hello.asm
  • ld -o hello hello.o
  • gdb hello -tui

Now when I only run hello it will run fine but gdb fails to show any source code. Why? When I tryp run in gdb I will see Hello World text just fine but it does not show the source.

5
  • That's a bug somewhere in nasm or gdb. Note that if you start doing si for example, gdb will happily show you the source code. Commented Jan 2, 2015 at 19:41
  • what do you mean "start doing si" @Jester ? Commented Jan 2, 2015 at 19:42
  • Use the si command to single step instructions. gdb will correctly show the source code. Use start instead of run or place a breakpoint manually first. Commented Jan 2, 2015 at 19:43
  • @Jester Sorry, can you give a full example as an answer? I am very new to Assembly and I have no idea what "use the si command to single step instructions" mean.. Sorry :) Commented Jan 2, 2015 at 19:49
  • Excellent commenting style. Voted +1 for that factor alone Commented Jan 3, 2015 at 1:05

1 Answer 1

8

It looks like stabs format doesn't work with GDB, try DWARF instead ( http://en.wikipedia.org/wiki/DWARF )

compile with

nasm -f elf -g -F dwarf hello.asm

then in gdb type

start

then

si

you will see sources with comments so on. as Koray Tugay said there is most probably a bug in gdb.

Sign up to request clarification or add additional context in comments.

3 Comments

May also help to put nop as the first instruction after... well, you're using main. Does ld not complain about the lack of an entrypoint? ld usually expects _start - gcc (like it says where you found the code) expects main. Tell ld -e main or change it to _start to shut ld up... or use gcc...
@FrankKotler Thanks for the information. _start made ld not complain anymore. But what is the cause for this? Any source I can read?
Some tutorials here: asm.sourceforge.net/resources.html#tutorials You might find that kind of information in the "teensy" tutorial, although writing obsessively short files is probably not something you want to do. Of course there's man ld...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.