Assembly 使用直接寻址模式的数组元素之和

Assembly 使用直接寻址模式的数组元素之和,assembly,Assembly,机器只有直接寻址模式意味着: load R1, address --> fill R1 with value of Memory[address] store R1, address --> fill Memory[address] with value of R1 其他一些指示: add R1, #immediate --> R1 = immediate + R1 add R1, address --> R1 = M[add

机器只有直接寻址模式意味着:

load R1, address    -->    fill R1 with value of Memory[address]
store R1, address   -->    fill Memory[address] with value of R1
其他一些指示:

add R1, #immediate  -->    R1 = immediate + R1
add R1, address     -->    R1 = M[address] + R1
Loop R1, L          -->    if != 0 goto L 

如rcgldr所指出的,没有寄存器间接寻址模式,除了自修改代码之外没有其他方法

start:      load  R1, #arrayAddr     ; address of first array element
            load  R2, #arrayLength   ; loop counter
            load  R3, #0             ; sum

arrayLoop:  store R1, modifyAddr     ; modify operand of the instruction below
opcodeAddr: add   R3, dummyAddress   ; get array element, add to sum
            add   R1, #2             ; next address (assuming 16-bit integers, i.e. 2 bytes per element)
            add   R2, #-1            ; decrement counter
            loop  R2, arrayLoop
modifyAddr
定义为标签
opcodeAddr
处的指令地址:

modifyAddr = opcodeAddr + 1       ; (assuming instructions have a 1-byte opcode)

我用了3个寄存器。如果指令集包含一条“比较”指令,那么两个寄存器就足够了。

在一些旧计算机上,如CDC 3000系列,有指令从另一条指令的地址字段加载或存储一个值。在这种情况下,自修改代码是指令集的一部分并起作用。