Instantly share code, notes, and snippets.

@bdurrani bdurrani/parsebytecode.fs
Last active Sep 25, 2015

Embed
What would you like to do?
Parse the output of objdump -d -M intel and print out the byte code as an array of hex bytes
open System
open System.Diagnostics
open System.Globalization
let inline flip f x y = f y x
let parseLine(input:string) =
let trimmed = input.Trim()
if String.IsNullOrWhiteSpace trimmed || (trimmed.Contains("\t") |> not) then
Array.create 0 ""
else
let temp = trimmed.Split[|'\t'|]
|> Array.filter (String.IsNullOrWhiteSpace >> not)
|> flip Array.get 1
temp.Trim().Split[|' ';'\t'|]
|> Array.filter (String.IsNullOrWhiteSpace >> not)
|> Array.filter (fun x-> x.Length = 2 )
|> Array.filter ( fun x ->
let x,_ = Byte.TryParse (x ,NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture)
x
)
[<EntryPoint>]
let main argv =
let lines =
argv.[0]
|> IO.File.ReadAllLines
|> Seq.skipWhile(fun x -> (x.Contains(@"<main>") |> not) )
|> Seq.skip 1
|> Seq.skipWhile( String.IsNullOrEmpty)
|> Seq.collect parseLine
|> Seq.map (fun x -> @"0x" + x + @",")
|> Seq.fold (fun acc curr -> acc + curr) ""
Debug.WriteLine(lines)
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment