Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 2:02 pm
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:
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;
}
to assembly code is this: 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:
The compiler does not benefit from knowing the variable codebyte is the same at both Point #1 and Point #2.
Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 2:03 pm
(August 16, 2022 at 7:24 pm)LadyForCamus Wrote: (November 14, 2020 at 3:35 pm)FlatAssembler Wrote: Can anybody here diagnose why the layout breaks in Firefox for Android when in the Landscape mode?
Why are you always asking us to do your homework for you?
That's not really my homework, that is my bachelor thesis.
Posts: 1664
Threads: 5
Joined: September 26, 2018
Reputation:
12
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:08 pm
(August 17, 2022 at 2:02 pm)FlatAssembler Wrote: 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:
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;
}
to assembly code is this:
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:
The compiler does not benefit from knowing the variable codebyte is the same at both Point #1 and Point #2.
In .NET, if the switch statement has more than 5 elements, it changes to using a jump table, instead of nested if's (this is from Microsoft's documentation).
In C++ the compiler is free to do whatever the heck it wants.
Do you ever wonder why switch statements in most languages are only for enumberable types (enum or integer)? It is so one can create a jump table or map in situations where that gives better performance.
Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:25 pm
(August 17, 2022 at 3:08 pm)HappySkeptic Wrote: (August 17, 2022 at 2:02 pm)FlatAssembler Wrote: 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:
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;
}
to assembly code is this:
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:
The compiler does not benefit from knowing the variable codebyte is the same at both Point #1 and Point #2.
In .NET, if the switch statement has more than 5 elements, it changes to using a jump table, instead of nested if's (this is from Microsoft's documentation).
In C++ the compiler is free to do whatever the heck it wants.
Do you ever wonder why switch statements in most languages are only for enumberable types (enum or integer)? It is so one can create a jump table or map in situations where that gives better performance.
What's a "jump table"? Can JavaScript do that, since it allows strings to be used in switch statements?
Posts: 9147
Threads: 83
Joined: May 22, 2013
Reputation:
46
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: 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:
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;
}
to assembly code is this:
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:
The compiler does not benefit from knowing the variable codebyte is the same at both Point #1 and Point #2. 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.
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.
Posts: 2020
Threads: 133
Joined: July 26, 2017
Reputation:
5
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:30 pm
I am a bit amazed people here are recommending me to use switch-case. In our "Razvoj Programske Podrške po Objektno Orijentiranim Načelima" classes, we were taught that using switch-case is a sign of bad style. Domagoj Kusalić in his book "Napredno Programiranje i Algoritmi u C-u i C++-u" also says switch-case should be used only rarely, if ever.
Posts: 9147
Threads: 83
Joined: May 22, 2013
Reputation:
46
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:34 pm
(This post was last modified: August 17, 2022 at 4:31 pm by bennyboy.)
(August 17, 2022 at 3:30 pm)FlatAssembler Wrote: I am a bit amazed people here are recommending me to use switch-case. In our "Razvoj Programske Podrške po Objektno Orijentiranim Načelima" classes, we were taught that using switch-case is a sign of bad style. Domagoj Kusalić in his book "Napredno Programiranje i Algoritmi u C-u i C++-u" also says switch-case should be used only rarely, if ever. Does it say why?
Let me ask you a question. In your .js file (Hi, mom, I got mentioned in someone's github!), you have a series of conditions formed like this:
Code: if ((currentDirective & 0xff000) === 0x00000) { // Do your LOAD register,register stuff }
else if ((currentDirective & 0xff000) === 0x01000) { // Do your LOAD register,constant stuff }
So you will have to do that bit operation every time. Why do you not do this:
Code: var filteredDirective = currentDirective & 0xff000;
if (filteredDirective === 0x00000) { // Do your LOAD register,register stuff }
else if (filteredDirective === 0x01000) { // Do your LOAD register,constant stuff }
Now, personally, I don't think that this difference will matter much unless you have hamsters running the code, but it seems like a lot of unnecessary operations to me.
Also, for style, I'd probably replace the hex constanst with meaningful labels, so you can check them all in code easily without scanning through many nested conditionals, though I don't know enough about JavaScript to know if it is comparable with "===":
Code: const LRR = 0x00000; // Load register, register
. . .
if (filteredDirective === LRR) { // Do your LRR stuff }
Posts: 67283
Threads: 140
Joined: June 28, 2011
Reputation:
162
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:42 pm
Do you want style points or utility?
I am the Infantry. I am my country’s strength in war, her deterrent in peace. I am the heart of the fight… wherever, whenever. I carry America’s faith and honor against her enemies. I am the Queen of Battle. I am what my country expects me to be, the best trained Soldier in the world. In the race for victory, I am swift, determined, and courageous, armed with a fierce will to win. Never will I fail my country’s trust. Always I fight on…through the foe, to the objective, to triumph overall. If necessary, I will fight to my death. By my steadfast courage, I have won more than 200 years of freedom. I yield not to weakness, to hunger, to cowardice, to fatigue, to superior odds, For I am mentally tough, physically strong, and morally straight. I forsake not, my country, my mission, my comrades, my sacred duty. I am relentless. I am always there, now and forever. I AM THE INFANTRY! FOLLOW ME!
Posts: 1664
Threads: 5
Joined: September 26, 2018
Reputation:
12
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 3:57 pm
(This post was last modified: August 17, 2022 at 3:58 pm by HappySkeptic.)
(August 17, 2022 at 3:30 pm)FlatAssembler Wrote: I am a bit amazed people here are recommending me to use switch-case. In our "Razvoj Programske Podrške po Objektno Orijentiranim Načelima" classes, we were taught that using switch-case is a sign of bad style. Domagoj Kusalić in his book "Napredno Programiranje i Algoritmi u C-u i C++-u" also says switch-case should be used only rarely, if ever.
I don't know those books, but if I search for "are switch statements bad", I get a bunch of reasons which would be "even more bad" in a bunch of nested if's.
Do you know what one "preferred" solution to a large switch statement is? A map that calls different pieces of code. The map can then be dynamic, and modified at runtime, while a switch statement needs direct modification when new functionality is added.
But, when switching on a fixed set of enumerated types, a switch is preferred. In some languages, switching on an enum will actually give a warning or error if all the values of the enum aren't included as cases (without a default case). In this way, adding values to the enumeration would not be a maintenance nightmare. You get none of that with nested If's.
Posts: 9147
Threads: 83
Joined: May 22, 2013
Reputation:
46
RE: PicoBlaze Simulator in JavaScript
August 17, 2022 at 4:43 pm
(This post was last modified: August 17, 2022 at 4:51 pm by bennyboy.)
(August 17, 2022 at 3:42 pm)The Grand Nudger Wrote: Do you want style points or utility?
In programming, there's normally utility in style-- something that new programmers ignore when they try to write ultra-compact code. More readable code can reduce mistakes, and allow other programmers to understand it quickly: a potential savings of hours of real-life human time instead of a few milliseconds of CPU time. For most tasks, even a primitive CPU will be overkill by orders of magnitutde.
FA's case is different-- he's having performance issues, and he'll have to axe style wherever necessary to maximize efficiency-- even if that means dog-shit-looking code that nobody will enjoy reading.
@ FlatAssembler
I'm assuming you're using JavaScript because this is meant to run online. Did I miss (or forget) a link to a webpage that is actually running this? Do you have sample code that you feel is executing too slowly?
|