Parameter Passing: Difference between revisions

no edit summary
No edit summary
Line 90:
* objects with an identity are passed by reference. Objects with identities are task types, protected types, non-copyable (limited) types, tagged types ([[object-oriented programming|OO objects]] with type identity)
* for all other types the choice is left to the compiler.
 
===Example [[ARM Assembly]]===
Whether an assembly language uses pass-by-value or pass-by-reference mostly depends on the compiler and the language the source code was written in, if not written directly in ARM Assembly.
 
Early versions of the ARM were unable to directly operate on memory without loading the value into a register first. So almost all commands were inherently pass-by-value unless they explicitly stored an updated value in the same memory location.
<lang ARM Assembly>;this example is for the ARM7TDMI and might not be true for newer revisions.
mov r0,#memoryAddress ;r0 contains the memory address, not the value stored within.
mov r1,#anotherMemoryAddress ;r1 contains the memory address, not the value stored within.
add [r0],[r0],[r1] ;won't work, you have to LDR the value into a register, work on it there, then store it back.
 
ldr r0,memoryAddress ;load the VALUE stored at "memoryAddress" into r0
ldr r1,anotherMemoryAddress ;load the VALUE stored at "anotherMemoryAddress" into r1
mov r2,#memoryAddress ;load the MEMORY ADDRESS into r2
add r0,r0,r1 ;add r1 to r0 and store the result in r0
str r0,[r2] ;store the value of r0 into the MEMORY ADDRESS contained in r2.</lang>
 
Typically when talking about pass-by-value or pass-by-reference, a programmer is referring to the contents of a memory address. The stack and the CPU's data registers are not included in the conversation. It's expected that a data register gets "clobbered," after all, it's impossible to do actual math otherwise. Or so you might think, but the ARM takes the concept of "pass-by-value" one step further. The data registers themselves can be operated on in some ways without altering their contents:
* The data register where a math operation is stored can be different from both operands.
* In any operation, the contents of a data register can be bit-shifted or bit-rotated just for that operation, and the actual value is unchanged.
[[x86 Assembly]] can do neither of the above; at least one of its registers used as an operand must be the destination for the result.
 
 
===Example [[C]]/[[C++]]===
1,489

edits