screenshot of textbook (The code is an example of adding a list of numbers together and storing the answer using a loop)
100 LDM #0 ; Load 0 into ACC
101 STO total
102 STO counter
103 LDR #0 ; Load 0 into IX
104 loop:
105 LDX number ; Load the number indexed by IX into ACC
106 ADD total
107 STO total
108 INC IX
109 LDD counter
110 INC ACC
111 STO counter
112 CMP #3
113 JPN loop
114 END
115 number: #5
116 #7
117 #3
118 counter:
119 total:
If zero is stored in the index register instead of the address at which "number" is defined, I don't see how the intended number would be loaded into the accumulator.
If line 105, "LDX number" is loading the address into IX, the comment next to it "Load the number indexed by IX into ACC" confuses me. Is it loading the address to IX, and the number at this address into ACC at the same time?
No:
It adds the address of the element (e.g. variable) specified in the instruction (
number) to the value inIX.The result is an address.
The CPU loads the content of the memory at this address (the sum of the addition) into the register
ACC.In other words:
If the register
IXcontains the value 5, the instruction loads the 6-th word (on most CPUs: byte) of the variablenumberinto the registerACC.If you start with
IX=0, continue withIX=1and so on, you first load the first word ofnumber, then the second word and so on ...You might do it the following way, too:
In this case,
IXalready contains the "correct" address where to load from so nothing needs to be added in theLDXinstruction.However, the following code would not work:
This code would load
ACCfrom the addressIX+number. Ifnis the address of the first word of the variablenumber, the code would load words from2*n,2*n+1,2*n+2... instead ofn,n+1and so on ...