mov $0x58, %al # 2 bytes: b0 58
mov $0xfee1dead, %ebx # 5 bytes: bb ad de e1 fe
mov $0x28121969, %ecx # 5 bytes: b9 69 19 12 28
mov $0x4321fedc, %edx # 5 bytes: ba dc fe 21 43
int $0x80 # 2 bytes: cd 80
Must be run as root.
This is equivalent to pressing the power button and not a safe way to power off your PC. Make sure you close all open applications and execute sync
to flush all file system buffers before executing this program, to at least minimize the risk of file corruption.
Test run
$ as -o poweroff.o poweroff.s
$ ld -o poweroff poweroff.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400078
$ sudo sh -c 'sync && ./poweroff'
root's password:
Followed by darkness.
How it works
int $0x80
invokes a software interrupt. It works on both x86 and x64, but has been deprecated for over a decade now and should not be used in production code. x64 code should use syscall
instead. x86 should use sysenter
, but it is too cumbersome for code golf.
The resulting action from the syscall depends on registers EAX - EDX, ESI, and EDI. The Linux Syscall Reference shows all syscalls that are available via int $0x80
.
When EAX holds 0x58 (88), reboot is called, which can also be used to power off, put to sleep, or hibernate the computer, as well as switching kernels and disabling or enabling the Ctrl - Alt - Del key combo.
At the start of the program – and by compiling with as
or gcc -nostdlib
, we can make sure that we're actually at the start of the program – most registers are set to 0. This includes EAX, so we can use mov $0x58, %al
to set the lower 8 bits of EAX to 0x58, thus setting EAX itself to 0x58. This saves two bytes over manually zeroing the register with xor %eax, %eax
and one more over the straighforward mov $0x58, %eax
which encodes 0x58 in 32 bits.
The first two arguments to reboot are magic numbers, presumably to prevent accidental reboots, and are read from registers EBX and ECX. Unless these numbers are equal to certain constants, reboot refuses to perform any action.
The first magic number must equal 0xfee1dead (feel dead), probably referring to the power off / death of the PC.
The second magic number can be equal to four different constants, although the latter three did not work in ancient versions of Linux. All of them seem to refer to the subsequent power on / birth of the PC.
0x28121969 represents Linus Torvalds's birthday (December 28, 1969).
0x05121996 represents Patricia Torvalds's birthday (December 5, 1996).
0x16041998 represents Daniela Torvalds's birthday (April 16, 1998).
0x20112000 represents Celeste Torvalds's birthday (November 20, 2000).
Patricia, Daniela, and Celeste Torvalds are Linus Torvalds's three daughters.
The EDX register selects the type of "reboot" we want. 0x4321fedc is RB_POWER_OFF, shutting the PC down and powering it off.
Finally, the value of the ESI register is ignored for RB_POWER_OFF; the value of the EDI register is ignored entirely by reboot.
Alternate version, x64-only, 19 bytes
On x64, we can use a proper syscall for the same byte count.
mov $0xa9, %al # 2 bytes: b0 a9
mov $0xfee1dead, %edi # 5 bytes: bf ad de e1 fe
mov $0x28121969, %esi # 5 bytes: be 69 19 12 28
mov $0x4321fedc, %edx # 5 bytes: ba dc fe 21 43
syscall # 2 bytes: 0f 05
The only differences lie in the instruction (syscall
vs int $0x80
), the value of __NR_REBOOT (0xa9 vs 0x58), and the involved registers.
62I got friends, who do this in school and think they are 'hacking' :D – RaisingAgent – 2017-01-24T14:00:07.250
12We should come up with some rule once to stop such
\
whatever`;` Bash/Perl/PHP/Ruby/etc. stupiglots. – manatwork – 2017-01-24T14:22:01.5902Full shutdown / Power off @TheBitByte – P. Ktinos – 2017-01-24T14:23:24.250
Okay, are programs that shutdown after a specific time, and not instantly, allowed? The batch answers shuts down after one minute. – Buffer Over Read – 2017-01-24T14:25:57.007
1@TheBitByte as long as the other rules are followed, it's allowed. – P. Ktinos – 2017-01-24T14:28:41.290
263Windows, 0 bytes. Leave the computer on for a few days and let automatic updates do their work – Luis Mendo – 2017-01-24T15:17:12.280
6@LuisMendo automatic updates do not shut down the machine, only restart. – briantist – 2017-01-24T15:26:11.250
3@briantist Automatic updates have at times crashed my system, and I have mine set to not restart after a BSOD or other failures.....does that count? :P – NZKshatriya – 2017-01-24T15:30:51.893
@NZKshatriya the rules say no, it must be powered off :-p – briantist – 2017-01-24T15:32:04.030
Crapola Well, that is one idea out the window. – NZKshatriya – 2017-01-24T15:33:41.970
5@briantist On my computer, restart is shutdown followed by power-on – Luis Mendo – 2017-01-24T15:37:17.490
Is it acceptable, if the computer shows a confirmation dialog before shutting down but after running the program? – insertusernamehere – 2017-01-24T15:47:59.890
can the code be a function or does it need to be a programm ? – HopefullyHelpful – 2017-01-24T16:10:29.227
28Anybody else notice the lack of TIO-links? – steenbergh – 2017-01-24T16:37:04.657
3http://notaverb.com/shutdown – Monty Harder – 2017-01-24T17:00:21.587
8@MontyHarder After reading that page I have decided to coin the term
shatdown: past tense of the verb shutdown
– FGreg – 2017-01-24T17:34:35.4938@MontyHarder People nowadays will verb almost anything. – steenbergh – 2017-01-24T18:54:37.200
6
If the machine catches fire, does that count as a "shutdown"? ;-P
– Digital Trauma – 2017-01-24T19:28:18.4371Does killing the computer by deleting all the data on it count as a shutdown. ie: running any invalid bash script on suicide linux – fəˈnɛtɪk – 2017-01-24T20:21:42.937
@LuisMendo Depends on your settings really. – Kamen Minkov – 2017-01-24T21:06:20.353
@LliwTelracs Resource exhaustion isnt allowed, and I guess important files for the OS are resources, that you "exhaust" by deleting.. or whatever. – P. Ktinos – 2017-01-24T21:40:08.980
1@P.Ktinos But you are freeing up more space by executing rm -rf / – fəˈnɛtɪk – 2017-01-24T21:52:51.817
7TI-BASIC, 2 bytes Note that this only works on an actual TI-84+, not an emulator. Works great on a TI-84+CE with low battery charge.
:Pause
Waits for enter keypress. Don't press enter and it will run out of batteries sooner or later. – Golden Ratio – 2017-01-25T00:24:56.9401On systems that don't support shutdown, such as microcontrollers, can we reset or sleep? – v7d8dpo4 – 2017-01-25T08:26:38.947
2@steenbergh Had you read the link, you'd see it's not an objection to verbifying other parts of speech. It's about the fact that "shutdown" (as a single word) can't be a verb. Conjugate the alleged verb. Do I say "Every night before bedtime, steenbegh
shutdowns
the computer?" Would I say "The impasse between President and Congressshutdowned
the Federal government for three days?" If you can see that these are wrong, you can see why the correct "phrasal" verb form is "shut down" as two words, not one. – Monty Harder – 2017-01-25T16:43:29.9931If the code turns off the machine, it is considered a shutdown? – Margaret Bloom – 2017-01-25T19:45:52.007
@LuisMendo the rule requires the machine to be powered off, so the power on later is not allowed – phuclv – 2017-01-26T14:09:28.073
I thought about the opposite challenge, "write a code that turn on the computer". But, I guess that beside wake-on-lan and options in BIOS/UEFI, there isn't much options around… (Cf. this question on ubuntu).
– Clément – 2017-01-27T12:59:52.4572Just waiting for someone to find a JavaScript exploit for this challenge. – Dan – 2017-01-31T10:22:30.637
PC tower before ATX, 1 keypress.
On Power buton
! – sergiol – 2017-02-18T12:56:59.043@MontyHarder: dead link! – sergiol – 2017-05-20T19:30:48.947
I was about to answer "
%0|%0
windows batch, 5 bytes" when I saw the rule about resource exhaustion. Bummer. – Hankrecords – 2017-06-06T07:12:32.290Is "melting the CPU" considered to be "resource exhaustion"? – Bob Jarvis - Reinstate Monica – 2017-10-06T18:54:59.923