* varargs call frames
[not found] <tencent_3C42366FC45E16CC94CC9949FCC136D40C07@qq.com>
@ 2023-04-26 15:51 ` Nick Clifton
0 siblings, 0 replies; only message in thread
From: Nick Clifton @ 2023-04-26 15:51 UTC (permalink / raw)
To: anix; +Cc: gdb
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-04-26 15:51 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <tencent_3C42366FC45E16CC94CC9949FCC136D40C07@qq.com>
2023-04-26 15:51 ` varargs call frames Nick Clifton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).