Jump to content
Jet Set Willy & Manic Miner Community

source code for JSW


Norman Sword

Recommended Posts

                        Routine savings           Total saving
Rope compression    258                          258
cheat code                51                           309
room draw                 50                           359
lower att draw +
logo draw                400+                         759+
twinkle                     39                            798+
clock update            14                             812+
room exits               36                             848

conveyor                   9                              857

room storage          120                             977

Edited by Norman Sword
Link to comment
Share on other sites

S_M_C_    Self Modifed Code

 

 

Quick answer:-   The data is part of code that is Self modified. e.g. Self_modified_code

 

 

An explanation.

 

In every listing or source code I write. I make the distinction between variables, labels and the one part of an assembly listing that can or could cause problems. When writing code it is possible to design code to have multiple entry points, be recursive  and even have code that changes by virtue of the code modifying itself.

 

When code modifies itself we can have references to opcodes and variables in the code that might not be directly situated on a label. These references cause problems because they look the same as any other part of the code and will cause unknown problems if edited. It is better to basically red flag the code

 

for example:- I can write

 

FLUX:   ld hl,100

 

more code.......

 

           ld hl,200

          ld (FLUX+1),hl

 

Editing the code around FLUX looks simple enough, and when the code has only a hundred or so lines easy enough to remember, Consider the situation when your code has grown to 30,000 lines of code with hundreds of routines. Can you now assume you will always remember that FLUX needs to be treated with caution.

 

So to remove those easily forgotten references to code that changes I introduced a standard prefix to any label that will have something modified because of it.

 

The label might be referencing an op-codes or the data of an opcode. The type of reference is unimportant. What the label prefix is forcing on you as you see it in the code and see any reference to that label in the code. Is the caution that what you read in the source listing might NOT be what is seen as the code is run.

 

The prefix reference also forces caution on the code around it. The example above re written:-

 

FLUX:

S_M_C_flux  equ $+1

      ld hl,100

 

more code.......

 

           ld hl,200
          ld (S_M_C_flux),hl

 

A quick scan of the code alerts us to the presence of code that is being modified.

example 2

S_M_C_ direction: inc a

lots of  code....

 

    xor a                                ;kill movement

   ld (S_M_C_direction),a

 

lots more code...

 

  ld a,$3d                            ;reverse direction

  ld (S_M_C_direction),a

 

 

If the label S_M_C_direction: was changed to be just direction: it becomes very easy to edit the op code or move the op code. "direction:" would look to be just another label and the editing of the code directly after the label would seem to be no problem. If the code is part of a program that has lots of program paths, you edit can throw up unexpected problems that can take a long time to find.  Far easier to red flag it. 


so S_M_C_ self modified code  or self Modifying code..... Both versions imply the same caution.

 

 

 

Addendum:-   In the source listing for Jet set willy, I have used the same S_M_C_    prefix. It is used in the only part of Matthews code that modifies itself.             at $8D5C    we have S_M_C_page    , where the page offset is changed

Edited by Norman Sword
Link to comment
Share on other sites

Regarding the format here:

 

 


S_M_C_pointer_room_data equ $+1

    ld hl,$-$

 

Does the "$-$" bit refer to the fact that the operand is a two-byte word?

 

EDIT: Apparently not - I've just noticed the self-modifying code in your source listing for Matthew's original code:

 

 


S_M_C_page: equ $+1 ;
LD D,$-$ ;L8D5C
 
In that case, the operand is only a single byte, yet you still use the same format: "$-$".
Edited by IRF
Link to comment
Share on other sites

The usage of $-$.

When using self modified code we have the situation that the code is changed depending on what the program flow is. In certain circumstances we know that the reference we see is always modified before the part of the code we are looking at is executed.

When we know that the code is changed before it is executed the value stored is of no importance when it is written.

So having code say LD A,7 ; and knowing that the value is supplied and always set before we execute the opcode, seems to be pointless. The S_M_C_ is indicating that the code is being modified. The $-$ is indicating that the value will be supplied before the opcode is executed.  It is also giving a very good indication of what is being modified.

Edited by Norman Sword
Link to comment
Share on other sites

Would it be worth making a distinction between a single byte being subject to modification, and a two-byte word?

 

Like LD D, $-

versus:

LD HL, $-$-

 

Or something?

 

On reflection, I suppose it's obvious from the context how many bytes are affected (i.e. how many are going to be self-modified by the program).

Link to comment
Share on other sites

The opcode decides the size of the data used. I can not load the "A" register with 16 bits.

In a similar way if I was to specify ld hl,2. I am always loading 16 bits never 8 bits.

The $-$ is specifying 0. It is indicating a null value. The opcode is specifying the size of the data. 

 

you can not specify ld d,$-        the syntax is wrong.
you could specify ld d,-$

 

   LD D,$ or any variation on that will load "D" with the low value of the program counter at that point. The value would change on each and every edit of the code if the program counter at that point is changed by edits.

The $-$ is a consistent and fixed value of zero


If you write enough code you will end up having to use references to data that you do not have. E.g. call $-$   which is a call to somewhere, but you do not have the address. The address is supplied by some other piece of code and in any case can move. So an address needs to be supplied to the assembler $-$ is as good an address as going out of your way to fabricate an address just to assemble the code.



 

Edited by Norman Sword
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.