Skip to content

Instantly share code, notes, and snippets.

@satokjp
Last active December 20, 2015 07:59
Show Gist options
  • Save satokjp/6097613 to your computer and use it in GitHub Desktop.
Save satokjp/6097613 to your computer and use it in GitHub Desktop.
PDP-11 binary F# Mono Xamarin Mac
open System.IO
open System.Text
let mem = Array.zeroCreate<byte>(0x10000)
mem.[0] <- 0x10uy
mem.[1] <- byte 0x20
printfn "0:%d %02x" mem.[0] mem.[0]
printfn "1:%d %02x" mem.[1] mem.[1]
let read16 i = (uint16 mem.[i]) ||| ( (uint16 mem.[i+1] ) <<< 8 )
printfn "read16 0 = %04x" (read16 0)
//let args = System.Environment.GetCommandLineArgs()
let f = new FileStream("a.out" , FileMode.Open)
f.Read( mem, 0, 16) |> ignore
let textsize = int (read16 2)
let datasize = int (read16 4)
f.Read( mem, 0, textsize + datasize) |> ignore
f.Close()
let disasm () =
// .text
let mutable i = 0
while i < textsize do
printf "%04x %04x \t" i (read16 i)
match int mem.[i+1], int mem.[i] with
| 0x15, 0xc0 ->
i <- i + 2
printfn "mov $%x, r0" (read16 i)
| 0x89, n ->
printfn "sys %d" n
| 0x00, 0x06 ->
printfn "rtt"
| _ ->
printfn ".word %o" (read16 i)
i <- i + 2
let run () =
let mutable i = 0
let mutable r0 = uint16 0
while i < textsize do
match int mem.[i+1], int mem.[i] with
| 0x15, 0xc0 ->
i <- i + 2
r0 <- read16 i
| 0x89, 0x04 ->
let str = int (read16 (i + 2))
let len = int (read16 (i + 4))
i <- i + 4
printf "%s" (Encoding.ASCII.GetString(mem, str, len))
//printfn "write(%x, %x, %x)" r0 str len
| 0x89, 0x01 ->
exit (int r0)
| _ ->
printfn "error %x" i
i <- i + 2
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment