Author Topic: Confusion about labels  (Read 6988 times)

Offline clem55

  • New Member
  • Posts: 1
Confusion about labels
« on: April 05, 2011, 05:51:35 PM »
Hi. I'm new to assembly programming.

I have a question about labels.

Code: [Select]
section .text
global _start
call _writeHello
jmp .again

mov eax, 4
mov ebx, 1
mov ecx, buf
mov edx, len
int 0x80

section .data

buf: db 'Hello World!', 10
len: equ $-buf

If I assemble this code and them use gdb to disassemble the _start procedure, the following output is produced:
(gdb) disassemble _start
Dump of assembler code for function _start:
   0x08048080 <+0>:   nop
End of assembler dump.

which implies that the function _start ends at the nop instruction, before the .again label. Why does this happen? I was expecting that call and jmp instructions to be included in the _start procedure. Do labels mark the ending of the previous function and beginning of another?

What's the correct code to mark a particular address in the code to jump to later?

Thank You.
« Last Edit: April 05, 2011, 08:00:25 PM by clem55 »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Confusion about labels
« Reply #1 on: April 05, 2011, 10:56:31 PM »
What's the correct code to mark a particular address in the code to jump to later?

Labels. I don't think you're confused about labels - you seem to be using them correctly. You might be confused about gdb (I know I am!), or gdb might be confused about labels. An observation: gdb seems not to like Nasm's "local label" syntax - if we use a '.', gdb seems to think we're using a structure. It recognizes ".again:" as a label (apparently), but:

Code: [Select]
(gdb) disassemble .again

gives a syntax error. Okay, the "true name" of that label would be "_start.again"...

Code: [Select]
(gdb) disassemble _start.again

"Attempt to extract a component of a value that is not a structure"

So I guess the "workaround" is: "You can use local labels, but don't expect to tell gdb about 'em."

Code: [Select]
(gdb) help disassemble

gives a reference to "frame". I added a "stack frame" ("enter 0, 0" and "leave") to your "_writeHello" function - didn't seem to help (I added some unreachable code after "ret", and gdb disassembles it). Also mentions "two arguments are taken as a range of memory to dump"...

Code: [Select]
disassemble _start _writeHello

Seems to do what you were expecting. My definition of "function" would be "up until the 'ret'", but gdb seems to consider "function" to mean "from one label to the next". That's my current understanding, at least. More information welcome!

You know that old Otis Redding tune "Hard to Handle"? I think it's about gdb! :)