English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General > Coders. Tutorials

 
 
Thread Tools
Old 26 January 2007, 10:01   #41
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
Right seeing as nobody fancied it here is the full code to the previous routine.

Code:
        lea       TEXT,a0       Address of TEXT into a0
        move.b    (a0),d0        Copy count into d0 (5)
        addq.l    #1,a0          Increase a0 to point to first char of word
        lea        COPY,a1       address of COPY buffer into a1

LOOP    move.b    (a0),(a1)      Copy contents of address pointed by a0 to address pointed by a1
        addq.l    #1,a0          Increase address in a0 by 1
        addq.l    #1,a1          and same for a1
        subq.b    #1,d0          Decrease loop count by 1
        bne        LOOP          if d0 is unequal to 0 loop

        move.b    #0,(a1)        add terminating NULL
        rts                      End program

TEXT    dc.b      5,"HELLO"
COPY    ds.b      6
The 68k family of processors has an addressing mode which allows us to refine and improve this code to make it smaller and quicker. The addressing modes are called Address Register Indirect with Post Increment and Address Register Indirect with Pre Decrement.

With Post Increment the code is copied and then the source address is automatically increased by 1 for byte, 2 for word and 4 for long-word. To achieve this a + is added directly after the address register (a0)+, so the following code

Code:
move.b (a0), d0
addq #1,a0
can be replaced with the following which does exactly the same

Code:
move.b (a0)+,d0
What's happening is the contents pointed to by a0 is being copied into d0 and then the adress in a0 is increased by 1.

Auto Decrement works by first decreasing the address and then copying the data. Example:

Code:
move.b -(a0),d0
Our program will now look like the following

Code:
        lea       TEXT,a0        Address of TEXT into a0
        move.b    (a0)+,d0       Copy count into d0 (5) and increase address in a0
        lea       COPY,a1        address of COPY buffer into a1

LOOP    move.b    (a0)+,(a1)+    Copy contents of address pointed by a0 to address pointed by a1 and increase both by 1
        subq.b    #1,d0          Decrease loop count by 1
        bne       LOOP           if d0 is unequal to 0 loop

        move.b    #0,(a1)        add terminating NULL
        rts                      End program

TEXT    dc.b      5,"HELLO"
COPY    ds.b      6
This code can be improved further by using DBcc loops, does someone NOT experienced want to have a go at researching this and posting the code?

Last edited by BippyM; 26 January 2007 at 10:10.
BippyM is offline  
Old 28 January 2007, 11:29   #42
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
DBcc loops

To improve our program further we can use a DBcc loop (Test Condition - Decrement and Branch) the cc represents the test and loop type.

The DBcc instructions tests both the status flags and the data registers. There are differences between loops written using dbcc loops and those using conventional loops (bcc (bra, beq, bne etc)). If the condition being tested is satisfied then control passes to the instructions following the dbcc. If it is not satisfied then the low word of the data register is reduced by 1 and the loop is taken. The loop is continued until the result in the data register equals -1.

Code:
LOOP    move.b    (a0)+,(a1)+    Copy characters
        dbra      d0,LOOP        deduct 1 off d0 and loop until d0 equals-1
This code will keep on looping until d0 equals -1. When it does the program resumes.

Now because dbcc tests until the result equals -1 we need to reduce the count in d0 by 1 otherwise it'll do 1 too many loops. A subq.b #1,d0 is all that is needed

Code:
        lea TEXT, a0               address into a0
        move.b move.b (a0+), d0    Copy counter into d0 and increase a0
        sub.b #1, d0               Decrease d0 by 1 for the dbra loop
        lea COPY, a1               address of dest into a1

LOOP    move.b (a0)+, (a1)+        Copy and increment
        dbra d0, LOOP              Test is d0 =-1 and if not decrease and loop
        move.b #0, (a1)            Add terminating 0
        rts

TEXT    dc.b 5, "HELLO"
COPY    ds.b 6
BippyM is offline  
Old 28 January 2007, 13:15   #43
thor
Registered User
 
thor's Avatar
 
Join Date: Mar 2006
Location: Germany
Posts: 899
Maybe d0 should be cleared first, as DBcc operates on word size and if there's something in d0 it may not work as wanted (actually happens when using ASMOne's debugger the second time with this code, because $FFFF is still in d0).
thor is offline  
Old 28 January 2007, 13:33   #44
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
Hmm it doesn't discuss that in the asm book i'm reading so either

clr.l d0 or move.l #0,d0
BippyM is offline  
Old 28 January 2007, 13:34   #45
thor
Registered User
 
thor's Avatar
 
Join Date: Mar 2006
Location: Germany
Posts: 899
better moveq #0,d0
thor is offline  
Old 28 January 2007, 13:40   #46
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
Cool

The next chapter in the book discusses program design and the use of Warnier Diagrams

I'm not going to implicitly discuss program design here, I am however going to make a new thread about it so it can be discussed more openely and others can give advice on what they do etc.

If anyone wants me to go through program design the book refers to then I will.
BippyM is offline  
Old 28 January 2007, 19:24   #47
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
From now on we will be doing some amiga specific stuff and starting to delve into amiga libraries etc. As before i'll be taking notes and for the first few posts it is going to be amiga related, starting with some hardware basics. I have not actually started these chapters yet, but i will tonight so expect a post covering this stuff over the next few days.

Has anyone learnt anything yet and are there any questions before I move on?
BippyM is offline  
Old 28 January 2007, 21:07   #48
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
Quote:
Originally Posted by bippym
I have not actually started these chapters yet, but i will tonight so expect a post covering this stuff over the next few days.
Have a good reading time, don't forget to have enough coffee available as it helps a lot.
StingRay is offline  
Old 28 January 2007, 21:08   #49
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
Oh, regarding dbf loops, on 060 it's faster to use the subq/bne combination.

Quote:
LOOP move.b (a0)+, (a1)+ Copy and increment
dbra d0, LOOP Test is d0 =-1 and if not decrease and loop
move.b #0, (a1) Add terminating 0
rts
The "move.b #0,(a1)" is actually not necessary since you have a cleared buffer already (ds.b 6). It's better to use "clr.b (a1)" (or "sf (a1)") instead of the move.b #0,(a1), these instructions are faster and shorter.
StingRay is offline  
Old 28 January 2007, 22:57   #50
billy
Registered User
 
Join Date: Jun 2005
Location: US / A
Age: 49
Posts: 134
ASM again and again.

Interesting thread bippy. Keep it up please!
billy is offline  
Old 29 January 2007, 01:04   #51
Galahad/FLT
Going nowhere
 
Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 8,989
Also regarding Dbra routines, they are limited in the size of data for copying, only useful for small copying routines, for something like a screen, you would use a subq.l # in conjunction with a bne.s
Galahad/FLT is offline  
Old 09 February 2007, 17:35   #52
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
Been busy doing some action replay and hacking about in code, but here is the next part of this

It's not complete yet so this post will be updated when it is I also said we'd be covering amiga specific stuff but I seemed tro have missed a chapter

-----

Subroutines & Parameter Addressing

There are two methods for transferring program control to a subroutine. There is the Jump to SubRoutine command (JSR), this causes an unconditional jump to a specified memory address. This instruction behaves just like the JMP command only the return address is placed on the stack, so the proggram can return from the subroutine. When the RTS is executed the return address is placed back on the PC and the program continues from the command after the calling routine.

Stack

The 68k family of processors allow the stack to be placed anywhere in memory. Register a7 is used to hold the address of the top of the stack. Before control is passed to a sub-routine the processor calculates the address of the next command and places the address on the stack. 680x0 stacks then grow downwards in memory, and since the SP always points to the last item added to the stack this means, this means before adding a new item you must first decrease the stack pointer by a number equivilent to the byte-size of the object being stored. This way it properly points to the location to be used next.

There are two ways to pass data between subroutines. There is register based parameter passing, this method is both quick and simple, since pointers to larger objects, such as strings and other blocks of data can be passed. The routine is passed the address of the object as opposed to the object itself. Memory based parameter addressing is slower but does offer more flexibility.

Register Preservation

Normally it is advised to create subroutines that do not alter the contents of any temporary registers that they may use, i.e those that will not be used to return a result. The best way of preserving these registers is to push their contents onto the stack, and restore them just before the subroutine returns. One way to do this is as follows

Code:
move.l    a6, -(sp)        preserve a6
move.l    a5, -(sp)        preserve a5
<do something>
move.l    (sp)+, a5        restore a5
move.l    (sp)+, a6        restore a6
There is however a command that can be used to preserve/restore multiple registers at once. Itr is called Movem

Movem exists in two forms. The instruction used when moving multiple registers to memory is called move-multiple-registers-to-memory (movem) it can use all absolute and indirect addressing modes except auto-increment modes. This is deliberate because it stops the programmer putting data on the stack, which would cause the stack to grow upwards in memory.

Internally the command is built in the following way: The first word contains bit patterns which identify the instruction, the transfer size and the effective destination specification. The second word is a 16bit mask which has been assigned registers in the follwoing fashion

Code:
a7 a6 a5 a4 a3 a2 a1 a0 d7 d6 d5 d4 d3 d2 d1 d0
or if the predecrement mode is used like this

Code:
d0 d1 d2 d3 d4 d5 d6 d7 a0 a1 a2 a3 a4 a5 a6 a7
registers are moved in the order bit 0, bit 1, bit 2 etc. of the mask. The order therefore is d0 d1 d2 etc for the normal mask and a7 a6 a5 for the reserved mask (assuming the appropriate mask bits are set to 1)

The equivilent move-multiple-registers-from-memory movem instruction does not use this mask reversal. In fact it always uses the bit mask arrangement described first: a7 to a0 & d7 to d0 and instead allows the auto-increment addressing mode but does not allow auto-decrement.

more to come.. keep posted

Last edited by BippyM; 09 February 2007 at 17:54.
BippyM is offline  
Old 13 February 2009, 17:30   #53
Herpes
... aka Amix73
 
Herpes's Avatar
 
Join Date: Jan 2009
Location: Austria
Posts: 87
Two years have gone by ... I am sure you are an expert by now. Really enjoyed reading it any chance that it will be continued?
Herpes is offline  
Old 13 February 2009, 17:56   #54
BippyM
Global Moderator
 
BippyM's Avatar
 
Join Date: Nov 2001
Location: Derby, UK
Age: 48
Posts: 9,355
yeah expert if I continued regularly.. I always get distracted and go away from it, then come back again..

Right now I am far too busy to do anything, so it's gonna be a short while
BippyM is offline  
Old 13 February 2009, 18:47   #55
Photon
Moderator
 
Photon's Avatar
 
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,604
I might translate my asm school from "Svenska Hemdator Hacking" magazine if someone can point me to scans, has scans of it come out of that AMR thread? Clue me in.
Photon is offline  
Old 15 February 2009, 01:58   #56
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
Quote:
Originally Posted by Herpes View Post
Really enjoyed reading it any chance that it will be continued?
I second this It´s a very interesting thread..
If bippym can´t maybe someone want to continue this thread
AlfaRomeo is offline  
Old 17 February 2009, 00:38   #57
Herpes
... aka Amix73
 
Herpes's Avatar
 
Join Date: Jan 2009
Location: Austria
Posts: 87
Quote:
Originally Posted by Photon View Post
I might translate my asm school from "Svenska Hemdator Hacking" magazine if someone can point me to scans, has scans of it come out of that AMR thread? Clue me in.
That would be cool, of course ! New (at least for me) & interesting info - I hardly can't wait!
Herpes is offline  
Old 12 April 2009, 09:03   #58
Loedown
Precious & fragile things
 
Join Date: Feb 2009
Location: Victoria, Australia
Posts: 1,946
Greetings,
I find it both useful and terribly processor specific. Coding for PIC is good, you can define labels for loop points within Microchip's coder,

Code:
           start2          org 0005        
                           clrf    temp0
                           clrf    temp1
                           clrf    temp2
                           clrf    temp3
                           clrf    temp4
                           clrf    temp5
                           clrf    temp6
                           clrf    temp7
                           clrf    temp8
                           clrf    temp9
                           clrf    intcon        ;clears intcon register
                           movlw    b'00000001'
                           movwf    temp7        ;sets temp7 as high bit check
                           movlw    b'00000101'    ;sets timer 1, no prescale
                           movwf    t1con
                           clrw
Was just a chunk of code from the start of one of my projects, I used to love trying to predict the old BEQ and BNE values for branching on 6502, this is where labels really come in handy, so instead of;

Code:
LDA $08
LDX $08
SBC
BNE $03
JMP $FFD2
LDA $40

You can use;

       LDA $08
       LDX $08
       SBC
       BNE JUMP:
JUMP:  JMP $FFD2
       LDA $40
Paul
Loedown is offline  
Old 06 June 2010, 19:03   #59
AlfaRomeo
A1200 040 SAM440EP 667
 
AlfaRomeo's Avatar
 
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
[quote=bippym;296073]Right seeing as nobody fancied it here is the full code to the previous routine.

Code:
        lea       TEXT,a0       Address of TEXT into a0
        move.b    (a0),d0        Copy count into d0 (5)
        addq.l    #1,a0          Increase a0 to point to first char of word
        lea        COPY,a1       address of COPY buffer into a1
 
LOOP    move.b    (a0),(a1)      Copy contents of address pointed by a0 to address pointed by a1
        addq.l    #1,a0          Increase address in a0 by 1
        addq.l    #1,a1          and same for a1
        subq.b    #1,d0          Decrease loop count by 1
        bne        LOOP          if d0 is unequal to 0 loop
 
        move.b    #0,(a1)        add terminating NULL
        rts                      End program
 
TEXT    dc.b      5,"HELLO"
COPY    ds.b      6
[quote]

Tried to assemble this code with devpac and all goes well but when run the code it crash the machine
I had to past the last lines: 'TEXT dc.b 5,"HELLO" ' and 'COPY ds.b 6'
to first lines in code so devpac can assemble.
In debuger the first line appear as: 'TEXT movep.l $454C(a0),d2'
and the 2nd line: 'divul.l ??,d0:d0'
and in mode: "run a line at once" the debuger didn´t pass the 2nd line.
Someone knows why it happens?
AlfaRomeo is offline  
Old 06 June 2010, 20:38   #60
pmc
gone
 
pmc's Avatar
 
Join Date: Apr 2007
Location: completely gone
Posts: 1,596
What version of Devpac are you using AlfaRomeo...?

I put this code into Devpac V3.18 and it assembled and ran with no problems.

By the way, code would be shorter like this:

Code:
	lea	TEXT(pc),a0
	move.b	(a0)+,d0
	subq.b	#1,d0
	lea	COPY(pc),a1
loop	move.b	(a0)+,(a1)+
	dbf	d0,loop
	move.b	#0,(a1)
	rts

TEXT	dc.b	5,"HELLO"
COPY	ds.b	6
EDIT: Only just noticed that Bipps had already posted a shortened version earlier... ;-)

Last edited by pmc; 06 June 2010 at 20:50. Reason: Only just noticed...
pmc 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
Downfall - diary of a game... Graham Humphrey Amiga scene 505 15 March 2015 19:26
Uridium 2 : Diary of a Game silkworm Amiga scene 15 09 August 2011 09:00
19 Part One - Boot Camp Retro-Nerd project.aGTW 2 19 February 2008 22:11
Help....what is this part 2? Dizzy Retrogaming General Discussion 7 05 June 2007 15:27

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 18:13.

Top

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