program FixAsm;

var FileToFix : text;
    FileLine : string;
    Where : byte;
    InsideBss : Boolean;
    WhichNumber, ErrCode : Integer;

procedure Usage;
begin
  Writeln('USAGE: FixAsm <name of file to fix>');
  Halt;
end;

begin
  If ParamCount <> 1 Then Usage;
  InsideBss := FALSE;
  Assign(FileToFix, ParamStr(1));
  Reset(FileToFix);
  Writeln('GLOBAL _main');
  While NOT Eof(FileToFix) Do Begin
    ReadLn(FileToFix, FileLine);
    If InsideBss Then Begin
      Where := Pos('	DB	', FileLine);
      If Where > 0 Then Begin
        Delete(FileLine, Where, 4);
        Insert('	RESB	', FileLine, Where);
      End;
    End
    Else Begin
      Where := Pos('offset [', FileLine);
      If Where > 0 Then Begin
        Delete(FileLine, Where, 8);
        Insert('dword ', FileLine, Where);
        FileLine[Pos(']', FileLine)] := ' ';
      End
      Else Begin
        Where := Pos(' push', FileLine);
        If Where > 0 Then Begin
          Val(Copy(FileLine, Where+6, 255), WhichNumber, ErrCode);
          If ErrCode = 0 Then Insert('dword ', FileLine, Where+6);
        End
        Else Begin
          Where := Pos('st( 0)', FileLine);
          If Where > 0 Then Begin
            FileLine[Where+2] := '0';
            Delete(FileLine,Where+3,3)
          End
          Else If FileLine = 'SECTION .bss' Then InsideBss := TRUE
        End;
      End;
    End;
    Writeln(FileLine);
  End; { While }
  Close(FileToFix);
end.
