I've got this code, but how would I make it using loops. Basically Its a code that takes the first value in the x array and adds it to the next, saves it to the first value in y, then takes the second value from x and adds it to the third and stores it in the second value in y, and so forth. Then it takes the last value of x and adds it to the first value of x and saves it to the last value of y.
.data
x sword 10,20,30,-10,-20, -30
y sword 6 dup (?)
mov ax,x
add x+2,ax
mov y,ax
mov ax,x+2
add x+4,ax
mov y+2,ax
mov ax,x+4
add x+6,ax
mov y+4,ax
mov ax,x+6
add x+8,ax
mov y+6,ax
mov ax,x+8
add x+10,ax
mov y+8,ax
mov ax,x+10
add x,ax
mov y+10,ax
I'm assuming it's homework, so no complete answer.
First, to read/write a memory location, you need to use []. [ax] is the memory location at ax, and so on. Works for arrays, too: [x+2] means the second element of x (because they're two bytes each).
Moving on. You can accomplish a loop over array(s) with an index or with running pointers. Let's consider the first.
So you need to run an index ( i
) from 0 to 5. For each value, you need to assign to y[i] the value of x[1] + x[i+1]
, with the exception of the last one. You'll need a conditional to take care of the last one.
In Intel, there are two registers specifically designed to be indices - SI
and DI
. You can address arrays with them premultiplied - constructs like [array+SI*2]
.
In order to do a loop, you need a conditional jump back: if the index is less than array size, execute the loop again.
So, in pseudocode, deliberately using variable names instead of registers:
i=0
Loop:
copy x[i] into y[i] (more than one command in assembly)
if i = array_size-1 then add x[0] to y[i] (conditional jump here)
else add x[i+1] to y[i]
increment i
if i = array_size then jump to loop
Can you follow from that?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.