There were unwritten laws before the laws were given to Moses as those laws mostly pertained to the Israelites
In Romans 5: 12-14, Paul is discussing the time period between Adam and Moses. Sin entered the world through one man (Adam). The penalty of sin is death, and Paul tells us that death entered humanity through Adam. All humans have sinned, Rom. 3:23, and death, therefore, has power over everyone. In verse 13, Paul uses the word law in two different senses. One law was given through Moses, but before that law was given, a more fundamental law existed. Between the time of Adam and Moses, everyone sinned. They were ignoring God, going their own ways, doing things God did not want them to do. God's law existed, even though it had not been written down, and everyone was transgressing it. Therefore, death ruled over them all, even if they did not break a specific command in the way that Adam did. Adam and Eve should have obeyed, but they acted selfishly, and they sinned. They wanted wisdom, but they tried to take it for themselves instead of receiving it legitimately. The result was death for them and all their descendants. All human beings have a selfishness that predisposes them to sin. Everyone sins, and everyone needs the sacrifice of Jesus Christ, as Paul explains in Romans 5.
Cain had a built-in sense of right and wrong. He knew that his attitude toward his brother was wrong. God told Cain to resist the sinful nature, but instead he allowed it to rule over him, and he murdered his brother. This was a sin, even though no written law said it was. All normal humans have a conscience, a natural inbuilt sense of right and wrong. This is what Paul refers to in Romans 2:14-15. By nature, God has written a moral sense, a law, into human hearts. Their understanding of right and wrong is not perfect, but every sane person has at least a basic concept of right and wrong, of love and selfishness. Although everyone falls short, some people do have good behavior as compared to others. By nature, they do things that are required in God's law — not the rituals of Moses, but the more general requirements of the law that existed before Moses.
Although many people try to do what they think to be right, none is perfect. Many others choose to live selfishly, violating the standards of their societies. The biblical story tells us that people became more and more violent, and God destroyed them with a flood, Gen. 6:11-13. After the flood, he gave an additional warning about murder, Gen. 9:5-6. He also established a covenant or agreement with Noah, promising that he would not destroy the earth with a flood, verses 8-11. Abraham believed God, and he was therefore judged to be in a right relationship with God even though he was not perfect. If Abraham believed God's astounding promise, then he also had enough faith to do whatever God asked. Even when God's command seemed to threaten God's promise to him, Abraham was willing to obey God. But it was the faith, not the obedience, that was counted for righteousness. The attitude of heart was considered more important than the result.