Does every program have access to all hardware?
No, most programs have no access at all to hardware. They call
device drivers through the OS to perform input and output. These
device drivers are custom written to access the hardware for the
device they control.
Modern computers use privileged instructions that cause an error
if a user program tries to execute them to access hardware. The
device drivers have the privilege to execute these instructions and
access hardware.