Assembly number guessing game

The re-write of the x86 Assembly version of NumGuess was long overdue. 15 years ago when I wrote the original code I didn’t think about standardisation or guidelines, just wanted to make it work. As the NumGuess project took off and received numerous contributions, the old assembly code became the weakest link.

A couple of weeks ago I decided to jump back into 16 bit x86 assembly programming to bring it in line with the rest of the project. It was clear from the start that it needed a complete re-write as the whole code was useless. Nibbling away at it in my free time, it is finally complete. Today I give you not just the guideline compliant 286 DOS code, but an improved 386 DOS version that uses 32 bit registers as well, which pushes the total number of NumGuess implementations to 40.

Random number generator

The original had no such thing. As the limit was hard-coded to 100, it simply queried the current time, which was accurate to 100th of a second. With the guideline’s requirement of a dynamic limit, that was no longer feasible, and an RNG was needed that gave believable randomness.

I chose MINSTD, which is a simple Lehmer RNG with a multiplier of 48271, which conveniently fits into a word register, the largest on the 16-bit platform I was aiming for. Unfortunately I still had to do some arithmetic calculations for up to 47 bits, which needed some extra work-arounds and code branching on 286.

The largest number it generates is 2147483646, one less than the maximum positive value of a 32 bit signed integer. This is the current limitation for all numbers in both versions.

The 386 version

The 386 version is just a simple improvement on the 286 one, taking advantage of the 32 bit registers that are available. To avoid using protected mode and keep it simple, it still uses 16-bit segments, but with the simplified calculations the code is about 140 lines shorter.

Conclusion

To compile the code, you’ll need the DOS versions of either TASM or MASM. The easiest way is the Tasm for Windows project on SourceForge, which bundles it with DOSBox. Compile and link with the following commands to generate an executable:

tasm num_286
tlink num_286

If you prefer MASM, you can download it from here, then:

masm num_286.asm
ml num_286.obj

Enjoy the latest addition to the NumGuess family, comments and suggestions are welcome. If you find a bug, please submit an issue on GitHub.

Share Button
Posted in Uncategorized

Android Studio vs Eclipse folder structure

With the recent stable release of Android Studio in December, it has replaced Eclipse as the standard Android development environment. All developers are advised to migrate to it, as the ADT Eclipse plugin will not be maintained and updated, and as of now, it is no longer supported.

In this article I’ll have a quick look at the differences in the folder structure of the two IDEs.

Eclipse workspaces and Android Studio projects

Android Studio changes the terminology a bit. In Eclipse, you had workspaces with a number of projects and library projects in them. In Android Studio, this becomes a project with a number of modules and library modules. Apart from the names, they’re pretty much the same thing, but thanks to the mandatory Gradle build system, it tends to be more organised.

Many libraries no longer need to be copied into your workspace. Simply add a reference to the module’s build.gradle file, state the required version, and it will be automatically fetched from the remote repository, compiled and included without having to worry about it. No more hunting for Android support library JARs in the SDK folders, and no more manual Eclipse project opening nightmare. Many third party libraries can be included in the same way with Gradle, which saves a lot of clutter. Your project folder will be neat and clean, and really only contain your own code.

Folder structure

Here’s a list of folders where your code and resources are stored compared to Eclipse:

What is it? Eclipse Android Studio
Android manifest [project] [module]/src/main
Assets [project]/assets [module]/src/main/assets
Java source files [project]/src [module]/src/main/java
Resources [project]/res [module]/src/main/res
Included JARs [project]/libs [module]/libs

In the next article I’ll have a look at migrating your Eclipse project to Android Studio.

Share Button
Posted in Uncategorized

NumGuess JScript and VBScript

Two more NumGuess versions have been added bumping the total up to 39. This time it’s JScript and VBScript versions with complete Windows Script Host support. They can be run with cscript.exe for console, or wscript.exe for annoying pop up windows.

The VBScript version is almost the same as the VB.NET one, with small modifications of error handling and console input.

The JScript version was a bit more tricky. On the up side, it includes and uses the JavaScript library, which is JScript compatible. On the down side, Microsoft somehow forgot to add input box support for wscript, so it has to be invoked from a VBScript part. This is why the file extension had to be changed to .wsf and the code itself is embedded into XML.

For the record, there were two more work-arounds proposed on the internet for the above issue, none of them worked:

  • The first one involved creating a ScriptControl ActiveX object to run VBScript code directly from JScript via eval. It looked like a fairly clean solution, but it doesn’t work in 64 bit mode, and on my machine it didn’t work in forced 32 bit mode either.
  • The second proposal was to create a hidden Internet Explorer instance, load an empty page and directly call window.prompt(). Unfortunately due to IE security improvements, it crashed with access denied, and I just ended up with an unkillable hidden IE process.

So yes, the JScript version is not ideal, but it works. If wscript support is not needed, the JScript part works fine in a .js file as well.

And as always, we’re always open to new contributions.

Share Button
Posted in Uncategorized

End of 2014 report

Another year has gone by and CodeBin blog is celebrating its second birthday today. Yay! I’d also like to wish all my readers a happy and prosperous new year! This is the usual recap of all that’s happened here.

Apart from a few moderately popular posts about LibGDX and Android stuff, the most popular post this year was about upgrading the Eclipse development IDE to the latest version. With this many hits, I think it’s time for developers to think about making it more straight-forward and automatic in the future.

CodeBin also went social in February last year with Facebook and Twitter accounts, and although the follower and like counters are still under the double-digit mark, it was more as an experiment and development excercise than anything else. Anyways, if you feel like social networks is the best way to keep track of my posts, it’s there if you need them. ;)

The NumGuess project has seen some nice pull request with a couple of extra contributors, and it’s already a bigger and nicer project than I would have expected 15 years ago when I wrote the first line of code. Thank you all for your hard work!

The most ambitious project in 2014 was undoubtedly Mangler.js. While it is still not feature complete and lacks up to date documentation, it has helped me shorten some javascript code at work, and anyone is free to try and use it.

That’s about it, let’s see some random analytics stats.

Visitors

The site’s popularity has seen significant increase, I had about 10 times more visits compared to the year before. Many thanks to all my 31,051 visitors with 47,540 page views.

Most popular posts and pages

Other (7.2%)

Projects

OpenPrince (33.2%)
Mangler.js (22.3%)
NumGuess (18.3%)
FakeTileAO (17.9%)
EsoInt (4.8%)

Ostudio

C64 Games (38.9%)
Ostudio (22.4%)
Kill Bill (12.7%)
Ocamel (8.2%)
Oscorch (6.7%)
Otron (3.7%)

Visitor sources

Google search (89.4%)
Direct traffic / unknown (7.3%)
Semalt (0.6%)
Bing (0.3%)
StackOverflow (0.2%)
Yahoo (0.2%)
Other (2.0%)

Browsers

Chrome (58.9%)
Firefox (28.3%)
Safari (4.8%)
Internet Explorer (4.3%)
Opera (2.1%)
Other (1.6%)

Platforms

Desktop (95.5%)
Mobile (3.4%)
Tablet (1.1%)
Share Button
Posted in Uncategorized

NumGuess R Project contribution

R language logoNumGuess program #37 has arrived as an unexpected contribution by Gergely Daróczi, this time in the form of an R script.

It’s very nice and clean code, paying great attention to the guideline from the start. The R language is mainly used for statistical computing and graphics, and now for guessing numbers as well. For more information about the language, check out the R-project website.

As always, if you can code in something that’s missing, the project is always open for contributions.

Share Button
Posted in Uncategorized

Happy Holidays!

I’d like to take this opportunity to wish all my readers merry Christmas and happy holidays! Due to the popularity of last year’s riddle, I’d refrain from doing another one. If you’re desperate, just look up the old post and leave a comment, it’s not too late to be the first to submit the correct answer.;)

As this blog is getting close to its second birthday, stay tuned as I plunge into the analytics and give you this year’s highlights and most popular posts soon.

Share Button
Posted in Uncategorized

Ocamel vs Jazz Jackrabbit menu

While browsing the internet for old DOS games, I just came to realise that Jazz Jackrabbit uses the same font in their menus as Ostudio’s award winning Ocamel, i.e. their developers also found 3D Studio’s funny looking built-in font.

Ocamel's main menu vs Jazz Jackrabbit's

Ocamel’s main menu Vs Jazz Jackrabbit’s

Share Button
Posted in Uncategorized

Eclipse out of memory error

When using Eclipse for Android development, it can consume a lot of memory. If you’re working with multiple library projects with varying levels of target APIs and/or opening layout editors for different Android versions, sooner or later the java VM running the Eclipse IDE will run out of memory. This results in crashes, or even worse: cryptic, seemingly unrelated error messages and compilation errors.

To increase the memory available for the IDE, open eclipse.ini in your Eclipse folder and look for these lines:

...
-Xms40m
-Xmx512m
...

Change them to the following:

...
-Xms512m
-Xmx1024m
...

This should be sufficient for most development work, at least I haven’t encountered any issues since I’ve made these changes.

Share Button
Posted in Uncategorized

NumGuess Haxe and Jasmin

The NumGuess project has received another couple of great contributions by vbence:

  • New fully compliant Haxe version was added as the 36th implementation.
  • Jasmin version was fixed to follow the guidelines perfectly.

Keep up the good work guys, let me know if you have any other language ideas.

Share Button
Posted in Uncategorized

Custom indeterminate ProgressBar on Android

The default loading circle animation on Android is a bit choppy and grey. It’s very easy to roll your own with perfectly smooth animation and a bit of colour to match your app’s branding. In this simple example I’m not using any bitmaps, just a basic shape in an XML drawable.

First you need to create a drawable resource, simply put the following XML into /res/drawable/progressbar_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

    <shape
        android:innerRadius="20dp"
        android:shape="ring"
        android:thickness="4dp"
        android:useLevel="false" >
        <size
            android:height="48dp"
            android:width="48dp" />

        <gradient
            android:centerColor="#80ec7e2a"
            android:centerY="0.5"
            android:endColor="#ffec7e2a"
            android:startColor="#00ec7e2a"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

It is a simple ring shape with sweep gradient, in this case using a nice orange colour #ec7e2a. The gradient only changes the colour’s alpha value by adding an extra byte to the front: #ffec7e2a is opaque, #00ec7e2a is fully transparent and #80ec7e2a is halfway in-between.

Here's how it looks like against different backgrounds,

Here’s how it looks like against different backgrounds,

To use it in your layout, simply set the indeterminateDrawable attribute of your ProgressBar:

<ProgressBar
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:indeterminateDrawable="@drawable/progressbar_custom" />

Animation speed

The rotation speed of the indeterminate progress bar is fixed at one revolution every 4 seconds, changing the duration attribute in the drawable XML has no effect. If you prefer to speed it up, you can change the toDegrees attribute to multiples of 360:

  • 720 makes one turn in 2 seconds
  • 1080 makes one turn in 1.33 seconds
  • 1440 makes one turn in 1 second

Any higher would make your eyes water. :)

Share Button
Posted in Uncategorized

If you find something useful, please feel free to buy me a cup of coffee.