我怎么能用二进制写"hello world"?

Nat*_*ong 14 binary

假设我想编写一个显示"hello world"的程序,我想用二进制编写它.我怎么能这样做?

我有一点想法:

  • 我需要确定我正在使用的芯片架构
  • 我需要找出它使用的二进制文件
  • 我需要一些二进制的参考
  • 我可能需要在编辑器中更改设置(Vim)

任何人都可以带我走过这个吗?

var*_*tec 22

它有点复杂,因为实际打印"Hello,world!" stdout是一个系统调用,因此你需要知道正确的内核系统调用号.这当然因操作系统而异.此外,您还需要知道二进制格式,这种格式也会有所不同,尽管ELF(可执行文件和可链接格式)在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
Run Code Online (Sandbox Code Playgroud)

...在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                           |........|
Run Code Online (Sandbox Code Playgroud)

由于您希望"手动编译",这基本上意味着将上面的汇编程序助记符转换为它们的操作码,然后将结果包装成正确的二进制格式(上例中的ELF)

更新:正如@ adam-rosenfield的答案所示,ELF二进制文件为"Hello,world!" 可以手工制作低至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" 
Run Code Online (Sandbox Code Playgroud)

当编译成最小的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