From: Nick Clifton <nickc@redhat.com>
To: anix <2362292778@qq.com>
Cc: "gdb@sourceware.org" <gdb@sourceware.org>
Subject: varargs call frames
Date: Wed, 26 Apr 2023 16:51:32 +0100 [thread overview]
Message-ID: <1e770cc8-44dd-06e0-c874-4f4a48c5b0d9@redhat.com> (raw)
In-Reply-To: <tencent_3C42366FC45E16CC94CC9949FCC136D40C07@qq.com>
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
parent reply other threads:[~2023-04-26 15:51 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <tencent_3C42366FC45E16CC94CC9949FCC136D40C07@qq.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1e770cc8-44dd-06e0-c874-4f4a48c5b0d9@redhat.com \
--to=nickc@redhat.com \
--cc=2362292778@qq.com \
--cc=gdb@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).