Writing a register based VM in less than 125 lines of C code
<i>Our machine has W=UINT16_MAX words, of N=16 bits each</i><p>Did the seemingly-off-by-one-error catch anyone else? A 16-bit integer has 65536 values, so you'll be going one past the end of the virtual RAM if you attempt to access address FFFFh.<p><i>With this simple trick, we can avoid writing a switch statement with 16 (+1) cases</i><p>Personally, I prefer having a switch since it's more obvious which opcode goes where. Probably makes the code a little shorter too (and is that another off-by-one, this time in the other direction?)<p>Also, I understand the need to keep a "learning architecture" simple, but without byte-wide memory access, you'll quickly realise how annoying it is to work with actual bytes. For example, any string handling, unless you decide to use UTF-16 instead. I think a sweet spot is 16-bit address and 8-bit data, with register pairs for 16-bit operations, much like the popular Z80 and the 8080/8085 that preceded it.<p>The DEC Alpha is a real architecture that made a similar mistake and later had to be extended to fix it: <a href="https://en.wikipedia.org/wiki/DEC_Alpha#Byte-Word_Extensions_(BWX)" rel="nofollow">https://en.wikipedia.org/wiki/DEC_Alpha#Byte-Word_Extensions...</a>