Jump to content

javascript decimal and hex confusion


david77

Recommended Posts

I've got some of my CPU working. I have provided details of the current problem onhttp://homepage.ntlw...s/modelinfo.htm I am having difficulty persuading javascript to handle hex data consistently. MOV A,10 puts 10hex into the A regand MOV ( B),A writes that to memory addressed by B - correctly. MOV A,( B) should read that back, but it gets 16. MOV A,10 uses 'memread' to read from memory (ok) while MOV A,( B) uses the same 'memread' code and gets it wrong. Any clues on where to look please? ( B) should be ( B ) without the spaces.

Link to comment
Share on other sites

If you want a number to be interpretted as hexadecimal you have to prepend 0x to it. Remember that hexadecimal is just a representation of the number in your source code, the number will become just a number in the end which is usually printed out in decimal form. I don't see any Javascript syntax in your source code at all. Can you show some actual Javascript code?

Link to comment
Share on other sites

function hexstr2(n) {var hD="0123456789ABCDEF";var h = hD.substr(n&15,1);n>>=4;h=hD.substr(n&15,1)+h;return h;} function showdata(dbusval) { // this writes the data to CPU imagevar s=hexstr2(dbusval);n=parseInt(s,16);var s= "Data "+ s + " "+ mybin(n);settext("dataval",s);} function memread(addr) { // this gets called by microcode step 'a'dbusval=mem[addr];dbusval=parseInt(dbusval, 16);showdata(dbusval);return dbusval;} case "a": mcodetxt="Memread";dbusval=memread(abusval); // dbusval = value on databus, abusval = value on address bussetcolor("dbus","red");setcolor("arrowin","red");if ((abusval < memfirst) || (abusval > memlast)) {showmem(abusval);}break; I want it to display in hex. It seems to work as an instruction operand (MOV A,10) but not with MOV A,( B ) which uses the same call.

Edited by david77
Link to comment
Share on other sites

Do you have some kind of interpretter built in Javascript? "MOV A, ( B )" is not Javascript syntax. If you want to print a number in hexadecimal format you'll need to make a custom function to do it.

function decToHex(num) {    var digit, value;    value = "";    while(num > 0) {	    digit = num%16;	    num = Math.floor(num/16);	    switch(digit) {		    case 10: digit = "A"; break;		    case 11: digit = "B"; break;		    case 12: digit = "C"; break;		    case 13: digit = "D"; break;		    case 14: digit = "E"; break;		    case 15: digit = "F"; break;		    default:			    digit = String(digit);	    }	    value = digit + value;    }    return value;}

I can't ensure this is the most efficient algorithm, but it works.

Link to comment
Share on other sites

I have an assembler program at http://homepage.ntlworld.com/stonebanks/asm5.htm which translates assembler mnemonics to machine codes. The instruction set and microcodes as shown at http://homepage.ntlworld.com/stonebanks/showmicrocode2.htm Clicking H shows two progs on N1 and N2.N1 is a simple INC in a loop copying from A to B reg. This works correctly from 00 to FF.N2 writes 10 to mem location 19 (correctly) and then reads it back - and gets 16. It then writes 20 to mem 19, and reads back 32. It then writes 30 and reads back 48. My problem is that the microcode step 'a' shown at #3 is the same step used for fetching the immediate data 10 correctly when mem is addressed by PC reg but gets it wrong when mem is addressed by B reg. I think that I'll have to create another microcode step that works when mem is addressed by B reg - but it's strange that the same code function memread(addr) gives different results depending on where the address comes from.

Edited by david77
Link to comment
Share on other sites

What does "Warn Status" mean under my name on the left?
I'm not sure exactly how the system works, but when you get warnings from the mods about objectionable content (or the like) the bar fills up. Or at least I assume that's what it's for. I've never gotten any warnings and my bar has always been empty. :P
Link to comment
Share on other sites

Ingholme - I've created another bit of microcode which calls memread2. The original memread works when reading immediate data as part of an instruction and memread2 which includes your code at #4 when reading mem addressed by B reg. I don't understand why I need to have different memread routines, but both of the programs loaded with N1 and N2 work now. Thanks

Link to comment
Share on other sites

Javascript really is crap. Apologies for being rude, but it was easier to write these programs originally in Turbo Pascal than it is now to simply change the code for another language - and the reason is type. With Pascal, a programmer specifies that a variable is a string or an integer or whatever. Both the programmer and the compiler know what they are doing. I wanted to use Opera's Dragonfly to see what is going on with my CPU. RET appears to work correctly, but PC gets set to some strange value during the following fetch operation. At present, a program is written into memory from child.htm page but Dragonfly then wants the page reloaded which destroys the recently loaded program. So I wrote the computer code into the CPU memory as part of the CPU javasript code. The first instruction is MOV SP,99 - that puts 99hex into the Stack Pointer for a CALL return address. If running a program loaded by child.htm, it does what I want. Write the same thing into memory with code as part of my CPU and the memory displayed on screen looks correct, but it writes 153 into SP .... 153 = 9x16 + 9. So what am I seeing in memory on screen? Is it decimal, is it hex, or is it a string? I would know with Pascal, I haven't a clue with Javascript. I received an email recently http://www.i-program...s/167/4320.html most of which went over my head - in which Anders Hejlsberg, who brought us Turbo Pascal, Delphi and C#, not to mention the whole idea of .NET says "...a new cross platform language called JavaScript and the typesystem went out with the bath water. Now we can all wonder how we do medium to large scale development. Erik Meijer: "...Are you saying you cannot write large programs in JavaScript?" Anders Hejlsberg: "No, I think you can, but I don't think you can maintain them." The laughter at this point is telling of the opinion and attitude of the audience and the panel. Anders Hejlsberg: "... I tried a refactor in a big JavaScript program. Is this x the same as this x over there? I'm afraid I couldn't tell you." From here the discussion goes on to explain that this is the reason Google is creating Dart: Gilad Bracha: "...you can write them (large JavaScript programs) but after that you will be suitably punished." Erik Meijer: ".. but if you look at JavaScript it was defined independently of HTML" Gilad Bracha: "That's true but combining two bad things does not make them better. ".

Edited by david77
Link to comment
Share on other sites

I don't believe you understand Javascript. It can be easily built and easily maintained if you know what you're doing. It is a loosely typed language.Javascript has: functions, objects (standard data structure), variables, operators, loops and conditions. Javascript does not directly interact with the processor or with the system memory. It's an interpretted language meant to interact with websites. Javascript doesn't manage memory, the browser manages the memory, so you don't need to work with pointers or addresses. If what you want to do is build a computer simulator, you're going to need an infrastructure that simulates memory addresses and the rest but that's a whole different matter. You'd need to build the "computer" before making a program for it. I don't understand your project, but you would probably be better off building it in Java or C++, or even Assembly (which seems to be the syntax you were using).

Link to comment
Share on other sites

Guest So Called
Javascript really is crap. Apologies for being rude, but it was easier to write these programs originally in Turbo Pascal than it is now to simply change the code for another language - and the reason is type. With Pascal, a programmer specifies that a variable is a string or an integer or whatever. Both the programmer and the compiler know what they are doing.
You've made a big mistake in not using the right tool for the right job. Of course you can't write a really large program in JavaScript. JavaScript wasn't intended for that. JavaScript is intended to run on somebody's browser when they visit your site, to enhance the content. I'm a professional HW/SW engineer. I think I understand what you're trying to do. Write your user interface in JavaScript, write your simulator code in PHP*, and then tie it together with AJAX. At least you'll be executing your code on a real computer instead of executing it on a browser pretending to be a programming platform. Note also that JavaScript running on a browser is intended to be running in a "sand box" where it's a desirable quality to not give JS code too much power or it could be a security risk. IOW it is intentionally crippled. * Or write it in some other language that executes on your server.
Link to comment
Share on other sites

Ingolme is right when he says that I don't know what I am doing. I don't claim to be a programmer but I have done a bit of programming over the years. I had one of the first BBC micros many years ago so have used BASIC, Pascal, and am now struggling with Javascript. MY CGT program at http://homepage.ntlworld.com/stonebanks/0809tax28.htm has over 1000 lines of code and has been in use for several years. I'm quite proud of it although someone did say that it was written by a crap programmer when I asked for advice on another website. I did sell my Pascal model computer program to RAF Cosworth as a training aid and to a couple of other colleges, but I spent more on advertising than I received in sales so didn't make my fortune there. I now want to put the program onto the web. I do not have any access to server-side programs. If I want to get this working on-line, then it will have to be in a language that runs on a user's browser, and that means Javascript. If this was Pascal, I'd declare memory to be an array of 256 bytes. In JS it is declared as an array of whatever type JS wants to make it - yuk. Yesterday, I wanted to use the Opera Dranonfly debugger but it wouldn't let me load the CPU program from child.htm so I wrote it into the CPU prog mem[0] = 0x3D; mem[1]=0x99; etc It looked ok on screen but then did its own thing when I tried to execute one micro-code step at a time - that's when I wrote my intemperate post above. Today, I have written the CPU machine-code program into the CPU JS code as text mem[0] = "3D"; mem[1]="99"; etc. That looks ok on screen and works until the fetch after the RET. Dragonfly told me an hour ago (but won't tell me now) that it couldn't convert 'n' in mem[n] to an object. I suspect that n is text. It seems to have returned zero so PC got loaded with the content of mem[0] which is 3D instead 06 which should have been the address following the CALL. The sun is shining now so I'll go and do something outside. I'll give this some more thought later. Thanks to both of you for your advice

Link to comment
Share on other sites

[10/06/2012 21:06:13] JavaScript - file://localhost/C:/Documents%20and%20Settings/Computer/My%20Documents/SVGexamples/cpu18.svgEvent thread: clickUncaught exception: TypeError: Cannot convert 'n' to objectError thrown at line 432, column 0 in hexstr(n) in file://localhost/C:/Documents%20and%20Settings/Computer/My%20Documents/SVGexamples/cpu18.svg: s = n.toString(16).toUpperCase();function hexstr(n) // was hexfn6(n) // 6 i/p 0-255, return 2 char hex string{ var s = "";settext("screen3", n); // this reports that n is undefineds = n.toString(16).toUpperCase(); s=trim(s); if (s.length < 2) {s = "0" + s;} if (n==0) {s="00";} return s;}called from line 877, column 13 in showmem2(n, m) in file://localhost/C:/Documents%20and%20Settings/Computer/My%20Documents/SVGexamples/cpu18.svg: b=hexstr(mem[n]);function showmem2(n,m) {var a, b;a=hexstr(n); b=hexstr(mem[n]); settext(m,a+": "+B); // a is the address, b is the content}called from line 882, column 17 in showmem(n) in file://localhost/C:/Documents%20and%20Settings/Computer/My%20Documents/SVGexamples/cpu18.svg: showmem2(n+1,"a2"); function showmem(n) { // this shows address and content of 'memory' on screen - the first locn is 61hex, then 99 and onwardsmemfirst = n;showmem2(n,"a1");showmem2(n+1,"a2");showmem2(n+2,"a3");showmem2(n+3,"a4");showmem2(n+4,"a5");showmem2(n+5,"a6");showmem2(n+6,"a7");showmem2(n+7,"a8");showmem2(n+8,"a9");showmem2(n+9,"a10");showmem2(n+10,"a11");showmem2(n+11,"a12");showmem2(n+12,"a13");showmem2(n+13,"a14");showmem2(n+14,"a15");showmem2(n+15,"a16");showmem2(n+16,"b1");showmem2(n+17,"b2");showmem2(n+18,"b3");showmem2(n+19,"b4");showmem2(n+20,"b5");showmem2(n+21,"b6");showmem2(n+22,"b7");showmem2(n+23,"b8");showmem2(n+24,"b9");showmem2(n+25,"b10");showmem2(n+26,"b11");showmem2(n+27,"b12");showmem2(n+28,"b13");showmem2(n+29,"b14");showmem2(n+30,"b15");showmem2(n+31,"b16");memlast = n+31;}called from line 797, column 53 in ex2(ch) in file://localhost/C:/Documents%20and%20Settings/Computer/My%20Documents/SVGexamples/cpu18.svg: showmem(abusval); // ex(2) executes the microcode steps case "L": mcodetxt="PC to Addr"; abusval=PC; showaddr(abusval); setcolor("PC2addr","red"); setcolor("Abus","red"); break; // PC and the addr are showing 3Dcalled from line 1002, column 4 in fnmcode() in file://localhost/C:/Documents%20and%20Settings/Computer/My%20Documents/SVGexamples/cpu18.svg: ex2(ch);function fnmcode() { // execute one microcode step and is called when button 'M' is clickedhexIR=IR.toString(16);hexIR = parseInt(hexIR, 16);var s=mc[hexIR]; // s is the microcode string shown below IR on screenvar ch;if (!f2) {fetch(true); f2=true; s=mc[hexIR]; mcodestep=0; settext("showmc",s); settext("showmc2","fetch");} else { if (mcodestep==0) { resetcolors(); } settext("showmc",mc[hexIR]); if (mcodestep<s.length) { ch=s[mcodestep]; ex2(ch); settext("showmc2",mcodestep.toString()+" "+ch+": "+mcodetxt); if (ch=="z") { f2=false; } else { mcodestep++; } } } return ch;} fnmcode executes one microcode stepex2 is called to run thru the code for a step. "L" PC to Addr seems to be the cause of the troubleshowmem and showmem2 simply show the content of memory accessedhexstr returns the address and content of a memory locn (with 2 calls)I would be grateful for any clue where to look. Thanks

Link to comment
Share on other sites

Try printing out the value of "n" to see what it is. By the way, if you want to use hexadecimal numbers in Javascript, you don't need to parse a string "a2", you just need to prepend 0x to the number:

var hexNum = 0xA2alert(hexNum); // Outputs 162 (decimal)

Link to comment
Share on other sites

For what it's worth, I've got several large Javascript programs, on the order of 100,000 lines and 2MB each of Javascript alone, not including the PHP parts. Are they easier to maintain than they would be in a strongly-typed language? No. But we've been maintaining them for years, adding new features, etc. A lot of bugs get into the system from various programmers, but the large majority of the bugs in the application are in the PHP code instead of the Javascript code. The Javascript code is typically even easier to debug than the PHP code (most of the Javascript problems are related to scoping, in PHP the other programmers can find any number of creative ways to make something not work). As far as this particular project goes, Flash/Actionscript 3 would probably be a good choice. Don't waste time with Actionscript 1 or 2, but if you like strongly-typed object-oriented languages then Actionscript 3 would be a good choice. It's based on ECMAScript, like Javascript, but it has a lot of features that developers of languages like C++ or Java would appreciate. Since it's embedded in a Flash movie, it will directly support doing the animation and visualization that your program requires. That isn't to say that it can't be done in Javascript, because it can, but there may be better tools for this specific application.

Link to comment
Share on other sites

Guest So Called

What on earth would you use 100,000 lines of JavaScript for, and does all that code load into the visitor's browser at one time? I would have thought a browser could not contain that much downloaded code.

Link to comment
Share on other sites

Not all at once. The largest single Javascript file is a little over 45,000 lines with comments and everything. When the code is minified it comes out to about 800KB, without comments or extra whitespace. After server compression, less than 100KB of data gets sent across the wire for that file. That particular application is a learning management system, and that file is the interface for admins. It's grown more than 50% in the last 2 years. We're actually about to redesign and rewrite large parts of it this summer. When a regular student logs in their interface file is a little under 11,000 lines, about 200KB minified. For another application, for help desk/inventory tracking, I used a different design method where it loads many small module files when they're needed instead of one monolithic file with everything.

Link to comment
Share on other sites

Of course you can't write a really large program in JavaScript. JavaScript wasn't intended for that. JavaScript is intended to run on somebody's browser when they visit your site, to enhance the content.
As JSG has pointed out, that is simply not true. It is up to the developer to use the tools at his disposal to build and solid architecture and foundation for their application. I write interactive television applications all day and they are written in JS. Our applications span very large networks and while we don't necessarily have to worry about supporting browsers, code maintainability is an every present influence on our work. We rely on following accepted and useful design patterns as well as unit testing, combined with build tools (Maven/Ant) and VCS (SVN/Git). I have built libraries and API's in JS for our clients to use with their other applications. I like to find some of the best influences of classical programming and mesh them with a lot of the power that Javascript offers. Everyday I am learning something new and my co-workers and I are always finding better ways to write our code. I assure that Javascript is definitely capable of supporting a large application and can still be maintainable and also easy for onboarding new developers. As with any software project, it really is how you start it. I'm sure it's just as easy to have a Java application blow up in your face just as easily if you don't plan well.
Link to comment
Share on other sites

To address this point:

I would have thought a browser could not contain that much downloaded code.
In terms of speed, something like IE6 is going to take a noticeably long time to do various things in the application, but any modern browser is relatively quick. With Chrome, for example, I can clear the cache and refresh the page and the DOMContentLoaded event fires in less than 1 second. It takes about another second for the interface to finish rendering. In terms of memory usage, in Chrome it looks like those tabs will start at between 30MB - 50MB, with more space being allocated depending on what you're doing (running reports, managing content, etc). That requires a total of about 550KB of data downloaded, including about 340KB of Javascript (across the wire, i.e. after compression).
Link to comment
Share on other sites

I'm not a programmer and I'm not going to learn a new language at my age.My traffic lights work without subroutines- see http://homepage.ntlworld.com/stonebanks/cpuandtraffic2.htm Click on N2, paste to CPU, then click on S (slow) I'm going to try Dart - it's Javascript with optional types. I suspect it will be a lot of work to knock the assembler and CPU progs into shape, but I'll have a go.Thanks for all of your comments.

Edited by david77
Link to comment
Share on other sites

Guest So Called

Well I'm impressed. One thing I know for sure, I'm never going to write any browser script that big.

Link to comment
Share on other sites

If what you're trying to do is an assembly emulator, I have proof this isn't THAT hard in JavaScript.Here is a tiny JS Assembly emulator I whipped up in the last 2 hours. It has a command set that's beyond useless, but the framework for supporting other assembly instructions is already there. AND it works perfectly with hex and decimal numbers alike.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...