How to write and executable Windows .exe manually (machine code with Hex editor)?

后端 未结 6 505
野趣味
野趣味 2021-01-30 07:06

I\'d like to know how is it possible to write something as simple as an Hello World program just by using an Hex Editor. I know that I could use an assembler and assembly langua

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 07:38

    you can do a disassembly and try figure out the machine code for the opcodes you use in your assembler

    for example

    org 0x100
    mov dx,msg
    mov ah,0x09
    int 0x21
    ret
    msg db 'hello$'
    

    compiled with nasm -fbin ./a.asm -o ./a.com has ndisasm a.com deliver the following disassembly:

    00000000  BA0801            mov dx,0x108
    00000003  B409              mov ah,0x9
    00000005  CD21              int 0x21
    00000007  C3                ret
    00000008  68656C            push word 0x6c65
    0000000B  6C                insb
    0000000C  6F                outsw
    0000000D  24                db 0x24
    
    00000000 to 00000007 are the instructions
    

    so you can play with the ba0801 machine code, using some hex editor, try changing it to ba0901, and only 'ello' will be printed, you can play around with your hex editor and pad stuff out with NOP, which is 0x90 in machine code, for example:

    00000000:  ba 50 01 90 90 90 90 90  90 90 90 90 90 90 90 90  .@..............
    00000010:  b4 09 90 90 90 90 90 90  90 90 90 90 90 90 90 90  ................
    00000020:  cd 21 90 90 90 90 90 90  90 90 90 90 90 90 90 90  .!..............
    00000030:  c3 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90  ................
    00000040:  71 77 65 72 74 79 75 69  61 73 64 66 67 68 6a 24  qwertyuiasdfghj$
    00000050:  61 73 64 66 67 68 6a 6b  61 73 64 66 67 68 6a 24  asdfghjkasdfghj$
    00000060:  -- -- -- -- -- -- -- --  -- -- -- -- -- -- -- --  ----------------
    

    if you save this with the extension .com you can run it in DosBox

提交回复
热议问题