0

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.

1
  • Inner while will only run for the "section .data" line I think once due to 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 increment i in the outer loop and 2. I get a feeling there is a more pythonic approach to this (like ".data" in cad instead of find) but you ll have to explain a bit more what you try to achieve (seems like you extract data section) Commented May 11, 2019 at 22:21

1 Answer 1

1

Two quick suggestions:

Way 1: While loops

This is how I 'd go with loops

file=open('program.asm','r+')
lista= file.readlines()
i=0
while i < len(lista):
    print(i)
    cad=lista[i]
    if "data" in cad:
        # Move INTO data section
        print(cad.strip())
        i+=1
        cad=lista[i]
        # Now, read until the next section
        while "section" not in cad and i < len(lista):
            print(i)
            print(cad.strip())
            i=i+1
            cad=lista[i]
    # If no data section found, keep on looking
    else:
        i += 1
file.close()

Way 2: Extract data

This might need editing for different new-line formats or for cases where another (not bss) section follows data, but you get the idea:

file=open('program.asm','r+')
lista= file.readlines()

data_start = lista.index("section .data\n")
bss_start = lista.index("section .bss\n")
# Print all lines starting from data section and until bss
print("\n".join(i.strip() for i in lista[data_start:bss_start]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.