Arrays: Difference between revisions

Added SSEM
(→‎{{header|Perl}}: Migrating from http://rosettacode.org/wiki/Creating_an_Array)
(Added SSEM)
Line 4,875:
Row 3: 3-count
</pre>
 
=={{header|SSEM}}==
At the machine level, an array is a block of sequential storage addresses. Modern computer architectures support arrays through <i>indexed addressing</i>, where the contents of a particular register can be used to provide an offset from some specified address. A program to find the sum of a four-element array beginning at address <tt>array</tt> might look, in pseudocode, like this:
<pre> load register 0, #0 ; running total
load register 1, #0 ; index
loop: add register 0, array+register 1
add register 1, #1
compare register 1, #4
branchIfLess loop</pre>
If we do not know in advance how many elements the array will have, we can mark the end with a special value (say, zero) and test for that. Again in pseudocode:
<pre> load register 0, #0 ; running total
load register 1, #0 ; index
loop: load register 2, array+register 1
compare register 2, #0
branchIfEqual done
add register 0, register 2
add register 1, #1
goTo loop
done: ; program continues with sum in register 0</pre>
On a machine like the SSEM, which has only one addressing mode and only one general-purpose register (the accumulator or <tt>c</tt>), we can achieve the same things using <i>instruction arithmetic</i>—also known as <i>self-modifying code</i>. Since an instruction that refers to address <math>n+1</math> can be obtained by adding one to an instruction that refers to address <math>n</math>, the pseudocode to find the sum of a four-element array (and store it at address <tt>sum</tt>, which we assume initially holds zero) becomes:
<pre>loop: load accumulator, sum
instr: add accumulator, array
store accumulator, sum
load accumulator, instr
add accumulator, #1
compare accumulator, #(add accumulator, array+4)
branchIfEqual done
store accumulator, instr
goTo loop
done: ; program continues</pre>
We are now in a position to translate this algorithm into SSEM instructions and run it. As always, the SSEM version is a bit fiddlier than the pseudocode because the SSEM has no <tt>load</tt> or <tt>add</tt> instructions; but it follows the pseudocode as closely as the instruction set allows, so it should be comparatively readable. As a test, we shall sum an array of the first four positive integers—a very significant operation for the Pythagoreans of old—and halt with the accumulator holding the result.
<lang ssem>10101000000000100000000000000000 0. -21 to c
11101000000000010000000000000000 1. Sub. 23
00101000000001100000000000000000 2. c to 20
00101000000000100000000000000000 3. -20 to c
10101000000001100000000000000000 4. c to 21
10000000000000100000000000000000 5. -1 to c
01001000000000010000000000000000 6. Sub. 18
00101000000001100000000000000000 7. c to 20
00101000000000100000000000000000 8. -20 to c
10000000000001100000000000000000 9. c to 1
01101000000000010000000000000000 10. Sub. 22
00000000000000110000000000000000 11. Test
01001000000001000000000000000000 12. Add 18 to CI
11001000000000000000000000000000 13. 19 to CI
10101000000000100000000000000000 14. -21 to c
00101000000001100000000000000000 15. c to 20
00101000000000100000000000000000 16. -20 to c
00000000000001110000000000000000 17. Stop
10000000000000000000000000000000 18. 1
11111111111111111111111111111111 19. -1
00000000000000000000000000000000 20. 0
00000000000000000000000000000000 21. 0
11011000000000010000000000000000 22. Sub. 27
10000000000000000000000000000000 23. 1
01000000000000000000000000000000 24. 2
11000000000000000000000000000000 25. 3
00100000000000000000000000000000 26. 4</lang>
The program could easily be modified to work with arrays of unknown length, if required, along the lines of the second pseudocode example above.
 
=={{header|Suneido}}==
519

edits