I am trying to create a loop in loop and change data in array

Started by ilian007, November 14, 2009, 06:24:35 AM

Previous topic - Next topic

ilian007

hello,
I am trying to create a loop in loop and change data in array

In the begining I have array with 15 elements all of them zeroes.

I have to pass thru each index of the array and check if 0 or 1. If the index is 0 to change it with 1 and if the index is 1 to change it with 0.
here is what I am trying to do:


Array dup 15 (0)

For x=1 to 15 (step thru the indexes of the array)
    For y=x to 15 step x
If Arr[y] = 0 then
  Arr[y] =1
Else
        Arr[y]=0
    EndIf
next y
next x



It compiles normally but when I execute the program nothing happens and have to end the command prompt.....


here is my code:

;===================================================================
INCLUDE PCMAC.INC       
     .MODEL  small,BASIC
.586
.STACK 100h

;===================================================================
                                       ;PROCEDURES TO
             
   
   EXTRN   CLEAR:FAR   ;CLEAR SCREEN
   EXTRN   NEWLINE:FAR    ;DISPLAY NEWLINE CHARACTER
           EXTRN   GETDEC$:FAR   ;GET 16-BIT DECIMAL FROM KBD
   EXTRN   PUTDEC$:FAR   ;DISPLAY 16-BIT DECIMAL INT
   EXTRN   GETDEC:FAR
   EXTRN   PUTDEC:FAR
   EXTRN   PUTBIN:FAR   ;DISPLAY 16BIT BINARY INT
           EXTRN   PUTOCT:FAR     ;DISPLAY 8-BIT NUMBER
   EXTRN   PUTSTRNG:FAR   ;DISPLAY CHARACTER STRING


;===================================================================
;DATA  S E G M E N T   D E F I N I T I O N
;
  .DATA
MyArr   Dw 15 dup (0)
X Dw ?
Y Dw ?

   


;===================================================================
;C O D E   S E G M E N T   D E F I N I T I O N
;
            .CODE
    ASSUME  DS:DGROUP
;===================================================================
MAIN PROC

        MOV     AX,DGROUP     ;SET DS-REGISTER TO POINT TO
        MOV     DS,AX               ;CONSTANT DATA SEGMENT
MOV     ES,AX    ;SET ES-REGISTER
CALL    CLEAR
START:
MOV x,0
MOV y,0
mov ax,0

incx:
inc x ;for x =1 to 15 x++
cmp x,15
ja prints ; if X>15 jump to Print
mov cx,0
mov y,0
add cx,x ; else add x to cx


incy:
add y,cx ; y=x to 15 step x
mov bx,y
shl bx,1
mov ax, myarr [bx]

cmp ax, 0
je addone

addzero:
mov myarr [bx], 0
jmp cont1
addone:
mov myarr [bx], 1


cont1:
cmp y,15 ;for y = 1 to 15 y++
jbe incy
jmp incx ;jump to increment X

prints:
        add bx,1
shl bx,1
MOV Ax, myarr [bx]
call putdec
call newline
cmp bx, 15
jbe prints

endd:



.EXIT
MAIN ENDP             
       END MAIN


Thank You

Added code tags

dedndave

simplify the loop structure
here is a loop inside a loop - with nothing else...

        mov     cx,15      ;outer loop count

loop_outer:
        push    cx
        mov     cx,15      ;inner loop count

loop_inner:
        push    cx
;
;
; contents of inner loop
;
;
        pop     cx
        loop    loop_inner

        pop     cx
        loop    loop_outer

this would work for a 15 x 15 array

this code:

        pop     cx
        loop    loop_outer

is the same as this:

        pop     cx
        dec     cx
        jnz     loop_outer

sinsi

>If the index is 0 to change it with 1 and if the index is 1 to change it with 0.
Do you mean 'if the value of the indexed array element' or the actual index? Your array has 15 elements but you are only talking about index 0 or 1.

There's a hint - look up XOR


For x=1 to 15 (step thru the indexes of the array)
    For y=x to 15 step x

? you are stepping through the array from 1 to 15 -> good
then you step again ?
for x=1 to 15
  myarray[x] = myarray[x] XOR TheMagicNumber
next x

Light travels faster than sound, that's why some people seem bright until you hear them.

dedndave

Sinsi - that would XOR a 1 onto all elements
he only wants to modify elements that are 0, or 0 and 1 (his post is a little ambiguous - lol)

MichaelW

The pseudo code indicates to me that the intention is to toggle the indexed element between 0 and 1, and an XOR with 1 will do that. I did a straightforward translation of the pseudo code to assembly, with 15 instructions. The x loop runs 15 times, the y loop runs 45 times, and the array ends up as:

100100001000000
eschew obfuscation

ilian007

Michael I believe I need to do what you did. Have to go thru the array and when hit 0 to change it with 1. When hit 1 to change it with 0 :)

what is wrong with my code ?

for x =1 I have to go y=1 to 15
steping with the value of x

x=1 y=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
x=2 y=2,4,6,8,10,12,14
x=3 y=3,6,9,12,15
......
........

x=15 y=15

exit

or something like that :)

ilian007

it doesnt give me any errors, but I have to close it with closing the CMD window :((

ilian007

I have changed:

   cmp ax, 0
   je addone

addzero:
   mov myarr [bx], 0
   jmp cont1
addone:
   mov myarr [bx], 1


with: xor myarr [bx], 1

saved about 10 lines of code.

dedndave tried to do the loop in the way you showed. Still trying to make it. where the outer content should be ?

MichaelW

I don't know exactly what the problem is, but using the first code you posted if I comment out the array accesses:

;mov myarr [bx], 0
;mov myarr [bx], 1

Then the code runs and exits. If I uncomment the array accesses and enlarge the array to 200 elements, then the code runs and exits. So the array index is going outside the original array bounds. If I leave the number of elements at 200, and add counters for the x and y loops, I get 16 and 60 loops. The way your loops are structured the 16 loops for the x loop may be OK, but 60 loops for the y loop is a problem. If you cannot find the error by examining the code, you could try loading the program into DEBUG and tracing through it.
eschew obfuscation

ilian007

I am working in the moment finding the solution using dedndave method for the loop and Michael's method with Xor/ing by 1 will post updates a little bit later. Now printing but always zeroes.

thanks :)