操作系统学习初步-gcc输出汇编标号处理程序

操作系统学习初步-gcc输出汇编标号处理程序

当用Ada语言或C语言写的代码输出汇编文件后,多个文件中可能存在着重复的标号。这将导致用as汇编器同时编译多个文件时出错,如下:

LFE10:
LFB10:
L10:

下列Ada程序将前2种标号去除,第3种标号加 _文件名 前缀,这样不同的文件名前缀将可以避免冲突。

with ada.Command_Line;      use ada.Command_Line;
with ada.Text_IO;
with ada.Streams.Stream_IO; use ada.Streams.Stream_IO;
with ada.Unchecked_Deallocation;
procedure RemoveSign is
   type ASTR is access all string;
   procedure Free is new ada.Unchecked_Deallocation(string,ASTR);
   function load(file:in string) return ASTR is
      use ada.Streams.Stream_IO;
      pFile:File_Type;
      fsize:count;
      St   :Stream_Access;
      src  :ASTR:=Null;
   begin
      open(pFile,in_file,file);
      fsize:=size(pFile);
      St:=stream(pFile);
      src:=new string(1..Integer(fsize));
      string'read(St,Src.all);
      close(pFile);
      return src;
   exception when others=>
         return Null;
   end load;

   function GetName(S:string) return string is
      last:Integer;
      first:Integer:=-1;
   begin
      for i in reverse S'range loop
         if S(i)='.' then
            last:=i-1;
         elsif S(i)='/' or S(i)='\' then
            first:=i+1;
            exit;
         end if;
      end loop;
      if first=-1 then
         first:=S'first;
      end if;
      declare
         s0:string(1..last-first+4):="__"&s(first..last)&"_";
      begin
         return S0;
      end;
   end GetName;

begin
   if Argument_Count<=1 then
      ada.Text_IO.Put_Line("Remove unused sign in asm source file");
      ada.Text_IO.put_line("Useage:RemoveSign File1 File2...");
   else
      for i in 1..Argument_Count loop
        declare
            File1:ada.Streams.Stream_IO.File_Type;
            S1:Stream_Access;
            Src  :ASTR:=load(Argument(i));
            FN:string:=GetName(Argument(i));
        begin
           open(File1,out_File,Argument(i));
           S1:=Stream(File1);
            for j in src'range loop
              if j+2<=src'last and then (src(j..j+2)="LFE" or src(j..j+2)="LFB") and then (src(j-1)=ASCII.CR or src(j-1)=ASCII.LF) then
                  for k in j..src'last loop
                     if src(k)=':' then
                        src(k):=' ';
                        exit;
                     else
                        src(k):=' ';
                     end if;
                  end loop;
              end if;
            end loop;
            for j in src'range loop
               if src(j)/='L' then
                  character'write(s1,src(j));
               else
                  if (j-1>=src'first and then (src(j-1)=' ' or src(j-1)=character'val(9) or src(j-1)=ASCII.CR or src(j-1)=ASCII.LF)) and  (j+1<=src'last and then src(j+1) in '0'..'9') then
                     string'write(S1,FN&src(j));
                  else
                     character'write(s1,src(j));
                  end if;
               end if;
            end loop;
           close(File1);
         end;
      end loop;
   end if;
end RemoveSign;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值