20 August 2014, 21:23 | #1 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
string starts with
Can you show me an asm routine that checks if the string in a0 starts with the string in a1? Both are null terminated.
|
20 August 2014, 21:29 | #2 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 6,985
|
Code:
loop: cmp.b (a0)+,(a1)+ bne.s loop subq.l #1,a1 tst.b (a1) bne does_not_match Edit: no, that one does not stop if both strings are equal. This one is better: Code:
loop: move.b (a0)+,d0 cmp.b d0,(a1)+ bne does_not_match tst.b d0 bne.s loop Code:
loop: move.b (a0)+,d0 move.b (a1)+,d1 cmp.b d0,d1 bne check tst.b d0 bne.s loop bra strings_are_equal check: tst.b d1 bne does_not_match does_match: Last edited by thomas; 20 August 2014 at 21:40. |
20 August 2014, 21:32 | #3 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Thanks but as far I can see the bne.s schould be a beq.s? (in your first example)
|
20 August 2014, 21:46 | #4 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 6,985
|
Forget the first two tries. That was just brainstorming.
|
20 August 2014, 21:52 | #5 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
|
20 August 2014, 22:07 | #6 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
Ok, works, thank you. By the way, in my case it can't happen that both strings are equal.
|
20 August 2014, 22:23 | #7 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Hmm, I think gcc came up with a better one (unless I have a bug)
Code:
loop: move.b (a1)+,d0 cmp.b (a0)+,d0 beq.s loop tst.b d0 bne does_not_start_with_a1 ;a0 starts with a1 here Code:
.L3: move.b (%a0)+,%d0 move.b (%a1)+,%d1 cmp.b %d0,%d1 jne .L2 tst.b %d0 jne .L3 ;match here .L2: tst.b %d1 bne do_not_match Last edited by alkis; 20 August 2014 at 22:49. |
20 August 2014, 22:43 | #8 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 6,985
|
|
20 August 2014, 22:47 | #9 | |
Registered User
Join Date: Jan 2012
Location: USA
Posts: 372
|
Quote:
Code:
next: move.b (a1)+, d0 beq.s match cmp.b (a0)+, d0 beq.s next no_match: ... match: ... |
|
20 August 2014, 22:52 | #10 |
XoXo/Tasko Developer
Join Date: Dec 2013
Location: Munich
Age: 48
Posts: 450
|
In my case:
- there are no empty strings, neither a0 nor a1 - the strings are never just equal |
21 August 2014, 22:31 | #11 |
AMOS Extensions Developer
Join Date: Jun 2007
Location: near Cambridge, UK
Age: 44
Posts: 1,924
|
|
22 August 2014, 17:28 | #13 | |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Quote:
Nevertheless, after you raised the point, I checked with the first 68000 manual that I found on my hard disk, and it says that the displacement for bCC is either a byte or word. I guess that makes beq.s valid. Edit: Also, I compiled to .o and when I 'objdump' it, gcc says "beq.s" Last edited by alkis; 22 August 2014 at 17:34. |
|
22 August 2014, 17:41 | #14 |
move.l #$c0ff33,throat
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
|
Strictly speaking, there is indeed no beq.s instruction (but most assemblers accept that syntax) but there sure is beq.b, i.e. a short distance (byte) branch! It won't be converted to beq.w (unless the distance is too large).
|
22 August 2014, 19:31 | #15 | |
AMOS Extensions Developer
Join Date: Jun 2007
Location: near Cambridge, UK
Age: 44
Posts: 1,924
|
Quote:
So there is a bytewise short distance conditional branch. I'll have to change some of my game code as I have been using word branch's (e.g. beq.w) where a byte one would be sufficient! |
|
22 August 2014, 20:55 | #16 | |
Registered User
Join Date: Jan 2012
Location: USA
Posts: 372
|
Quote:
It saves one word of space and takes eight cycles when a branch is not taken versus 12 cycles for the the 16-bit displacement version. That doesn't sound like a big win since those four cycles are only saved if the branch isn't taken, but it can make a difference in loops where there's a bailout branch in the middle of the loop that usually falls through. |
|
22 August 2014, 21:52 | #17 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
Just type
BEQwithout the size extension and let the assembler worry about it it will always do a better job than the programmer when it comes to this kind of peephole optimizations. |
23 August 2014, 00:04 | #18 | |
Registered User
Join Date: Jan 2012
Location: USA
Posts: 372
|
Quote:
Code:
nextstring: move.l (a2)+, d1 beq nextstring move.l -4(a2), a1 loop1: move (a0)+, d0 beq match cmp.b (a1)+, d0 beq loop1 nomatch: ... match: org $2000 ... beq nextstringis optimized into beq.s nextstringand that moves match:from being 126 bytes away to 128 bytes away from beq match? In that case beq matchmust be turned into beq.w match. The faster code sequence then is data-dependent. If more time is spent searching for the next string, then the first branch should be a short branch. If more time is spent searching for the substring then the first branch should be word sized. |
|
23 August 2014, 13:56 | #19 |
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
|
Sure but this is not a very common scenario, and I would say at this point you're reasoning and optimizing on algorithm level, so it's outside the scope of what the assembler does.
The reality is that a programmer who is not using an optimizing assembler will always end up spending more time writing more clutter into the code, and get a worse result. |
23 August 2014, 14:32 | #20 | ||
Banned
Join Date: Jan 2010
Location: Kansas
Posts: 1,284
|
There is a longword displacement branch for the 68020 also.
Bcc.B = Bcc.S signed 8 bit displacement, 2 byte instruction length Bcc.W signed 16 bit displacement, 4 byte instruction length Bcc.L signed 32 bit displacement, 6 byte instruction length DBcc signed 16 bit displacement, 4 byte instruction length FBcc.W signed 16 bit displacement, 4 byte instruction length FBcc.L signed 32 bit displacement, 6 byte instruction length Quote:
Quote:
Vasm does multiple optimization passes which I believe takes care of displacements that come into range due to optimizations. |
||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
highlight string | kkhard | Coders. C/C++ | 0 | 15 August 2014 04:26 |
Project 500 starts... | Paul_s | Hardware pics | 34 | 03 July 2008 01:01 |
The updating starts :) | Galahad/FLT | Amiga scene | 14 | 30 September 2005 18:58 |
Winuae stop starts! | stainy | support.WinUAE | 1 | 14 November 2001 01:36 |
|
|