cj1128.github.io icon indicating copy to clipboard operation
cj1128.github.io copied to clipboard

编写一个最小的 64 位 Hello World

Open cj1128 opened this issue 4 years ago • 17 comments

https://cjting.me/2020/12/10/tiny-x64-helloworld/

编写一个最小的 64 位 Hello World

cj1128 avatar Dec 09 '20 17:12 cj1128

好棒的分析!佩服你的研究精神 話說我之前一直以為Shebang是由目前所在的Shell去解析的

typebrook avatar Dec 18 '20 02:12 typebrook

喜欢介绍debug到每一个字节的文章. 喜欢blog的形式, 喜欢gitalk的hack行为. 这三份喜欢加在一起, internet是一个真实的世界呢

hcen1997 avatar Dec 18 '20 02:12 hcen1997

cool !!!

kang8 avatar Dec 18 '20 02:12 kang8

nb

ThreeZhiWang avatar Dec 18 '20 03:12 ThreeZhiWang

nb

energygreek avatar Dec 18 '20 07:12 energygreek

赞赞赞👍

hutusi avatar Dec 18 '20 14:12 hutusi

赞赞赞👍

bloatfan avatar Dec 19 '20 17:12 bloatfan

很有意思,感谢分享

hf-hf avatar Dec 22 '20 06:12 hf-hf

说明 php 是世界上最好的语言[狗头]

mebtte avatar Dec 24 '20 09:12 mebtte

ruanjf avatar Jan 12 '21 02:01 ruanjf

能感到洋溢出来的开心(

ofey404 avatar Jan 25 '21 04:01 ofey404

php那个 把 /p 添加env 是不是直接 #!p

gftao avatar Feb 01 '21 10:02 gftao

好奇你做的啥工作啊,我要毕业了,很好奇大佬些平时咋样的

fuchengjie avatar Jul 12 '21 08:07 fuchengjie

amanoooo avatar Nov 01 '21 03:11 amanoooo

最后汇编部分还可以继续优化,比如

push 1
pop rax
mov rdi, rax
mov rsi, message
push 13
pop rdx
syscall
push 60
pop rax
xor rdi, rdi
syscall

可以再省8个字节

regomne avatar Jun 24 '22 09:06 regomne

出了 @regomne 所说的部分,还可以:

  1. 把部分 rsirdi 替换为 esiedi
  2. phdr 部分可以向上移动 8 个字节,重复使用一部分空间 最终结果可以优化到 151 个字节,代码如下:
; hello_world.asm
  BITS 64
  org 0x400000

  ehdr:           ; Elf64_Ehdr
    db 0x7f, "ELF", 2, 1, 1, 0 ; e_ident
    times 8 db 0
    dw  2         ; e_type
    dw  0x3e      ; e_machine
    dd  1         ; e_version
    dq  _start    ; e_entry
    dq  phdr - $$ ; e_phoff
    dq  0         ; e_shoff
    dd  0         ; e_flags
    dw  ehdrsize  ; e_ehsize
    dw  phdrsize  ; e_phentsize
  phdr:           ; Elf64_Phdr
    dd  1         ; e_phnum      ; p_type
	              ; e_shentsize
    dd  5         ; e_shnum      ; p_flags
	              ; e_shstrndx
  ehdrsize  equ  $ - ehdr
    dq  0         ; p_offset
    dq  $$        ; p_vaddr
    dq  $$        ; p_paddr
    dq  filesize  ; p_filesz
    dq  filesize  ; p_memsz
    dq  0x1000    ; p_align
  phdrsize  equ  $ - phdr
  
  _start:
    ; write "hello, world" to stdout
	push 1
	pop rax            ; system call for write
	mov rdi, rax            ; file descriptor for stdout
	mov esi, message    ; pointer to string to write
	push 13
	pop rdx           ; length of string to write
	syscall          ; invoke the system call
	; exit with status code 0
	push 231
	pop rax      ; system call number for _exit
	xor edi, edi     ; exit status code (0)
	syscall          ; invoke the system call

  message: db "hello, world", 10 ; 10 is the ASCII code for newline

  filesize  equ  $ - $$

WCY-dt avatar Apr 20 '23 13:04 WCY-dt

@WCY-dt 出了 @regomne 所说的部分,还可以:

  1. 把部分 rsirdi 替换为 esiedi
  2. phdr 部分可以向上移动 8 个字节,重复使用一部分空间 最终结果可以优化到 151 个字节,代码如下:
; hello_world.asm
  BITS 64
  org 0x400000

  ehdr:           ; Elf64_Ehdr
    db 0x7f, "ELF", 2, 1, 1, 0 ; e_ident
    times 8 db 0
    dw  2         ; e_type
    dw  0x3e      ; e_machine
    dd  1         ; e_version
    dq  _start    ; e_entry
    dq  phdr - $$ ; e_phoff
    dq  0         ; e_shoff
    dd  0         ; e_flags
    dw  ehdrsize  ; e_ehsize
    dw  phdrsize  ; e_phentsize
  phdr:           ; Elf64_Phdr
    dd  1         ; e_phnum      ; p_type
	              ; e_shentsize
    dd  5         ; e_shnum      ; p_flags
	              ; e_shstrndx
  ehdrsize  equ  $ - ehdr
    dq  0         ; p_offset
    dq  $$        ; p_vaddr
    dq  $$        ; p_paddr
    dq  filesize  ; p_filesz
    dq  filesize  ; p_memsz
    dq  0x1000    ; p_align
  phdrsize  equ  $ - phdr
  
  _start:
    ; write "hello, world" to stdout
	push 1
	pop rax            ; system call for write
	mov rdi, rax            ; file descriptor for stdout
	mov esi, message    ; pointer to string to write
	push 13
	pop rdx           ; length of string to write
	syscall          ; invoke the system call
	; exit with status code 0
	push 231
	pop rax      ; system call number for _exit
	xor edi, edi     ; exit status code (0)
	syscall          ; invoke the system call

  message: db "hello, world", 10 ; 10 is the ASCII code for newline

  filesize  equ  $ - $$

esi那个优化,“程序入口的时候rsi为0”这件事是有什么标准规定了么?如果没有的话建议还是继续用rsi……

regomne avatar Apr 21 '23 06:04 regomne