Program termination: Difference between revisions

m
→‎{{header|6502 Assembly}}: made the example hardware-agnostic
m (→‎{{header|6502 Assembly}}: made the example hardware-agnostic)
Line 16:
exit(1)</lang>
=={{header|6502 Assembly}}==
===Commodore 64, etc.===
This mostly depends on the hardware, but typically home computers would boot in BASIC and <code>JSR</code> to the starting address on the disk. This means that the return address for BASIC is on top of the stack, and an <code>RTS</code> instruction will exit the program and return to BASIC, provided that execution is not inside one of the disk's subroutines.
<lang 6502>;assuming this is not a subroutine and runs inline.
cmp TestValue ;a label for a memory address that contains some value we want to test the accumulator against
LDA $DC01 ;read player 1's joystick
beq continue
and #%00010000 ;fire button
rts ;unlike the Z80 there is no conditional return so we have to branch around the return instruction.
bne JoyNotFire
continue:</lang>
rts ;returns to basic if the return address of BASIC is on top of the stack.
 
JoyNotFire:
===Nintendo Entertainment System===
;check the other buttons</lang>
There is no firmware or BASIC to return to, so the easiest way to do this is to "trap" the program counter with a branch back to itself. This isn't a complete termination since NMI code will still run, unless you disable the screen. In order to display a static "THE END" screen that doesn't take user input, which is what many games did once the game was beaten, you can do this:
 
<lang 6502asm>
sei ;disable IRQs
halt:
jmp halt ;trap the program counter</lang>
 
Of course, this depends on your game using a software abstraction of its screen data that gets written to the hardware ports during vBlank; since execution in the main program has halted the ports aren't getting any new data and instead the last thing written to their user ram counterparts is just getting written to the hardware ports over and over again.
 
While the NES was used as an example, this same logic should apply to other ROM-cartridge-based game consoles as well.
 
=={{header|AArch64 Assembly}}==
1,489

edits