Skip to content

Instantly share code, notes, and snippets.

@7shi 7shi/disasm1.fsx
Created Apr 12, 2014

Embed
What would you like to do?
[F#]機械語入門で書いたコード
let aout = System.IO.File.ReadAllBytes "../../write-3.out"
let read16 (a:byte[]) i = ((int a.[i + 1]) <<< 8) ||| (int a.[i])
let tsize = read16 aout 2
let dsize = read16 aout 4
printfn "tsize = %04x, dsize = %04x" tsize dsize
let mem = aout.[16 .. 16 + tsize + dsize - 1]
let mutable i = 0
let show len =
printf "%04x: " i
for j in 0 .. 5 do
if j < len then
printf "%02x" mem.[i + j]
else
printf " "
i <- i + len
while i < tsize do
match int mem.[i], int mem.[i + 1] with
| 0xb8, _ ->
show 3
printfn "mov ax, %04x" (read16 mem (i - 2))
| 0xbb, _ ->
show 3
printfn "mov bx, %04x" (read16 mem (i - 2))
| 0xc6, 0x07 ->
show 3
printfn "mov byte [bx], %02x" mem.[i - 1]
| 0xc6, 0x47 ->
show 4
printfn "mov byte [bx+%x], %02x" mem.[i - 2] mem.[i - 1]
| 0xc7, 0x07 ->
show 4
printfn "mov [bx], %04x" (read16 mem (i - 2))
| 0xc7, 0x47 ->
show 5
printfn "mov [bx+%x], %04x" mem.[i - 3] (read16 mem (i - 2))
| 0xcd, 0x07 ->
show 2
printfn "int %x" mem.[i - 1]
show 1
match int mem.[i - 1] with
| 1 ->
printfn "; sys exit"
| 4 ->
printfn "; sys write"
for j in 0..1 do
show 2
printfn "; arg"
| _ ->
printfn "; sys ???"
| _ ->
show 1
printfn "???"
let aout = System.IO.File.ReadAllBytes "../../write-5.out"
let read16 (src:byte[]) index =
(int src.[index]) ||| ((int src.[index + 1]) <<< 8)
let tsize = read16 aout 2
let dsize = read16 aout 4
printfn "tsize = %x, dsize = %x" tsize dsize
let mem = aout.[16 .. 16 + tsize + dsize - 1]
let mutable i = 0
let show len =
printf "%04x: " i
for j in 0 .. 6 do
if j < len then
printf "%02x" mem.[i + j]
else
printf " "
i <- i + len
let syscalls = [|
0, "indir"
0, "exit"
0, "fork"
2, "read"
2, "write" |]
while i < tsize do
match int mem.[i], int mem.[i + 1] with
| 0x88, 0x07 ->
show 2; printfn "mov [bx], al"
| 0x88, 0x67 ->
show 3; printfn "mov [bx+%x], ah" mem.[i - 1]
| 0x89, 0x07 ->
show 2; printfn "mov [bx], ax"
| 0x89, 0x0f ->
show 2; printfn "mov [bx], cx"
| 0x89, 0x4f ->
show 3; printfn "mov [bx+%x], cx" mem.[i - 1]
| 0xb1, _ ->
show 2; printfn "mov cl, %02x" mem.[i - 1]
| 0xb5, _ ->
show 2; printfn "mov ch, %02x" mem.[i - 1]
| 0xb8, _ ->
show 3; printfn "mov ax, %04x" (read16 mem (i - 2))
| 0xb9, _ ->
show 3; printfn "mov cx, %04x" (read16 mem (i - 2))
| 0xbb, _ ->
show 3; printfn "mov bx, %04x" (read16 mem (i - 2))
| 0xc6, 0x07 ->
show 3; printfn "mov byte [bx], %02x" mem.[i - 1]
| 0xc6, 0x47 ->
show 4; printfn "mov byte [bx+%x], %02x" mem.[i - 2] mem.[i - 1]
| 0xc7, 0x07 ->
show 4; printfn "mov [bx], %04x" (read16 mem (i - 2))
| 0xc7, 0x47 ->
show 5; printfn "mov [bx+%x], %04x" mem.[i - 3] (read16 mem (i - 2))
| 0xcd, 0x07 ->
show 2; printfn "int %x" mem.[i - 1]
let sc = syscalls.[int mem.[i]]
show 1; printfn "; sys %s" (snd sc)
for j = 1 to fst sc do
show 2; printfn "; arg"
| _ ->
show 1; printfn "???"
let aout = System.IO.File.ReadAllBytes "../../write-3.out"
let read16 (a:byte[]) i = ((int a.[i + 1]) <<< 8) ||| (int a.[i])
let tsize = read16 aout 2
let dsize = read16 aout 4
let mem = aout.[16 .. 16 + tsize + dsize - 1]
let mutable ip, ax, bx = 0, 0, 0
while ip < tsize do
match int mem.[ip], int mem.[ip + 1] with
| 0xb8, _ ->
ip <- ip + 3
ax <- read16 mem (ip - 2)
| 0xbb, _ ->
ip <- ip + 3
bx <- read16 mem (ip - 2)
| 0xc6, 0x07 ->
ip <- ip + 3
mem.[bx] <- mem.[ip - 1]
| 0xc6, 0x47 ->
ip <- ip + 4
let addr = bx + (int mem.[ip - 2])
mem.[addr] <- mem.[ip - 1]
| 0xc7, 0x07 ->
ip <- ip + 4
mem.[bx] <- mem.[ip - 2]
mem.[bx + 1] <- mem.[ip - 1]
| 0xc7, 0x47 ->
ip <- ip + 5
let addr = bx + (int mem.[ip - 3])
mem.[addr] <- mem.[ip - 2]
mem.[addr + 1] <- mem.[ip - 1]
| 0xcd, 0x07 ->
ip <- ip + 3
match int mem.[ip - 1] with
| 1 ->
exit ax
| 4 ->
let p = read16 mem ip
let len = read16 mem (ip + 2)
ip <- ip + 4
let data = mem.[p .. p + len - 1]
for b in data do
printf "%c" (char b)
| _ ->
printfn "sys ???"
exit 1
| _ ->
printfn "???"
exit 1
let aout = System.IO.File.ReadAllBytes "../../write-5.out"
let read16 (src:byte[]) index =
(int src.[index]) ||| ((int src.[index + 1]) <<< 8)
let tsize = read16 aout 2
let dsize = read16 aout 4
let mem = aout.[16 .. 16 + tsize + dsize - 1]
let mutable ip, ax, bx, cx = 0, 0, 0, 0
while ip < tsize do
match int mem.[ip], int mem.[ip + 1] with
| 0x88, 0x07 ->
mem.[bx] <- byte ax // al
ip <- ip + 2
| 0x88, 0x67 ->
let addr = bx + int mem.[ip + 2]
mem.[addr] <- byte (ax >>> 8) // ah
ip <- ip + 3
| 0x89, 0x07 ->
mem.[bx ] <- byte ax
mem.[bx + 1] <- byte (ax >>> 8)
ip <- ip + 2
| 0x89, 0x0f ->
mem.[bx ] <- byte cx
mem.[bx + 1] <- byte (cx >>> 8)
ip <- ip + 2
| 0x89, 0x4f ->
let addr = bx + int mem.[ip + 2]
mem.[addr ] <- byte cx
mem.[addr + 1] <- byte (cx >>> 8)
ip <- ip + 3
| 0xb1, _ -> // cl
cx <- (cx >>> 8 <<< 8) ||| int mem.[ip + 1]
ip <- ip + 2
| 0xb5, _ -> // ch
cx <- ((int mem.[ip + 1]) <<< 8) ||| int (byte cx)
ip <- ip + 2
| 0xb8, _ ->
ax <- read16 mem (ip + 1)
ip <- ip + 3
| 0xb9, _ ->
cx <- read16 mem (ip + 1)
ip <- ip + 3
| 0xbb, _ ->
bx <- read16 mem (ip + 1)
ip <- ip + 3
| 0xc6, 0x07 ->
mem.[bx] <- mem.[ip + 2]
ip <- ip + 3
| 0xc6, 0x47 ->
let addr = bx + int mem.[ip + 2]
mem.[addr] <- mem.[ip + 3]
ip <- ip + 4
| 0xc7, 0x07 ->
mem.[bx ] <- mem.[ip + 2]
mem.[bx + 1] <- mem.[ip + 3]
ip <- ip + 4
| 0xc7, 0x47 ->
let addr = bx + int mem.[ip + 2]
mem.[addr ] <- mem.[ip + 3]
mem.[addr + 1] <- mem.[ip + 4]
ip <- ip + 5
| 0xcd, 0x07 ->
match int mem.[ip + 2] with
| 1 ->
exit ax
| 4 ->
let addr = read16 mem (ip + 3)
let len = read16 mem (ip + 5)
let buf = mem.[addr .. addr + len - 1]
printf "%s" (System.Text.Encoding.ASCII.GetString buf)
ip <- ip + 7
| _ ->
printfn "??? syscall %d" mem.[ip + 2]
exit 1
| _ ->
printfn "??? %02x" mem.[ip]
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.