Previous page Select page Next page

Long jumps and short jumps

When you compiled the code, did it work? Probably not - I'll bet that you got a warning that your jump to "position_8" was more than 127 bytes away and your were then given an idea of by how much. That is because goAsm tries to store the length of the jump in a single signed byte. So, we can jump 127 bytes forward and a maximum of 128 bytes backwards. During the compilation, goAsm has worked out how far it is from that jump to the required label and discovered that it is not possible, using a single byte.

We will therefore have to use 2 bytes to store the problematic jump. It's very simple to do - instead of using the "greater than" sign, we use two of them:


    ; 4.  Jump to the relevant code

    Cmp Ebx, 0

      Jz >.position_0

    Cmp Ebx, 2

      Jz >.position_2

    Cmp Ebx, 6

      Jz >.position_6

    Cmp Ebx, 8

      Jz >>.position_8

Note that you should try to keep the other jumps as single symbols since the use of a single byte instead of two lets your program run much faster in certain circumstances. Recompile it and this time, when you resize the window by dragging it with the mouse, you should see the sort of effect shown in the animation here.

Improving the "peg" handler - step 4

In the last chapter, I spoke about the dangers of using "magic numbers" but you will have noticed that I have done just that in the code we have just been dissecting. I have used the number "4" to represent the border. How could we remove this and make the code more flexible? We could add another parameter to the pegPosition routine which would allow us to pass the actual size of the border we wanted. That means each of the four buttons in the example above could have a different border size.

So, I want you to change the signature of the routine by adding a new parameter - let's call it "pegBorder" - and then change the code to use this variable instead of the "4" we used earlier. What else will we have to do? We'll have to go back to our "OnResize" handler and cgange the calls to pegPosition to include the new parameter. If you get really stuck, click here to see my solutions for the OnResize and pegPosition routines. Click the "back" button on your browser to return here.

Previous page Select page Next page