Sunday, June 17, 2018

AMD64/X86_64 General Purpose Registers

The 64 Bit registers consist of 16 general purpose registers, made up of the original 8 registers included in the original 16 and 32 bit CPU and still available in the 16 bit and 32 bit modes.

The original AX, BX, CX and DX registers are further subdivided into high-low pairs AH/AL, BH/BL, CH/CL, DH/DL, however the use of the high portion is not supported by all of the operations, which is indicated by those registers shaded in light grey in the tables below.

The registers are expanded to 64 Bit and are accessed by prefixing the original 16 Bit register names with 'R', as in RAX, RBX, RCX, RDX etc. in addition 8 new general purpose registers have been introduced simply as numbered registers R8..R15. The new registers also provide additional registers used to access the sub elements of the registers by adding B, W, D suffix to the register name to access the least significant 8 bits, 16 bits and 32 bits respectively.

In addition to the expanded registers set R8..R15, new registers SIL, DIL, BPL and SPL have been added in 64 bit mode to provide access to the least significant 8 bits of the RSI, RDI, RBP and RSP registers

The addition of the new registers and the improved uniformity introduced for accessing the sub elements of the registers have made register management much simpler. Code can be made more efficient by using the faster registers to store intermediate data, needing to spill over to slower memory less frequently.


Sunday, November 6, 2016

Cross Compile for Raspberry PI on Windows 10

Cross Compiling for Raspberry PI or other ARM based devices capable of running Linux, on Windows 10 using the "Window subsystem for Linux" (aka Bash on Windows) is now as simple as cross compiling on native Linux.

To learn more about the Windows subsystem for Linux and how to activate it you can follow the instructions here.

Once you have completed that you can follow these steps to install the toolchain required to cross compile for the Raspberry PI.

Step 1 - Make sure your environment is up to date
$ sudo apt-get update
$ sudo apt-get upgrade 
Step 2 - Install the development tools
$ sudo apt-get install -y build-essential gcc-arm-linux-gnueabi g++-arm-linux-gnueabi 
For the very basics, that is it...

Let's try build some code and get it to run on the Raspberry PI

Step 3 - Create a source directory

In the bash shell you can create a "source" folder in the user directory and switch to the directory by issuing the following commands
$ mkdir ~/source
$ cd ~/source
The first command will create a "source" directory in the current users home directory, the second will change the current directory to the newly created "source" directory.

Step 4 - Write some code

First we will launch the nano text editor, in the Windows bash shell, by issuing the following
$ nano test.cpp 
This command will open the nano editor and set the file name to "test.cpp". Enter the following code in the editor window
#include <iostream>
int main() 
{
 
   std::cout << "Hello from your Raspberry PI!" << std::endl;
 
   return 0; 

Press "Ctrl-X" followed by "y" followed by "enter". 

Now you should be back at the command prompt in the source directory we created earlier. Issuing the "ls" command should show that we have a new file called "test.cpp". 

Step 5 - Compiling using the cross compiler

Next, we can compile the code for the Raspberry PI using the following command  
$ arm-linux-gnueabi-g++ test.cpp -o test 
This will start the compiler, which will compile the test.cpp file and if you have entered the code correctly, create an executable binary file called "test". If you have any errors you can go back to step 4 above and use the same commands to edit the file and fix any errors you might have encountered. 

Assuming you had no errors, you should now have a binary file on your machine called "test". This is the executable that is ready to run on an ARM based device running Linux, like your Raspberry PI. We can confirm that it is indeed not compatible with the x64 architecture of your Windows machine by trying to execute the code as follow. 
$ ./test 
The application should fail to execute and give a messages along the lines of 


bash: ./test: cannot execute binary file: Exec format error. 

This is because the binary is not in the correct format for the platform you are trying to execute it on. We need to copy the binary over to your Raspberry PI and try execute the binary there.

Step 6 - Copy the binary to the Raspberry PI

I am going to assume that you already have your Raspberry PI on the network and have identified the IP address, if not you can use Google or Bing to go get to that point.

In my case, my Raspberry PI is on the network and has been assigned an IP address of 192.168.0.50.

  Note:You need to replace the IP addresses below with the IP address of  your Raspberry PI.

To copy the binary from your windows machine to the Raspberry PI we can use secure copy (scp), this is a Unix/Linux command to securely copy files between two machines using a secure shell. The Windows subsystem for Linux makes these commands available to you "natively" from within the bash shell.

Enter the command below to copy the file from the Windows machine to your Raspberry PI (remember to change the IP address to the IP of your Raspberry PI)
$ scp test pi@192.168.0.50:/home/pi 
If this is the first time you are setting up a secure connection to the Raspberry PI from bash, either using scp or ssh you will be prompted to confirm that you trust the target machine, you can enter "yes" to have the host signature entered into your local known hosts file.

Note: If you took too long to confirm the host, the underlying connection might have been broken, in that case you can just reissue the scp command above.

Next you will need to enter the password, the default password for the "pi" user is "raspberry" which you can enter in response to the password prompt. When the command completes without errors, the binary should be copied over to your Raspberry. Let's go check...

Step 7 - Executing the binary on the Raspberry PI

From within the bash shell on your Windows machine, you can now ssh to to the Raspberry PI. If you prefer you could use Putty, but since we have the capability to use the Windows bash shell to do this I am going to stick with that. The next command will open a ssh session to your Raspberry PI, again, remember to use the IP address for your Raspberry and not the IP address in the example (unless of course your IP matches the on in the example :) )
$ ssh pi@192.168.0.50
Next, you will be prompted for the password for the user "pi", enter "raspberry" and you should now have an interactive secure session with your Raspberry PI. You can identify that you are interacting with the Raspberry and not the local shell by looking at the prompt, it should be a bright green prompt with the following text.
pi@raspberrypi:~ $ 
If you see the above, you know you are now remotely interacting with the Raspberry. Entering "ls" command should list the "test" file, also in bright green, that you copied from the Windows machine.

Lets try executing the binary and see if we have more luck than we did when executing it earlier on the Windows machine.

At the prompt enter "./test", you should then see the following

pi@raspberrypi:~ $ ./test
Hello from your Raspberry PI! 

If you see that then it worked! You compiled a source file on your Windows machine, using the ARM tools to target an ARM based Linux distribution, copied the file to the target device and executed it. How awesome is that???

There is still much more to this, but this should get you started cross compiling on Windows for your Raspberry PI. Best of all, this will also work for other ARM based devices capable of running Linux, like the BeagleBone Black for example.

For a more complete experience, take a look at:

VisualGDB - Use Visual Studio to target a range of embedded devices and Linux devices
Visual C++ for Linux development - Use Visual Studio to compile and deploy to ARM based Linux devices

Tuesday, February 9, 2016

Learning a little TypeScript - Implementing a generic Queue collection

Not being much of a JavaScript guy, other than dabbling when I needed a little client side script, I thought I should probably join the rest of the world and move away from the comfort of C/C++ and C#. Taking baby steps I tried my hand at TypeScript first. I thought others might find my first attempt useful.

Wanting to try out classes and generics, I decided to implement a toy collection class. The snippet  is loosely based on the .NET generic Queue collection interface and implements a similar collection in TypeScript.

Without further ado, here is my very first TypeScript snippet.



And here is a quick and dirty sample showing how the queue could be used


Wednesday, July 9, 2014

Taking my hobby playing with embedded devices to the next level

I have been playing around with all sorts of embedded micro-controllers, ranging from the Atmel AVRs on the Arduino boards, to the powerful ARM Cortex processors running .NETMF and plenty of native code in-between. I recently decided I wanted to try my hand at FPGA, it looked totally foreign and it is not like anything else I had ever done.

All I can say is I am hooked, having the power to construct a processor or logic array to your liking just feels cool. I started off just over a week ago and I took it slow, implementing half-adders then full-adders until I eventually build a full ALU. My goal is to build a custom processor designed to run the Pascal PCode generated by the original UCSD-Pascal compilers.

So far I have not had much to show for it, everything toggles a few LEDs drives a few 7-Segment displays etc. Today I thought I would start playing with the clock management features, the thing with the clocks is without an oscilloscope you cannot really see if things are happening the way you expect. So I thought I would kill two birds with one stone. I will eventually need to generate video output and I need a custom clock to generate the right frequency to get the monitor to display the video data at the target resolution.

The Paplio board that I am using has a Xilinx Spartan 6 FPGA which is being clocked at 32MHz, I was able to synthesize a frequency of 64MHz, close enough to the 65MHz required to support VGA 1024x768 signal. This is not the limit, I am sure it can still go higher, I just have not tried it yet.

Here is a quick video of the system in action

Here is a link to the hardware that I am using
http://papilio.cc/index.php?n=Papilio.Hardware

Saturday, September 8, 2012

New Gadgeteer Hardware and More...

Christmas came early... I ordered a bunch of kit that arrived yesterday, just in time for the weekend.

Development Boards
FEZ Hydra Basic Kit
FEZ Cerberus Mainboard
ST Development Board (STM32F4Discovery)

Modules
Newhaven - LCD Character Display
USB Client SP Module
RS232 Module
OLED Display Module (128x128)
Ethernet ENC28 Module
Extender Module

So far I have tested the OLED, it is much smaller than I expected but it is really a nice little screen. I can see it being very useful for low volume data that needs to be displayed.

The real fun so far was the Newhaven LCD Character display. I am a software guy, so having to solder does not make me feel comfortable. Don't get me wrong, soldering a few header pins on to a board is no issue.

The data sheet for the display refers to jumpers being set to select the communication mode for the display. but when I opened the device there were no jumpers to be found. A quick Bing search and it turns out that what they call jumper are really two pads on the board that need to be shorted. And those pads sit right next to the processor on the display controller.

When I finally decided to bite the bullet and drop a little solder between the pads, my 11 year old daughter came to check-up on what I was doing. She saw the tiny area I was tackling with what seems like a huge soldering iron and promptly offered to help, she suggested I put that the board under a magnifying glass, which I did and the end result I finally wrote a 'Hello World' .NETMF application.

 

Let me tell you I was quite relieved when it all worked.

Now to start playing with the STM32F4 Discovery board...

Saturday, May 26, 2012

Mini-Pacman on Miniature Arcade Console

I recently blogged about some of the stuff I am playing with on the .NET Micro Framework Gadgeteer platform. Kenny Spade from the TinyCLR community forums has kindly built a miniature arcade and put up a video of Mini-Pacman running on the arcade.

It is so cool to see your work move from a bunch of loose pieces lying around on a desk to something that looks so polished. Thanks Kenny!

Sunday, May 20, 2012

Playing with .NET Micro Framework and Gadgeteer

I am sure most people have heard about the .NET Micro Framework and more recently Gadgeteer. While I have had a keen interest for sometime, I never got around to purchasing any of the hardware until about 3 weeks ago.

Here are a few things I have done in my endeavor to learn more about the framework and the embedded devices.

The first application I developed was a little Pac man clone, I made the source for this available on codeplex just follow this link http://chrismcstuff.codeplex.com/

More recently, I have been writing a ray-casting engine in the style of Wolfenstein 3D. Eventually I hope to release this as well, with a simple interface that others can use to develop games of this style.

Here is the latest video of the ray-casting engine.

I will post updates as I make more progress.

I am doing this on the FEZ Spider from GHI Electronics

Sunday, October 2, 2011

My first attempt at using the HTML 5 Canvas

Today I found myself wondering what kind of performance I can get with Javascript and the new HTML 5 Canvas. Looking for something fun to implement quickly I dug up some code for a demo I did of the Silverlight WriteableBitmap and started porting that to Javascript.

Now I am not much of a Javascript guy, in fact I can safely say that this is probably the most Javascript I have written, probably more than all other previous attempts at playing with Javascript put together.

So why am I writing about this if I lack so much experience, well to be honest, it is because I was actually quite surprised by the results and I thought it would be worth sharing.

I did the initial development using IE 9, and I was impressed by the performance, I hit the test page using Firefox 7, Google Chrome 14 and Safari 5.1. By far Firefox was the slowest, IE 9, Chrome and Safari all performed really well on my Window 7 box. So if you are viewing this in Firefox, try it with IE 9 and see if your experience is similar.

Thursday, December 23, 2010

Visualizing Big-O complexity functions

I quickly threw this little Silverlight application together to help visualize some of the common Big-O complexity functions often quoted in any discusion on Data Structures and Algorithms.

This works on Linux Firefox with Moonlight 3 Preview Release it is a little slow but it works.

The following is a brief summary of the functions demonstrated in the application. You can get more detail here.
FunctionDescriptionExample
O(1) Constant complexity regardless of the domain size. Array direct indexing, Hash table
O(n) Linear complexity. As the domain size increases, the time/complexity increases linearly. If you double the items the complexity will double. Sequential search
O(log n) Logarithmic complexity. As the domain size increases, the time/complexity increases logorithmically Binary Search on sorted data, Lookup in balanced binary tree
O(n log n) Log Linear complexity. As the domain size increases, the time/complexity increases at a log linear or geometric rate. Heap Sort, Merge Sort, Quick Sort1
O(n²) Quadratic complexity. As the domain size increases, the time/complexity increases quadratically. Bubble Sort, Insertion sort
O(n³) Cubic complexity. As the domain size increases, the time/complexity increases at a cubic rate. Naive multiplication of two nxn matrices.
O(2ⁿ) Exponential complexity. As the domain size increases, the time/complexity increases exponentialy. Some graph algoritms like finding the exact solution to the traveling salesman problem.
1Quick sort has best and average case of O(n log n) but worst case of O(n²)

Saturday, September 25, 2010

Code Share: Finding controls by control type.

On stackoverflow.com the question was asked, how to ‘Find ContentPlaceHolders in Master Page’

In this case the OP wanted to work with a Master Page other than the one that was already The first part of the problem was getting the Master Page loaded in memory so that the Control tree could be interrogated.

Fortunately loading the Master Page is quite simple, you can use LoadControl to load the Master Page just like you would load any other user control.

For example in the Page_Load handler you could use something like the following to load the Master Page.

var site1Master = LoadControl("Site1.Master");

The next part, finding all the controls of a specific type, requires a simple recursive routine to search the control tree for all the controls of the type that you are interested in. Here is a simple implementation of just such a routine.



static class WebHelper
{
public static IList<T> FindControlsByType<T>(Control root)
where T : Control
{
if (root == null) throw new ArgumentNullException("root");

List<T> controls = new List<T>();
FindControlsByType<T>(root, controls);
return controls;
}

private static void FindControlsByType<T>(Control root, IList<T> controls)
where T : Control
{
foreach (Control control in root.Controls)
{
if (control is T)
{
controls.Add(control as T);
}
if (control.Controls.Count > 0)
{
FindControlsByType<T>(control, controls);
}
}
}
}

Using the tow pieces of code above, finding all the ContentPlaceHolders on the Master Page can be done like this.



// Load the Master Page
var site1Master = LoadControl("Site1.Master");

// Find the list of ContentPlaceHolder controls
var controls = WebHelper.FindControlsByType<ContentPlaceHolder>(site1Master);

// Do something with each control that was found
foreach (var control in controls)
{
Response.Write(control.ClientID);
Response.Write("<br />");
}


Hope someone finds this useful. I thought I would share it since I took the few moments to write it and did not want it to go to waste.