0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[アセンブラ] sectionを整列するプログラム 'secsort.py'

Last updated at Posted at 2024-10-25

axxに必要があって作りました。

例えば、

section .text
    mov rax,1
section .data
    .asciiz "test1"
section .text
    mov dsi,2
section .data
    db 10

のようなアセンブリリストを、

section .text
    mov rax,1
    mov dsi,2
section .data
    .asciiz "test1"
    db 10

のように並べ変えます。

$ ./secsort.py infile.s >outfile.sのように使ってください。

本体

secsort.py
#!/usr/bin/python
import sys

pas=0
current_section=".text"
sections={ '.text' :['']}
capital="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lower="abcdefghijklmnopqrstuvwxyz"
digit='0123456789'
alphabet=lower+capital
lwordchars=lower+capital+digit+"_"


def skipspc(s,idx):
    while len(s)>idx:
        if s[idx]==' ':
            idx+=1
            continue
        break
    return idx


def get_label_word(s,idx):
    idx=skipspc(s,idx)
    t=""
    if len(s)>idx and (s[idx]=='.' or s[idx] in lwordchars):
        t=s[idx]
        idx+=1
        while len(s)>idx:
            if not s[idx] in lwordchars: 
                break
            t+=s[idx]
            idx+=1
    return t,idx

def readsections(fn):
    global sections,current_section
    with open(fn,"rt") as file:
        while True:
            l=file.readline().replace(chr(13),'')
            if l=='':
                return True
            l=l.strip()

            (sec,idx)=get_label_word(l,0)
            if sec.upper()=="SECTION" or sec.upper()=="SEGMENT":
                (secname,idx)=get_label_word(l,idx)
                if secname==current_section:
                    continue
                else:
                    current_section=secname
                    continue
            elif sec.upper()==".LABELC":
                idx=skipspc(l,idx)
                ss=""
                while True:
                    if len(l)<=idx or l[idx]=='"':
                        break
                    ss+=l[idx]
                    idx+=1
                lwordchars=alphabet+digit+ss+"_"
                continue
            elif sec.upper()==".INCLUDE":
                idx=skipspc(l,idx)
                if l[idx]=='"':
                    idx+=1
                fn=""
                while True:
                    if len(l)<=idx or l[idx]=='"':
                        break
                    fn+=l[idx]
                    idx+=1
                readsections(fn)
            else:
                if current_section in sections:
                    sections[current_section]=sections[current_section]+[l]
                else:
                    sections[current_section]=[l]

def writesections():
    l=list(sections.items())
    for i in l:
        (a,b)=i
        print("section    ",a)
        for k in b:
            print(k)

if __name__=='__main__':
    readsections(sys.argv[1])
    writesections()

sortと言っても、sectionは見つけた順番に整列されます。

.includeにも対応しています。

.include "file.s"

とすると、"file.s"をインクルードします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?