I checked the official document of ETH and found that there is a gas cost on each opcode. I want to know why the Gas cost is generated. Can I say that gas cost is generated due to the EVM read/write of opcodes?
Gas is used not only to pay miners, now stakers, in exchange for executing smart contracts and transactions, but mostly it acts as a safety mechanism used to deter malicious actors from submitting harmful transactions since they will incur a cost for every resource they consume.
I like this description from Juan Xavier Valverde's gas optimization article:
Ethereum is a Turing-complete system. Turing-complete systems face the challenge of the halting problem i.e. given an arbitrary program and its input, it is not solvable to determine whether the program will eventually stop running. So Ethereum cannot predict if a smart contract will terminate, or how long it will run. Therefore, to constrain the resources used by a smart contract, Ethereum introduces a metering mechanism called gas. So, gas is the unit used in Ethereum for measuring and limiting computations per block.
And also these two descriptions from Ethereum.org:
Gas refers to the unit that measures the amount of computational effort required to execute specific operations on the Ethereum network. Since each Ethereum transaction requires computational resources to execute, those resources have to be paid for to ensure Ethereum is not vulnerable to spam and cannot get stuck in infinite computational loops. Payment for computation is made in the form of a gas fee. The gas fee is the amount of gas used to do some operation, multiplied by the cost per unit gas. The fee is paid regardless of whether a transaction succeeds or fails.
The STARTGAS and GASPRICE fields are crucial for Ethereum's anti-denial of service model. In order to prevent accidental or hostile infinite loops or other computational wastage in code, each transaction is required to set a limit to how many computational steps of code execution it can use. The fundamental unit of computation is "gas"; usually, a computational step costs 1 gas, but some operations cost higher amounts of gas because they are more computationally expensive, or increase the amount of data that must be stored as part of the state. There is also a fee of 5 gas for every byte in the transaction data. The intent of the fee system is to require an attacker to pay proportionately for every resource that they consume, including computation, bandwidth and storage; hence, any transaction that leads to the network consuming a greater amount of any of these resources must have a gas fee roughly proportional to the increment
They serve two primary purposes:
Resource Allocation: Gas costs prevent the network from being overwhelmed by excessive computation or storage demands. Assigning a cost to each opcode (the fundamental operation of the Ethereum Virtual Machine) ensures that resource-intensive operations don't monopolize the network's computing resources.
Economic Incentives: Miners are compensated with transaction fees paid in Ether for including transactions in a block. These fees cover the gas costs associated with executing the transactions. Miners prioritize transactions with higher gas fees, which incentivizes users to pay more for faster processing.