News for hackers. Stuff that matters.
Re:The smallest Hello World program by rep_lodsb on Thursday January 02, 2025 @04:17PM (#42579025)
Linux initializes all general purpose registers to zero. It's not documented AFAIK, but should be reliable - it has to init them to some value anyway to avoid leaking kernel state. So you can get away with:

    mov     al,1       ;write
    mov     edi,eax    ;handle=stdout
    mov     esi,msg    ;assumes load address below 4G
    mov     dl,msg.len
    syscall
    mov     al,60      ;assuming syscall succeeded, EAX was bytes written
    xor     edi,edi
    syscall
The load address stays constant unless there's some magic GNU extension header to enable ASLR. If we could get the code loaded below 64K, we could save another byte by using SI instead of ESI; however this doesn't work by default, you'd have to run 'echo 0 > /proc/sys/vm/mmap_min_addr' as root first.
Re:The smallest Hello World program by bd01 on Thursday January 02, 2025 @04:48PM (#42579315)
Initial register state is documented to be undefined except for rbp, rsp and rdx [1].

Can you say for certain that no other Linux version ever used GPRs to pass something else?

[1] System V ABI, page 29 (last line) and 30, https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf

Re:The smallest Hello World program by rep_lodsb on Thursday January 02, 2025 @05:01PM (#42579409)
For certain? No, but I wouldn't expect it. Not sure what that function pointer in rdx is intended for, but Linux doesn't use it.

(Note for pedants: rsp is technically a "general purpose register", but of course it is initialized to point to the userspace stack instead of zero.)

Re:The smallest Hello World program by retrac on Thursday January 02, 2025 @04:52PM (#42579331)
Assuming it is initial zero

   inc eax
is a byte shorter than mov al, 1
Re:The smallest Hello World program by rep_lodsb on Thursday January 02, 2025 @04:56PM (#42579363)
Yes, but only in 32 bit mode. Not that it matters, except for the hypothetical future processor or Linux kernel that is no longer compatible with that :)