Overloaded operators: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with " An https://en.wikipedia.org/wiki/Operator_overloading overloaded operator can be used on more than one data type, or represents a different action depending on the contex...")
 
mNo edit summary
Line 1: Line 1:


An [[https://en.wikipedia.org/wiki/Operator_overloading overloaded operator]] can be used on more than one data type, or represents a different action depending on the context. For example, if your language lets you use "+" for adding numbers and concatenating strings, then one would say that the "+" operator is overloaded.
An [https://en.wikipedia.org/wiki/Operator_overloading overloaded operator] can be used on more than one data type, or represents a different action depending on the context. For example, if your language lets you use "+" for adding numbers and concatenating strings, then one would say that the "+" operator is overloaded.


;Task
;Task
Line 6: Line 6:


<br><br>
<br><br>

=={{header|6502 Assembly}}==

Many commands have multiple [[6502_Assembly#Addressing_Modes| addressing modes]], which alter the way a command is executed. On the 6502 most of these are in fact different opcodes, using the same mnemonic.
<lang 6502asm>LDA #$80 ;load the value 0x80 (decimal 128) into the accumulator.
LDA $80 ;load the value stored at zero page memory address $80
LDA $2080 ;load the value stored at absolute memory address $2080.
LDA $80,x ;load the value stored at memory address ($80+x).
LDA ($80,x) ;use the values stored at $80+x and $81+x as a 16-bit memory address to load from.
LDA ($80),y ;use the values stored at $80 and $81 as a 16-bit memory address to load from. Load from that address + y.</lang>

Revision as of 16:02, 13 September 2021

An overloaded operator can be used on more than one data type, or represents a different action depending on the context. For example, if your language lets you use "+" for adding numbers and concatenating strings, then one would say that the "+" operator is overloaded.

Task

Demonstrate overloaded operators in your language, by showing the different types of data they can or cannot operate on, and the results of each operation.



6502 Assembly

Many commands have multiple addressing modes, which alter the way a command is executed. On the 6502 most of these are in fact different opcodes, using the same mnemonic. <lang 6502asm>LDA #$80 ;load the value 0x80 (decimal 128) into the accumulator. LDA $80 ;load the value stored at zero page memory address $80 LDA $2080 ;load the value stored at absolute memory address $2080. LDA $80,x ;load the value stored at memory address ($80+x). LDA ($80,x) ;use the values stored at $80+x and $81+x as a 16-bit memory address to load from. LDA ($80),y ;use the values stored at $80 and $81 as a 16-bit memory address to load from. Load from that address + y.</lang>