RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:25 pm
(This post was last modified: August 17, 2022 at 3:30 pm by bennyboy.)
(August 17, 2022 at 2:02 pm)FlatAssembler Wrote:This is a series of if/else statements, not a jump table. But the issue is whether, as you believe, a switch statement will compile to this same code. This isn't something we have to philosophize on-- go ahead and make a simple switch statement and check the assembled code.bennyboy Wrote:Now, I'm not an expert in computer languages.As somebody who has made a compiler for his programming language, I would expect compilers to output the same code for switch-case and if-else. Think of it this way, the way to translate this:
to assembly code is this:Code:var codebyte = currentDirective & 0xff000;
switch (codebyte){
case 0x00000:
//Call a Register load from register function here.
break;
case 0x01000:
//Call a Register load from value function here.
break;
}
The compiler does not benefit from knowing the variable codebyte is the same at both Point #1 and Point #2.Code:mov eax, dword ptr [currentDirective]
and eax, 0xff000
mov dword ptr [codebyte], eax
cmp dword ptr [codebyte], 0x00000 # Point #1
jnz label0
#Call a Register load from register function here.
label0:
cmp dword ptr [codebyte], 0x01000 # Point #2
jnz label1
#Call a Register load from value function here.
label1:
Anyway, your js code is calculating [codebyte] again and again in the conditional check of every single "if" statement, which is not the same as what you have in the above example.