17 April 2021, 15:17 | #1 |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
Self-modifying code not removing itself
Hello,
I'm writing this piece of code: Code:
.notFirstBlock: MOVE.W P61_LAST_POS,D0 CMPI #0,D0 BEQ.S .dontStartSequencer LEA .notFirstBlock,A0 MOVE.L #$4E714E71,(A0)+ ; NOP opcodes MOVE.L #$4E714E71,(A0)+ ; NOP opcodes MOVE.L #$4E714E71,(A0)+ ; NOP opcodes BSR.W __SET_SEQUENCER_LEDS .dontStartSequencer: This works fine but the three MOVEs won't remove themself for some reason. debugging this is how code looks like after the three MOVE.L: Code:
0001C75C 4e71 NOP 0001C75E 4e71 NOP 0001C760 4e71 NOP 0001C762 4e71 NOP 0001C764 4e71 NOP 0001C766 4e71 NOP 0001C768 20fc 4e71 4e71 MOVE.L #$4e714e71,(A0)+ [4e714e71] 0001C76E 20fc 4e71 4e71 MOVE.L #$4e714e71,(A0)+ [4e714e71] 0001C774 20fc 4e71 4e71 MOVE.L #$4e714e71,(A0)+ [4e714e71] Maybe this is a limitation of the CPU? or what? Also, inserting NOPs doesn't look lie the best idea to me and I'm wondering if there is something else better for this purpose. Thanks for any help |
17 April 2021, 15:22 | #2 |
move.l #$c0ff33,throat
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
|
I don't really understand your problem (it certainly is not a limitation of the CPU if code is not removed/changed/modified properly) but my first question is why you are using such a dirty approach anyway? This can be easily done without any SMC at all.
Last edited by StingRay; 17 April 2021 at 15:29. Reason: Typo |
17 April 2021, 15:30 | #3 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,423
|
Setting aside the question of why you'd want this or not want this, doesn't the code do exactly what you ask for?
If I'm reading this correctly, .notfirstblock is exactly six words away from the moves you execute. So LEA .notfirstblock,A0 and then move.l <blah>,(A0)+ three times will overwrite only the first six words and since the moves only start at the 7th word... They don't get overwritten. |
17 April 2021, 15:46 | #4 |
Going nowhere
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 9,014
|
As others have said, using SMC is in this modern age of more than 68000 processors, just a non starter.
Why not simply set a bit/byte in memory that skips that code once a set condition is met? I just don't care why your SMC doesn't work as you expect, I just care that there is no need for it and alternatives will cause no issues on faster processors. |
17 April 2021, 15:53 | #5 | |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
Quote:
thanks! |
|
17 April 2021, 15:54 | #6 |
Registered User
Join Date: Apr 2018
Location: UK
Posts: 489
|
He obviously has his reasons, he's just asking a question, don't go all stackoverflow on him.
|
17 April 2021, 15:59 | #7 |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
To answer why I choose this approach: get rid of unused CMPs. I know that a certain event must happen on every frame after a given value has changed and for this I check the value but afterwards I just want it to happen, without having to check for it every frame, forever even knowing it wont change.
I also like to experiment and learn |
17 April 2021, 16:08 | #8 | |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
Quote:
BTW I quite expected someone to get a bit upset, it always happens with SMC topic However I think it's a good idea to get rid of a CMP if it's there just to prevent something to happen from the beginning and is not needed anymore. I'm hoping to learn some alternatives, if any exist. |
|
17 April 2021, 16:36 | #9 |
move.l #$c0ff33,throat
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
|
This is a good reason but such optimisations should only be done if there is no other way to get the code run as fast as possible. SMC requires to either lock the code to 68000 only, have code to disable (instruction) caches or 2 different code paths for 68000 and 680x0 machines. Up to you do decide if SMC is really worth it. |
17 April 2021, 17:01 | #10 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,043
|
Unless this is repeated a lot in a loop, SMC is not worth the trouble...
Try these options instead: Code:
MOVE.W P61_LAST_POS,D0 CMPI.w stopper(pc),D0 BEQ.S .dontStartSequencer st stopper BSR.W __SET_SEQUENCER_LEDS .dontStartSequencer: ... stopper: DC.W 0 Code:
move.l seq_ptr(pc),a0 jsr (a0) .dontStartSequencer: ... seq_ptr: DC.L seq_checker seq_checker: tst.w P61_LAST_POS beq.s .done lea __SET_SEQUENCER_LEDS(pc),a0 move.l a0,seq_ptr jmp (a0) .done: rts |
17 April 2021, 19:47 | #11 |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
So I came out with this solution:
Code:
.notFirstBlock: LEA (PC),A2 MOVE.W P61_LAST_POS,D0 CMPI #0,D0 BEQ.S .dontStartSequencer LEA .skipCMP,A1 SUB.W A2,A1 MOVE.W A1,D0 EOR.W #$6000,D0 ; BRA.S .skipCMP LEA .notFirstBlock,A0 MOVE.W D0,(A0) .skipCMP: BSR.W __SET_SEQUENCER_LEDS .dontStartSequencer: |
17 April 2021, 21:44 | #12 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,043
|
If you insist on solving this simple problem with SMC (make sure you are running on 68000/010), this is a shorter and faster way to do it:
Code:
.notFirstBlock: tst.w P61_LAST_POS beq.b .dontStartSequencer move.w #$6000|(.call-(.notFirstBlock+2)),.notFirstBlock .call: bsr.w __SET_SEQUENCER_LEDS .dontStartSequencer: |
18 April 2021, 02:30 | #13 |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
and much better, thanks!
Don't worry, I'll test it properly. For now it works on all emulated CPUs and Vampire. |
18 April 2021, 10:50 | #14 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,247
|
No, it doesn't. Self-modifying code requires a data-cache push for the 68040 onwards, and a instruction cache flush for the 68020 onwards. Otherwise, your modified instructions might never be seen by those processors. If it works, then only by pure chance....
|
18 April 2021, 12:20 | #15 |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
Yes, I rephrase: it doesn't crash but the code works.
In this scenario if the modified instruction is not seen then the program will just keep checking for the value and follow its flow. This is OK since saving a few cycles on CPUs > 68000 is not needed. |
18 April 2021, 22:44 | #16 |
OctaMED Music Composer
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 670
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Questions related to WHDLoad, modifying code and original files... | Foebane | project.WHDLoad | 17 | 25 May 2018 17:19 |
Resource : modifying a line of code | kamelito | support.Apps | 7 | 28 December 2017 11:25 |
modifying PC case for A4000 | orange | Hardware mods | 3 | 12 November 2010 22:05 |
Modifying a copperlist | CmdrVimes | Coders. General | 5 | 06 September 2010 12:08 |
Modifying 3.1 rom | utri007 | support.Hardware | 6 | 30 December 2005 23:55 |
|
|