In line 1 the address of the first element of ARRAY, 1000, is put into the A register. Then I is added to it to get the actual address, which is then moved to S by A2S. Then LDS happens, which puts into A the desired element of ARRAY. 1000 is called the base address and the value of I is the offset. Many machines are byte addressable, so they store one complete integer or one complete float in a 4-byte chunk of memory in order to get enough bits to have a large enough value. In these cases, they multiply the offset by 4 to get the address of the beginning of the correct chunk of memory. Having wide words like the CSC-1 does makes addressing simpler, but tends to waste memory, especially when characters are involved. Since the CSC-1 has 16 bits per word, it could store two ASCII characters in a single word, called packing. If it didn't, then it would be wasting half its memory when it stored character strings. If it did pack two characters per word, then more complex instructions would be needed to unpack and use them. So most machines divide memory into rather small physical words, usually 8-bit bytes. The following code shows how to add 1 to each element of Array[I]. Notice that the STS instruction is used to change memory when the address is in the S register. The value in S is used twice in the following example: once to get the original value of Array[I] and the second time to tell the computer where it store it back. No operand is used for STS since the address to store is in S and the value to store is in ACC. LOOP: LDI 1000 ; Get Array[I] (base address) ADD I ; Add I to base address A2S ; Copy this address to S reg LDS ; Load accum. from this address JN ENDLOOP ; If negative, then done ADD ONE ; Add 1 to this value STS ; and store back into Array[I] LOD I ; I := I + 1 ADD ONE STD I JMP LOOP ; Go back to top of loop ENDLOOP: HLT ; Halt when done with loop ... ONE: NUM 1 ; Constant value 1 If the address of Array[I] were not already in the S register, it would have to be calculated and moved from A to S before issuing the STS instruction. This would be the case if the program were storing initial values in the array, for example. |