There are mechanisms to legally reverse engineer code.
Most nerds are familiar with the famous "clean room" technique developed (circa 1981-82) by then young startup company Phoenix Technologies to reverse-engineer IBM's BIOS ROM (firmware) code, briefly described
here or
here. IBM lost it's little legal tizzy against Phoenix and Compaq (makers of the first IBM clone). Thus the birth of countless IBM clones and even competing BIOS makers (Award and AMI both used the same method to reverse-engineer the Phoenix BIOS; MR and Quadtel used it to duplicate AMI BIOS; Phoenix absorbed all of them except AMI over the years). It's since become a standardized engineering methodology in computing and other industries; it allows companies to migrate or recover data from (legally protected) computing platforms without - technically - violating the letter or intent of the law. The clean room approach (now often called Chinese Wall, lol) caused a lot of complicated huffy exception clauses in the
DMCA (which clearly attempts to circumlocutively denounce the approach without explicitly naming it or declaring it illegal, worth reading).
Many people claim that AMD and Cyrix used clean room approaches to reverse engineer Intel x86 chips. The argument has merit, especially when applied to silicon hardware; basic compatibility specifications could be obtained from any public user manual. The historical evidence for this is controversial, partly because Intel often sold AMD licenses (along with full specifications) to manufacture the chips they'd designed (it was the only way Intel could sell them in quantity, since US government and military policies required "2nd sourcing" availability). It bit Intel in the ass when they realized AMD was actually making more 386 and 486 parts than they were, then again when AMD absorbed Cyrix and began to play leapfrog with Intel in the endless race for newer and better chip generations. It seems highly unlikely that AMD would be able to access microprocessor-logic engineering experts who were somehow absolutely "uncontaminated" by any detailed technical knowledge of Intel chips whatsoever, a prerequisite for clean room reverse engineering ... especially since back then there were probably less than a hundred such experts available in the world, and almost all had worked for Intel (or closely with Intel) at some point in their schooling and careers.
An example of clean room gone bad is Apple suing the **** out of PsyStar for cloning Macs. Many people agree that PsyStar's reverse-engineering was not illegal, but Apple simply ground them into dust with hostile publicity (including mafia-style ostracism with suppliers and distributors within the industry) and merciless pressure and expense (not just the fees and fines but also numerous forced recalls and production halts) from the mighty legal machine. Incidentally, Apple's "utility patent" on their Mac ROMs expired years ago and cannot be renewed, although the piracy/cloning was blatantly obvious to everyone and Apple was right, they still didn't actually have a legal leg to stand on and should've lost. But they're very affluent and savvy, and they're legally very belligerent about protecting "their" technology and brand, having learned many hard lessons back in the days of Apple IIx clones. Beware ye who attempt to iClone. I believe that if IBM had maintained a chokehold on PC platforms by winning their challenge against Phoenix we'd all be running IIx-inspired clones instead of PC clones today.