Halt and catch fire

From Rosetta Code
Revision as of 16:40, 15 September 2021 by Hkdtam (talk | contribs) (→‎{{header|Raku}}: alternative and some extra findings)
Task
Halt and catch fire
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Create a program that crashes as soon as possible, with as few lines of code as possible. Be smart and don't damage your computer, ok?

References
  1. Halt and Catch Fire



6502 Assembly

Upon executing this byte as code, the processor will halt. No interrupts can occur either. This does not occur on 65c02-based hardware such as the Apple II or Atari Lynx. <lang 6502asm> db $02</lang>

8086 Assembly

Translation of: Z80 Assembly

Disabling interrupts prior to a HLT command will cause the CPU to wait forever. <lang asm>cli hlt</lang>

68000 Assembly

The 68000 can only read words or longs at even addresses. Attempting to do so at an odd address will crash the CPU. <lang 68000devpac>CrashBandicoot equ $100001 LEA CrashBandicoot,A0 MOVE.W (A0),D0</lang>

ALGOL 68

This program will crash immediately on startup. <lang algol68>( print( ( 1 OVER 0 ) ) )</lang>

ALGOL W

This won't halt the CPU but the program will crash immediately on startup. <lang algolw>assert false.</lang>

C

<lang c>int main(){int a=0, b=0, c=a/b;}</lang>

C++

Use an unhandled exception to crash the program. <lang cpp>#include <stdexcept> int main() {

   throw std::runtime_error("boom");

}</lang>

Output:
terminate called after throwing an instance of 'std::runtime_error'
  what():  boom

The output depends on the compiler and platform but should be similar.

Go

This wouldn't survive go fmt which would stretch it out to 5 lines. However, that's not compulsory and the task says do it in as few lines as possible. <lang go>package main; import "fmt"; func main(){a, b := 0, 0; fmt.Println(a/b)}</lang>
An alternative shorter line would be: <lang go>package main; func main(){panic(0)}</lang>

Julia

<lang julia>.</lang>

Output:
ERROR: syntax: invalid identifier name "."

Nim

One possibility: <lang Nim>assert false</lang>

Another solution with the same number of characters (we could also use mod instead of div): <lang>echo 1 div 0</lang>

Pascal

Works with: Free Pascal

Do an illegal memory access at $0

<lang pascal>begin pByte($0)^ := 0 end.</lang>

Output:
Runtime error 216 at $0000000000401098

Perl

<lang perl>&a</lang>

Output:
Undefined subroutine &main::a called at line 1.

PL/M

This will terminate the program by restarting CP/M. <lang plm>100H: GOTO 0; EOF</lang>

Phix

I normally and quite often just use this:

?9/0

The ? means print and/but obviously the 9/0 triggers a fatal error before it gets that far.

Output:
C:\Program Files (x86)\Phix\test.exw:1
attempt to divide by 0

Global & Local Variables

--> see C:\Program Files (x86)\Phix\ex.err
Press Enter...

Alternatives include crash("some message") which produces similar output, and abort(n) which is somewhat quieter with abort(0) meaning (immediately) terminate normally without an error. All of those can be caught by try/catch: should you want to get properly brutal and defeat any active exception handler you can/must resort to inline assembly:

try
    #ilASM{
       [PE32]
           push 1 -- uExitCode
           call "kernel32","ExitProcess"
       [PE64]
           sub rsp,8*5
           mov rcx,1 -- uExitCode
           call "kernel32","ExitProcess"
       [ELF32]
           xor ebx, ebx 
           mov eax, 1  -- SYSCALL_EXIT 
           int 0x80 
       [ELF64]
           mov rax,231 -- sys_exit_group(rdi=int error_code) 
           xor rdi,rdi
           syscall
   }
catch e
    ?e
end try

No output, the try/catch is just for show. ExitProcess/sys_exit are the only non-catchable things I know of, apart from a few other deliberates such as quitting the debugger, and aside from being technically difficult to catch it seems reasonable to classify them as direct actions rather than errors.

Raku

<lang perl6>++8</lang> Syntactically: Valid.

Semantically: Change the mathematical concept of 8 to 9, either in your whole computer, or maybe the whole universe.

Fails with this run-time error:

Output:
Cannot resolve caller prefix:<++>(Int:D); the following candidates
match the type but require mutable arguments:
    (Mu:D $a is rw)
    (Int:D $a is rw --> Int:D)

The following do not match for other reasons:
    (Bool $a is rw)
    (Mu:U $a is rw)
    (Num:D $a is rw)
    (Num:U $a is rw)
    (int $a is rw --> int)
    (num $a is rw --> num)
  in block <unit> at -e line 1

Alternately, and perhaps more community condoned, to end the program as soon as possible without trying to change the Laws of the Universe, you could just enter: <lang perl6>die</lang>

In REPL:
Died
  in block <unit> at <unknown file> line 1

Same character count, exits the program as soon as possible (though trappable if desired through the exception system,) and it looks more like an intentional act rather than a typo. Plus, you can add a message that will be added when it dies to explain why.

Here is a silly alternative : A standalone Unicode counterpart for the sequence operator takes up 3 code units but visually just a single codepoint,

Output:
cat test.raku ; wc test.raku
…
1 1 4 test.raku

raku -c test.raku ; echo $?
Syntax OK
0

raku test.raku ; echo $?
Stub code executed
  in block <unit> at test.raku line 1

1

However when I tried to combine all to test against the Test module, the last one somehow lived through an EVAL,

<lang perl6>use Test;

dies-ok { ++8 }; dies-ok { die }; dies-ok { … };

eval-dies-ok '++8'; eval-dies-ok 'die'; eval-dies-ok '…' ;</lang>

Output:
ok 1 -
ok 2 -
ok 3 -
ok 4 -
ok 5 -
not ok 6 -
# Failed test at all.raku line 11

so it is indeed at one's discretion whether this one is qualified as a crasher.

REXX

<lang rexx>=</lang>

Wren

<lang ecmascript>Fiber.abort("")</lang>

Z80 Assembly

The processor is permanently halted. Strangely enough, this does not work on the Game Boy. Rather, both the HALT instruction and the instruction after it are skipped. <lang z80>di halt</lang>