假设我想编写一个程序来显示“hello world”,我想用二进制编写它。我怎么能这样做?
我有一点想法:
有人可以引导我解决这个问题吗?
答案 0 :(得分:22)
它有点复杂,因为实际打印“Hello,world!” stdout是一个系统调用,因此你需要知道正确的内核系统调用号。这当然因操作系统而异。您还需要知道二进制格式,这种格式也会有所不同,尽管ELF (Executable and Linkable Format)在Unix和Linux的几种版本中都是通用的。
这是Linux汇编程序代码:
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 |........|
由于您希望“手动编译”,这基本上意味着translating assembler mnemonics above to their opcodes,然后将结果包装成正确的二进制格式(上例中的ELF)
更新:@ {adam-rosenfield的this answer shows,“Hello,world!”的ELF二进制文件可以手工制作,最低可达116个字节。
这是使用Linux系统调用的32字节版本:
.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"
当编译成最小的ELF文件时,完整的可执行文件是116 字节数:
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
答案 1 :(得分:3)
通常,您可以使用十六进制编辑器。找出汇编代码,手工组装它,使用十六进制编辑器输入二进制值,然后将它们保存到文件中。获得文件后,放入计算机监视器并将文件加载到可用地址,然后跳转到第一条指令。这在单板计算机上很常见,现在仍然在微控制器上完成,但这不是你在现代操作系统上要做的事情。如果你真的想这样做,我建议运行一个低级仿真器(SIMH可以工作)或者使用微控制器(你可以选择TI MSP430开发套件少于五个钱)。
答案 2 :(得分:-1)
制作一个二进制字符串并输入 value= string str="0100100001100101011011000110110001101111100100000010101110110111011100100110110001100100";