I know this is simple but I'm having some troubles with "while" in Python. My guess is that "while" doesn't work as I think it does in this language. When doing a while loop inside another while loop as part of my code, the second while loop just does it's job one time and then continues, but my intention is for the second while loop to do it's job multiple times and then return to the first while loop.
The actual code I'm using is this one:
file=open('program.asm','r+')
lista= file.readlines()
i=0
while (i<len(lista)):
cad=lista[i]
if (cad.find('data')!=-1):
while (cad.find('section')!=-1 and i<len(lista)):
print(i)
print(cad)
i=i+1
cad=lista[i]
print(cad)
saveData(cad)
print(cad)
print(i)
print(i)
file.close()
The file contains the following:
section .data
a db 2
b db 3
section .bss
result resw 1
section .text
global CMAIN
CMAIN:
mov r1,a
mov r2,b
add r1,r2
mov word[result],r1
ret
The result I'm getting is
1
1
section .data
a db 2
a db 2
2
3
4
This is weird, with my prints the result should be
1
section .data
a db 2
a db 2
2
3
b db 3
b db 3
4
Basically, the if condition meets when i=1.
Then the while loop should print "a db 2" if i=2 and "b db 3" if i=3. If i=4 get out of the while loop.
Instead, the code does something very weird. I don't know where that second "1" comes from and the while loop only executes 1 time.
cad.find('section')!=-1. Then you ++i and you print "a db 2" twice... for the rest I am not sure but: 1. You do not incrementiin the outer loop and 2. I get a feeling there is a more pythonic approach to this (like".data" in cadinstead of find) but you ll have to explain a bit more what you try to achieve (seems like you extract data section)