What other languages use the same data model as Python?
harrismh777
harrismh777 at charter.net
Thu May 5 00:55:39 EDT 2011
Tim Roberts wrote:
> The fact that the parameter "a"
> in BumpMe happens to be an address is completely irrelevent to the
> definition of the parameter passing mechanism.
>
> C has pass-by-value, exclusively. End of story.
Yeah, Tim, I know... but that's my entire point in a nut-shell...
whether the language is pass-by-value or pass-by-reference has less to
do with how it is 'defined' (its mechanism--- indirection and stack) and
more to do with how it is routinely used with the standard features it
provides--- in this case memory indirection--- as pointers.
Something new here, just for fun...
... I ran my hello.c program through the gcc compiler and intercepted
its assembly source output. Some folks on the list may not know that the
gcc compiler used to generate CPython (at least on *nix systems) does
not generate object or machine code directly, but generates an
intermediate assembly source in AT&T syntax. The assembly code is
interesting for the discussion, if you've never seen it. If you have,
blow this off.
Anyway, I built a small wrapper called 'say()' around the printf
function, so that I could pass a string var to say(), and I then called
it a couple of times in main(). The assembly source code is listed at
the end of this post. The thing to notice here is two things:
1) assembly code is actually being used to generate the machine
code, not 'C' (and this is true for Python as well, compiled from
sources) In other words, Python interpreter does not do anything more
(nor less) than what can be done with assembler (nor machine code for
that matter). And,
2) The strings I am 'passing' to the say() function don't get
'passed' anywhere. (I know that most of you know this, bear with me) The
strings are set in memory, and through memory indirection pointers (the
parenthesis 'references') the string's memory addresses are placed on
the stack. The called routine say() has full access to the original
strings in memory (via their memory addresses) if necessary. The main
point being that the say() function has a 'reference' to the original,
and not a local copy of the 'value', and it can change it! The string's
addresses are .LC0 and .LC1/
Here is the assembly of my hello.c saved as hello.s with:
gcc -Wall -S -o hello.s hello.c
.file "hello.c"
.section .rodata
.LC0:
.string "\nhello, world!\n"
.align 4
.LC1:
.string "...and again I say, hello there, world!\n\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $0, 28(%esp)
movl $.LC0, (%esp)
call say
movl $.LC1, (%esp)
call say
movl 28(%esp), %eax
leave
ret
.size main, .-main
.section .rodata
.LC2:
.string "%s"
.text
.globl say
.type say, @function
say:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $.LC2, %eax
movl 8(%ebp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
leave
ret
.size say, .-say
.ident "GCC: (Ubuntu 4.4.1-4ubuntu9) 4.4.1"
.section .note.GNU-stack,"", at progbits
More information about the Python-list
mailing list