varargs call frames

Nick Clifton via Gdb <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
Hi anix,

> my source file: > > .section .data> msg:  .asciz "Address after MALLOC: 0x%x.\n"> p:    .int 0x0
> .section .text
> .globl _start
> _start:
>       pushl $0x14
>       call malloc
> k:    movl %eax, p
>       pushl %eax
>       pushl %ebx
>       pushl $msg
>       call printf

> // after assembling and loading, I fond that the result while running the file:
> [nwsh@localhost ch06]$ ./mmaddr
> *Address after MALLOC:**0xb774dfbc.*
>
> // in gdb environment:
> Breakpoint 1, k () at mmaddr.s:9
> 9     k:    movl %eax, p
> (gdb) p/x $eax
> $2 = 0x804b008 >
> // i do not know why the results under running and gdb different very mach.
> *// please tell me, Thank you very much!!!*

The answer is that you have a mistake in your code.  The call to 'malloc'
returns the allocated address in %eax, but you then destroy this pointer
with the movl instruction at label 'k'.  So when you call 'printf' you
are displaying whatever value happened to be in the %ebx register.  Which
could be anything.

The gdb commands are correct.  Upon return from malloc, the %eax register
holds the correct value.  It is just that you are not preserving this value
and passing it on to the call to printf.

Cheers
   Nick
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.