English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 17 April 2021, 15:17   #1
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
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:
which basically checks for a value and when this value changes the code removes itself, leaving just a BSR

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]
so the three MOVEs have generated six NOPs which replaced the instructions up to LEA but leaving the rest in place.

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
KONEY is offline  
Old 17 April 2021, 15:22   #2
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
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
StingRay is offline  
Old 17 April 2021, 15:30   #3
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,408
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.
roondar is offline  
Old 17 April 2021, 15:46   #4
Galahad/FLT
Going nowhere
 
Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 8,986
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.
Galahad/FLT is offline  
Old 17 April 2021, 15:53   #5
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
Quote:
Originally Posted by roondar View Post
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.
Ok, this is clear. What I did not take into account is that every MOVE.L will add three words the the count while replace only two with NOPs so the more are there the more are needed to remove themselves.

thanks!
KONEY is offline  
Old 17 April 2021, 15:54   #6
rothers
Registered User
 
Join Date: Apr 2018
Location: UK
Posts: 487
He obviously has his reasons, he's just asking a question, don't go all stackoverflow on him.
rothers is offline  
Old 17 April 2021, 15:59   #7
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
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
KONEY is offline  
Old 17 April 2021, 16:08   #8
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
Quote:
Originally Posted by rothers View Post
He obviously has his reasons, he's just asking a question, don't go all stackoverflow on him.
LOL "stackeroverflow him"

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.
KONEY is offline  
Old 17 April 2021, 16:36   #9
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
Quote:
Originally Posted by KONEY View Post
To answer why I choose this approach: get rid of unused CMPs.

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.
StingRay is offline  
Old 17 April 2021, 17:01   #10
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,039
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
Or:
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
a/b is offline  
Old 17 April 2021, 19:47   #11
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
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:
so instead of inserting NOPs I just mock a "BRA .skipCMP" in place of the first instruction to skip everything.
KONEY is offline  
Old 17 April 2021, 21:44   #12
a/b
Registered User
 
Join Date: Jun 2016
Location: europe
Posts: 1,039
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:
a/b is offline  
Old 18 April 2021, 02:30   #13
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
and much better, thanks!

Don't worry, I'll test it properly. For now it works on all emulated CPUs and Vampire.
KONEY is offline  
Old 18 April 2021, 10:50   #14
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,215
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....
Thomas Richter is offline  
Old 18 April 2021, 12:20   #15
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
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.
KONEY is offline  
Old 18 April 2021, 22:44   #16
KONEY
OctaMED Music Composer
 
KONEY's Avatar
 
Join Date: Jan 2009
Location: Venice - Italy
Age: 49
Posts: 666
This short syntax instruction is super cool! Are they explained is some docs?

Quote:
Originally Posted by a/b View Post
Code:
.notFirstBlock:
	tst.w	P61_LAST_POS
	beq.b	.dontStartSequencer
	move.w	#$6000|(.call-(.notFirstBlock+2)),.notFirstBlock
.call:	bsr.w	__SET_SEQUENCER_LEDS
.dontStartSequencer:
KONEY is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 17:07.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09015 seconds with 15 queries