Apply a callback to an array: Difference between revisions

no edit summary
m (Updated description and link for Fōrmulæ solution)
No edit summary
Line 13:
{{out}}
<pre>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]</pre>
 
=={{header|6502 Assembly}}==
For this example, assume both the source array and the destination have a size of 86 elements (memory offsets base+0x00 to base+0x55.)
This was implemented in easy6502.
<lang 6502asm>define SRC_LO $00
define SRC_HI $01
 
define DEST_LO $02
define DEST_HI $03
 
define temp $04 ;temp storage used by foo
 
;some prep work since easy6502 doesn't allow you to define arbitrary bytes before runtime.
 
SET_TABLE:
TXA
STA $1000,X
INX
BNE SET_TABLE
;stores the identity table at memory address $1000-$10FF
 
CLEAR_TABLE:
LDA #0
STA $1200,X
INX
BNE CLEAR_TABLE
;fills the range $1200-$12FF with zeroes.
 
 
LDA #$10
STA SRC_HI
LDA #$00
STA SRC_LO
;store memory address $1000 in zero page
 
LDA #$12
STA DEST_HI
LDA #$00
STA DEST_LO
;store memory address $1200 in zero page
 
 
loop:
LDA (SRC_LO),y ;load accumulator from memory address $1000+y
JSR foo ;multiplies accumulator by 3.
STA (DEST_LO),y ;store accumulator in memory address $1200+y
 
INY
CPY #$56 ;alternatively you can store a size variable and check that here instead.
BCC loop
BRK
 
foo:
STA temp
ASL ;double accumulator
CLC
ADC temp ;2a + a = 3a
RTS</lang>
 
{{out}}
<pre>
1200: 00 03 06 09 0c 0f 12 15 18 1b 1e 21 24 27 2a 2d
1210: 30 33 36 39 3c 3f 42 45 48 4b 4e 51 54 57 5a 5d
1220: 60 63 66 69 6c 6f 72 75 78 7b 7e 81 84 87 8a 8d
1230: 90 93 96 99 9c 9f a2 a5 a8 ab ae b1 b4 b7 ba bd
1240: c0 c3 c6 c9 cc cf d2 d5 d8 db de e1 e4 e7 ea ed
1250: f0 f3 f6 f9 fc ff
</pre>
 
 
=={{header|8th}}==
1,489

edits