假设我想编写一个以二进制形式显示“hello world”的程序,我该如何做?
我有一些想法:
- 我需要确定我正在使用哪种芯片架构 - 我需要了解它使用的二进制类型 - 我需要一些关于这种二进制类型的参考资料 - 我可能需要更改编辑器(Vim)中的某些设置。
有人可以帮我详细说明吗?
我有一些想法:
- 我需要确定我正在使用哪种芯片架构 - 我需要了解它使用的二进制类型 - 我需要一些关于这种二进制类型的参考资料 - 我可能需要更改编辑器(Vim)中的某些设置。
有人可以帮我详细说明吗?
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
在32位Linux上编译后,生成的二进制文件只有360字节,尽管其中大部分都是零:
00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 03 00 01 00 00 00 80 80 04 08 34 00 00 00 |............4...|
00000020 c8 00 00 00 00 00 00 00 34 00 20 00 02 00 28 00 |........4. ...(.|
00000030 04 00 03 00 01 00 00 00 00 00 00 00 00 80 04 08 |................|
00000040 00 80 04 08 9d 00 00 00 9d 00 00 00 05 00 00 00 |................|
00000050 00 10 00 00 01 00 00 00 a0 00 00 00 a0 90 04 08 |................|
00000060 a0 90 04 08 0e 00 00 00 0e 00 00 00 06 00 00 00 |................|
00000070 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000080 ba 0e 00 00 00 b9 a0 90 04 08 bb 01 00 00 00 b8 |................|
00000090 04 00 00 00 cd 80 b8 01 00 00 00 cd 80 00 00 00 |................|
000000a0 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a 00 2e |Hello, world!...|
000000b0 73 68 73 74 72 74 61 62 00 2e 74 65 78 74 00 2e |shstrtab..text..|
000000c0 64 61 74 61 00 00 00 00 00 00 00 00 00 00 00 00 |data............|
000000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000000f0 0b 00 00 00 01 00 00 00 06 00 00 00 80 80 04 08 |................|
00000100 80 00 00 00 1d 00 00 00 00 00 00 00 00 00 00 00 |................|
00000110 10 00 00 00 00 00 00 00 11 00 00 00 01 00 00 00 |................|
00000120 03 00 00 00 a0 90 04 08 a0 00 00 00 0e 00 00 00 |................|
00000130 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 |................|
00000140 01 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 |................|
00000150 ae 00 00 00 17 00 00 00 00 00 00 00 00 00 00 00 |................|
00000160 01 00 00 00 00 00 00 00 |........|
由于您想要“手工编译”,这基本上意味着将汇编助记符翻译成其操作码,然后将结果包装在正确的二进制格式中(如上面的ELF示例)
更新:正如@adam-rosenfield所展示的这个答案, “Hello, world!” 的 ELF 二进制文件可以手工制作到116个字节。原始答案现已删除,但对于管理员仍可见,因此这里有一份副本:
Here's a 32-byte version using Linux system calls:
.globl _start _start: movb $4, %al xor %ebx, %ebx inc %ebx movl $hello, %ecx xor %edx, %edx movb $11, %dl int $0x80 ;;; sys_write(1, $hello, 11) xor %eax, %eax inc %eax int $0x80 ;;; sys_exit(something) hello: .ascii "Hello world"
When compiled into a minimal ELF file, the full executable is 116 bytes:
00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............| 00000010 02 00 03 00 01 00 00 00 54 80 04 08 34 00 00 00 |........T...4...| 00000020 00 00 00 00 00 00 00 00 34 00 20 00 01 00 00 00 |........4. .....| 00000030 00 00 00 00 01 00 00 00 00 00 00 00 00 80 04 08 |................| 00000040 00 80 04 08 74 00 00 00 74 00 00 00 05 00 00 00 |....t...t.......| 00000050 00 10 00 00 b0 04 31 db 43 b9 69 80 04 08 31 d2 |......1.C.i...1.| 00000060 b2 0b cd 80 31 c0 40 cd 80 48 65 6c 6c 6f 20 77 |....1.@..Hello w| 00000070 6f 72 6c 64 |orld| 00000074