[Python] Python setting doesnt make sense [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    The following code is a assembler I had made for a custom microprocessor designed in Logisim.

    filename = input('Give me your program')
    file = open(filename,'r')
    encoded =''
    binary = open(filename+'Binary','w')
    i=1
    dictionary = {}
    for line2 in file:
    if line2.isspace():
    continue
    farr = line2.split(':')
    print(farr)
    if farr[0]=='set':
    dictionary[farr[1]] = i
    continue
    i=i+1




    print(dictionary)

    for line in file:
    line1 =''
    if line.isspace():
    continue
    arr = line.split(':')

    if arr[0]=='move':
    line1+='00000001'
    elif arr[0]=='not':
    line1+='00000010'
    elif arr[0]=='and':
    line1+='00000011'
    elif arr[0]=='or':
    line1+='00000100'
    elif arr[0]=='xor':
    line1+='00000101'
    elif arr[0]=='xnor':
    line1+='00000110'
    elif arr[0]=='and16way':
    line1+='00000111'
    elif arr[0]=='or16way':
    line1+='00001000'
    elif arr[0]=='xor16way':
    line1+='00001001'
    elif arr[0]=='xnor16way':
    line1+='00001010'
    elif arr[0]=='shiftRight':
    line1+='00001011'
    elif arr[0]=='shiftLeft':
    line1+='00001100'
    elif arr[0]=='rotateRight':
    line1+='00001101'
    elif arr[0]=='rotateLeft':
    line1+='00001110'
    elif arr[0]=='regval':
    line1+='00001111'
    elif arr[0]=='add':
    line1+='00010000'
    elif arr[0]=='sub':
    line1+='00010001'
    elif arr[0]=='muldec':
    line1+='00010010'
    elif arr[0]=='mulint':
    line1+='00010011'
    elif arr[0]=='div':
    line1+='00010100'
    elif arr[0]=='takeint':
    line1+='00010101'
    elif arr[0]=='takedec':
    line1+='00010110'
    elif arr[0]=='jump':
    line1+='00100000'
    elif arr[0]=='jumpeq':
    line1+='00100010'
    elif arr[0]=='jumpab':
    line1+='00100100'
    elif arr[0]=='jumpbel':
    line1+='00101000'
    elif arr[0]=='jumpneq':
    line1+='00110010'
    elif arr[0]=='jumpnab':
    line1+='00110100'
    elif arr[0]=='jumpnbel':
    line1+='00111000'
    elif arr[0]=='push':
    line1+='01000000'
    elif arr[0]=='pop':
    line1+='01000001'
    elif arr[0]=='store':
    line1+='10000000'
    elif arr[0]=='load':
    line1+='10000001'
    number = 1
    while number<len(arr):

    arg = arr[number]
    arg = arg.replace('/', '')
    if arr[0]=='jump':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'




    elif arr[0]=='jumpeq':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'



    elif arr[0]=='jumpab':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'



    elif arr[0]=='jumpbel':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'



    elif arr[0]=='jumpneq':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'





    elif arr[0]=='jumpnab':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'


    elif arr[0]=='jumpnbel':

    num = int(dictionary[arg])
    num = num*16

    arg=f'{num:024b}'


    else:

    if arg.startswith('R'):
    arg = arg.replace('R','')
    num = int(arg)

    arg = f'{num:05b}'

    elif arg.startswith('N'):
    arg = arg.replace('N','')
    num = int(arg)

    arg = f'{num:016b}'




    line1+=arg

    number += 1

    if len(line1)<32:

    for i in range(len(line1),32):

    line1+='0'








    binary.write(line1)
    binary.write('\n')


    The previous version of the assembler didnt have any labels,you had to specify the number of the line in case of a branching instruction

    I decided to add labels by using 2 blocks 1 for the labels to be put in a dictionary along with their respective lines.My issue is that the second for loop which does the assembly into a binary format is never executed.

    I run the script.py and for this file:

    regval:/R10:N0

    regval:/R1:N1

    move:/R0:/R10

    shiftRight:/R2:/R0

    set:FirstLoop

    regval:/R3:N1

    add:/R3:/R3:/R1

    set:SecondLoop

    regval:/R4:N1

    add:/R4:/R4:/R1

    mulint:/R5:/R4:/R3

    sub:/R8:/R5:/R0

    jumpeq:SetExit1

    sub:/R7:/R2:/R3

    jumpbel:SetExit2

    sub:/R6:/R2:/R4

    jumpnbel:SecondLoop

    set:SetExit1

    sub:/R6:/R2:/R4

    jumpbel:FirstLoop

    set:SetExit2

    regval:/R11:N0

    jump:SetExit3

    regval:/R11:N1

    set:SetExit3

    move:/R2:/R2


    I get this output:

    ['regval', '/R10', 'N0\n']
    ['regval', '/R1', 'N1\n']
    ['move', '/R0', '/R10\n']
    ['shiftRight', '/R2', '/R0\n']
    ['set', 'FirstLoop\n']
    ['regval', '/R3', 'N1\n']
    ['add', '/R3', '/R3', '/R1\n']
    ['set', 'SecondLoop\n']
    ['regval', '/R4', 'N1\n']
    ['add', '/R4', '/R4', '/R1\n']
    ['mulint', '/R5', '/R4', '/R3\n']
    ['sub', '/R8', '/R5', '/R0\n']
    ['jumpeq', 'SetExit1\n']
    ['sub', '/R7', '/R2', '/R3\n']
    ['jumpbel', 'SetExit2\n']
    ['sub', '/R6', '/R2', '/R4\n']
    ['jumpnbel', 'SecondLoop\n']
    ['set', 'SetExit1\n']
    ['sub', '/R6', '/R2', '/R4\n']
    ['jumpbel', 'FirstLoop\n']
    ['set', 'SetExit2\n']
    ['regval', '/R11', 'N0\n']
    ['jump', 'SetExit3\n']
    ['regval', '/R11', 'N1\n']
    ['set', 'SetExit3\n']
    ['move', '/R2', '/R2']
    {'FirstLoop\n': 5, 'SecondLoop\n': 7, 'SetExit1\n': 16, 'SetExit2\n': 18, 'SetExit3\n': 21}



    which means that the second loop is never executed and thus the binary file is empty.I suspect it is a space-sensitive issue since python is space-sensitive but I dont understand the first for loop starts at the same character horizontally with the second for loop.

    Continue reading...

Compartilhe esta Página