Parameter Passing: Difference between revisions
Content added Content deleted
Line 116: | Line 116: | ||
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. |
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. |
||
<syntaxhighlight 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 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. |
mov r1,#anotherMemoryAddress ;r1 contains the memory address, not the value stored within. |
||
Line 125: | Line 126: | ||
mov r2,#memoryAddress ;load the MEMORY ADDRESS into r2 |
mov r2,#memoryAddress ;load the MEMORY ADDRESS into r2 |
||
add r0,r0,r1 ;add r1 to r0 and store the result in r0 |
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. |
str r0,[r2] ;store the value of r0 into the MEMORY ADDRESS contained in r2. |
||
</syntaxhighlight> |
|||
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: |
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: |
||
Line 131: | Line 133: | ||
* 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. |
* 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. |
[[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++]]=== |
===Example [[C]]/[[C++]]=== |