Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: January 30, 2025, 3:33 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PicoBlaze Simulator in JavaScript
#51
RE: PicoBlaze Simulator in JavaScript
bennyboy Wrote:This isn't something we have to philosophize on-- go ahead and make a simple switch statement and check the assembled code.
Do you happen to know how to do that with JavaScript? I know JavaScript is, in modern engines, first compiled to a superset of WebAssembly, but I have no idea how to see that WebAssembly, and I also don't know how to see the assembly code that WebAssembly is compiled to.
bennyboy Wrote: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.
The compiler probably optimizes that away, right? And even if it doesn't, the "and" operation takes a very small amount of time.
bennyboy Wrote:Does it say why?
Allegedly, using switch-case is a sign you do not have enough subclasses. If a switch-case is used again and again in methods of some class (in different methods), it's a sign that by which the switch-case branches are logically different classes.
HappySkeptic Wrote:A map that calls different pieces of code.
You mean, like virtual methods?
bennyboy Wrote: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.
In our "Razvoj Programske Podrške po Objektno Orijentiranim Načelima" classes, we were also taught clean and idiomatic code usually results in a faster compiled program, because the compiler is optimized to work with idiomatic code.
bennyboy Wrote: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?
It is on my website. The example code that executes unacceptably slow is my "Decimal to Binary" example program:
Code:
;WARNING: I have tried to run this on
;         real PicoBlaze, but failed.
;         Now, I did not have time to
;         test whether a simpler
;         example program using UART
;         would work, to see if it
;         is a problem with this
;         program, or if it is maybe
;         a problem with the way I
;         set up PicoBlaze. I have
;         opened a
;         StackOverflow question
;         about it, but, thus far, I
;         received no response.


;This is an example program that uses
;UART, the interface that PicoBlaze uses
;for connecting to terminals (a DOS-like
;user interface, with a keyboard and a
;screen capable of displaying text).
;It loads base-10 integer numbers from
;the terminal, converts them into binary,
;and then prints the binary
;representations back onto the terminal.
;Example input would be:

;1
;2
;4
;8
;15
;127
;255
;

;And the expected output is:

;1_(10)=1_(2)
;2_(10)=10_(2)
;4_(10)=100_(2)
;8_(10)=1000_(2)
;15_(10)=1111_(2)
;127_(10)=1111111_(2)
;255_(10)=11111111_(2)
;

;Note that you need to click the
;"Enable UART" button in order to use it.
;Also, the trailing empty line in the
;input is necessary for the result to be
;printed.

;Now follows some boilerplate code
;we use in our Computer Architecture
;classes...
CONSTANT LED_PORT,         00
CONSTANT HEX1_PORT,        01
CONSTANT HEX2_PORT,        02
CONSTANT UART_TX_PORT,     03
CONSTANT UART_RESET_PORT,  04
CONSTANT SW_PORT,          00
CONSTANT BTN_PORT,         01
CONSTANT UART_STATUS_PORT, 02
CONSTANT UART_RX_PORT,     03
; Tx data_present
CONSTANT U_TX_D, 00000001'b
; Tx FIFO half_full
CONSTANT U_TX_H, 00000010'b
; TxFIFO full
CONSTANT U_TX_F, 00000100'b
; Rxdata_present
CONSTANT U_RX_D, 00001000'b
; RxFIFO half_full
CONSTANT U_RX_H, 00010000'b
; RxFIFO full
CONSTANT U_RX_F, 00100000'b

ADDRESS 000
START:
;At the beginning, the number is 0.
load s0, 0
;And we are storing its string
;representation at the beginning
;of RAM.
namereg s3, pointer
load pointer, 0
;Now follows a loop to load
;the digits of the number.
loading_the_number:
 ;Load a character from the UART
 ;terminal.
 call UART_RX
 ;Check whether the character is a digit.
   compare s9, "0"
   ;If it is not a digit, jump to the
   ;part of the program for printing
   ;the number you have got.
   jump c,print_the_number
   compare s9, "9" + 1
   ;Suggestion by a StackExchange user.
   jump nc, print_the_number
 ;If it is a digit, store it into RAM.
 store s9, (pointer)
 add pointer, 1
 ;Multiply the number you have got by 10.
 load sf, s0
 call multiply_by_10
 load s0, se
 ;Then, convert the digit from ASCII
 ;into binary.
 sub s9, "0"
 ;And then add it to the number you
 ;have got.
 add s0, s9
 call c, abort ;In case of overflow.
 jump loading_the_number ;Repeat until a
                         ;non-digit is
                         ;loaded.
print_the_number:
;If there are no digits to be printed,
;do not print anything.
sub pointer, 0
jump z, START
print_the_decimal:
load s4, pointer
load pointer, 0
printing_the_decimal_loop:
 compare pointer, s4
 jump nc, end_of_printing_the_decimal
 fetch s9, (pointer)
 ;Do some basic sanity check: Is the
 ;character you are printing indeed
 ;a decimal digit?
   compare s9, "0"
   call    c , abort
   compare s9, "9" + 1
   call    nc, abort
 ;If it is indeed a decimal digit,
 ;print it.
 call UART_TX
 add pointer,1
 jump printing_the_decimal_loop
end_of_printing_the_decimal:
;After you have repeated the decimal
;number, print the string "_(10)=".
load s9, "_"
call UART_TX
load s9, "("
call UART_TX
load s9, "1"
call UART_TX
load s9, "0"
call UART_TX
load s9, ")"
call UART_TX
load s9, "="
call UART_TX
;If the number to be printed is
;equal to zero, print 0.
sub s0, 0
jump nz, print_the_binary
load s9, "0"
call UART_TX
jump end_of_printing_loop
print_the_binary:
;Make the pointer point to the
;beginning of RAM.
load pointer, 0
;Now goes a loop which stores the binary
;representation of the number we have
;got into RAM, but reversed.
beginning_of_converting_to_binary:
 sub   s0, 0
 jump  z , end_of_converting_to_binary
 load  s9, "0"
 sr0   s0
 jump nc, store_digit_to_memory
 add   s9, 1
 store_digit_to_memory:
 store s9, (pointer)
 add   pointer, 1
 jump beginning_of_converting_to_binary
end_of_converting_to_binary:
;Do some basic sanity check, such as that
;the pointer does not point to zero.
compare pointer, 0
call z, abort ;Something went wrong
             ;so end the program.
;Check whether there are more than 8 bits.
compare pointer,9
call nc, abort
;Now goes a loop which will print
;the binary number in RAM, with digits
;in the correct order. The pointer now
;points at a memory location right after
;the binary number (not at the last digit,
;but after it).
beginning_of_printing_loop:
 sub   pointer, 1
 jump  c      , end_of_printing_loop
 fetch s9     , (pointer)
 ;Do some basic sanity check:
 ;Is the character the pointer points to
 ;indeed a binary digit?
   compare s9, "0"
   jump    z , memory_is_fine
   compare s9, "1"
   jump    z , memory_is_fine
   call abort ;Something went wrong,
              ;so end the program.
 memory_is_fine:
 ;If everything is fine, print that
 ;digit.
 call UART_TX
 ;Repeat until you have printed all
 ;digits of the binary number
 ;stored in RAM.
 jump beginning_of_printing_loop
end_of_printing_loop:
;After you have printed that binary
;number, print the string "_(2)" and
;a new-line.
load s9, "_"
call UART_TX
load s9, "("
call UART_TX
load s9, "2"
call UART_TX
load s9, ")"
call UART_TX
load s9, a ;newline character, 0xa=='\n'.
call UART_TX
;The program runs in an infinite loop...
JUMP START

multiply_by_10:
 load se, sf
 add  se, se
 call c , abort
 add  se, se
 call c , abort
 add  se, sf
 call c , abort
 add  se, se
 call c , abort
return

abort:
 load s9, "E"
 call UART_TX
 load s9, "R"
 call UART_TX
 load s9, "R"
 call UART_TX
 load s9, "O"
 call UART_TX
 load s9, "R"
 call UART_TX
 load s9, "!"
 call UART_TX
 load s9, a ;newline
 call UART_TX
 infinite_loop:
 jump infinite_loop
return

;Now follows some boilerplate code
;we use in our Computer Architecture
;classes...
UART_RX:
 INPUT sA, UART_STATUS_PORT
 TEST  sA, U_RX_D
 JUMP  Z , UART_RX
 INPUT s9, UART_RX_PORT
RETURN

UART_TX:
 INPUT  sA, UART_STATUS_PORT
 TEST   sA, U_TX_F
 JUMP   NZ, UART_TX
 OUTPUT s9, UART_TX_PORT
RETURN



Messages In This Thread
PicoBlaze Simulator in JavaScript - by FlatAssembler - November 5, 2020 at 9:34 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - November 14, 2020 at 3:35 pm
RE: PicoBlaze Simulator in JavaScript - by Sal - November 15, 2020 at 7:37 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - November 15, 2020 at 7:45 am
RE: PicoBlaze Simulator in JavaScript - by Sal - November 15, 2020 at 8:10 am
RE: PicoBlaze Simulator in JavaScript - by LadyForCamus - August 16, 2022 at 7:24 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 17, 2022 at 2:03 pm
RE: PicoBlaze Simulator in JavaScript - by BrianSoddingBoru4 - November 14, 2020 at 3:37 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - November 15, 2020 at 4:56 am
RE: PicoBlaze Simulator in JavaScript - by BrianSoddingBoru4 - November 15, 2020 at 4:59 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - November 15, 2020 at 8:11 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - December 20, 2020 at 3:12 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - September 7, 2021 at 5:52 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - February 8, 2022 at 1:20 pm
RE: PicoBlaze Simulator in JavaScript - by arewethereyet - February 8, 2022 at 1:23 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - February 8, 2022 at 1:33 pm
RE: PicoBlaze Simulator in JavaScript - by BrianSoddingBoru4 - February 8, 2022 at 2:38 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 7, 2022 at 7:24 am
RE: PicoBlaze Simulator in JavaScript - by arewethereyet - August 7, 2022 at 6:59 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 9, 2022 at 2:32 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 12, 2022 at 7:26 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 12, 2022 at 1:43 pm
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 12, 2022 at 9:24 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 12, 2022 at 1:41 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 15, 2022 at 6:55 am
RE: PicoBlaze Simulator in JavaScript - by arewethereyet - August 15, 2022 at 6:57 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 16, 2022 at 12:37 pm
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 12, 2022 at 4:37 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 15, 2022 at 6:48 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 12, 2022 at 6:34 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 15, 2022 at 6:44 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 15, 2022 at 9:35 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 16, 2022 at 12:38 pm
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 16, 2022 at 1:23 pm
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 16, 2022 at 5:39 pm
RE: PicoBlaze Simulator in JavaScript - by arewethereyet - August 15, 2022 at 6:47 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 17, 2022 at 2:02 pm
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 17, 2022 at 3:08 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 17, 2022 at 3:25 pm
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 17, 2022 at 3:25 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 17, 2022 at 3:30 pm
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 17, 2022 at 3:34 pm
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 18, 2022 at 11:06 am
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 17, 2022 at 3:57 pm
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 17, 2022 at 4:43 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 18, 2022 at 6:43 am
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 18, 2022 at 9:31 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 18, 2022 at 9:08 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 18, 2022 at 9:41 am
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 18, 2022 at 10:10 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 18, 2022 at 12:00 pm
RE: PicoBlaze Simulator in JavaScript - by HappySkeptic - August 18, 2022 at 12:12 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 19, 2022 at 9:41 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 19, 2022 at 9:44 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 21, 2022 at 5:31 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 21, 2022 at 10:10 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 21, 2022 at 11:05 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 21, 2022 at 7:12 pm
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 21, 2022 at 7:31 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 22, 2022 at 9:07 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 22, 2022 at 11:38 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 22, 2022 at 11:49 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 22, 2022 at 5:19 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 23, 2022 at 2:33 pm
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 23, 2022 at 6:04 pm
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 24, 2022 at 7:18 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 24, 2022 at 7:43 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 25, 2022 at 7:33 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - August 31, 2022 at 7:12 am
RE: PicoBlaze Simulator in JavaScript - by bennyboy - August 31, 2022 at 11:13 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - September 2, 2022 at 12:59 am
RE: PicoBlaze Simulator in JavaScript - by FlatAssembler - September 14, 2022 at 9:31 am

Possibly Related Threads...
Thread Author Replies Views Last Post
  A weird bug in the preprocessor of PicoBlaze Simulator in JavaScript FlatAssembler 81 10091 December 19, 2023 at 4:46 pm
Last Post: BrianSoddingBoru4
  Reformatting tools for JavaScript FlatAssembler 0 465 June 14, 2020 at 10:13 am
Last Post: FlatAssembler
  Anatomy of religion in RELIGION SIMULATOR game gravitysoftware 12 3444 February 8, 2015 at 12:52 pm
Last Post: c172
  Analysis of a Facebook social engineering/Javascript "hack" Autumnlicious 4 3538 March 2, 2011 at 9:29 am
Last Post: fr0d0



Users browsing this thread: 1 Guest(s)