Tuesday, June 26, 2007

Disassembly The Harpy package also contains a disassembler, so let's put it to work.
disasm :: e -> s -> CodeGen e s a -> IO String
disasm e s cg = do
    let cg' = do cg; getCodeBufferList
    (_, r) <- runCodeGen cg' e s
    case r of
        Left msg -> error (show msg)
        Right bs -> fmap concat $ mapM disAsm bs
  where disAsm (ptr, n) = do
            r <- disassembleBlock ptr n
            case r of
                Left msg -> error $ show msg
                Right insns -> return $ unlines $ map showIntel insns
Using the same example as in the last post we get.
main = do
    let fun x = (x+1) * x `quot` 2
    str <- disasmIO fun
    putStr str
And here is the sad truth:
003d6d20  53                            push   ebx
003d6d21  51                            push   ecx
003d6d22  52                            push   edx
003d6d23  8b 44 24 10                   mov    eax,dword ptr [esp+16]
003d6d27  50                            push   eax
003d6d28  6a 01                         push   1H
003d6d2a  5b                            pop    ebx
003d6d2b  58                            pop    eax
003d6d2c  03 c3                         add    eax,ebx
003d6d2e  50                            push   eax
003d6d2f  8b 44 24 14                   mov    eax,dword ptr [esp+20]
003d6d33  50                            push   eax
003d6d34  5b                            pop    ebx
003d6d35  58                            pop    eax
003d6d36  0f af c3                      imul   eax,ebx
003d6d39  50                            push   eax
003d6d3a  6a 02                         push   2H
003d6d3c  5b                            pop    ebx
003d6d3d  58                            pop    eax
003d6d3e  8b d0                         mov    edx,eax
003d6d40  c1 fa 1f                      sar    edx,1fH
003d6d43  f7 fb                         idiv   eax,ebx
003d6d45  50                            push   eax
003d6d46  58                            pop    eax
003d6d47  5a                            pop    edx
003d6d48  59                            pop    ecx
003d6d49  5b                            pop    ebx
003d6d4a  c3                            ret

Labels: ,


Blogger sigfpe said...

push eax
pop eax

Thursday, June 28, 2007 at 6:14:00 PM GMT+1  
Blogger augustss said...

I said it would make any compiler writer weep. :)

No worries, I'll make it better.

Thursday, June 28, 2007 at 10:35:00 PM GMT+1  

Post a Comment

<< Home