English Amiga Board


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

 
 
Thread Tools
Old 23 October 2018, 23:28   #1
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 46
Posts: 297
Preservation of registers

I think I read somewhere that d0 & d1 and a0 & a1 can be changed by subroutines, while the rest is supposed to be left unchanged. Is this correct? For example:

Code:
;-- copy chars into textBuffer and zero-terminate it
;in:    a0:     source pointer
;in:    d0:     # characters to copy
copyToBuffer:
    move.l  4.w,a6
    lea     textBuffer,a1
    jsr     _LVOCopyMem(a6)     ;does CopyMem() preserves a1?
    lea     textBuffer,a1       ;if it doesn't: reset it
    add.l   d0,a1
    move.b  #asciiNULL,(a1)
.return:
    rts
If a1 is changed by the CopyMem() call, I'll need to reset it before I can write the zero-terminator. Well, actually, in this case, it would probably already point to the place where the NULL should be written, so I could probably leave out the reset & the add.

But you know what I mean. I'm asking because this doesn't seem to be described in the docs (http://amigadev.elowar.com/read/ADCD.../node01F9.html). So what are the default rules and assumptions for this?
guy lateur is offline  
Old 23 October 2018, 23:37   #2
roondar
Registered User

 
Join Date: Jul 2015
Location: The Netherlands
Posts: 1,474
It’s hidden away a bit, but you can read about the library function register use here: http://amigadev.elowar.com/read/ADCD.../node000F.html

The short version is that Amiga library functions use d0/d1 and a0/a1 and expect the library pointer to be in a6. All of these apart from a6 can end up trashed. Other registers are safe.

Edit: made the above say what I meant

Last edited by roondar; 23 October 2018 at 23:47.
roondar is offline  
Old 23 October 2018, 23:39   #3
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,538
The usual ABI for most 68k operating systems implies that d0, d1, a0, a1 are scratch registers, which are not restored after function calls (Atari TOS also trashes d2 and a2, IIRC). This is true for all functions in AmigaOS, and for all functions generated by Amiga C compilers.

When calling your own functions in assembler you can make your own rules, but generally it is a good idea to always follow the ABI, so you can depend on it everywhere in your code and don't have to check every function you call.
phx is offline  
Old 23 October 2018, 23:41   #4
alkis
Registered User

 
Join Date: Dec 2010
Location: Athens/Greece
Age: 48
Posts: 506
OS-calls treat, as you wrote, d0/d1/a0/a1 as scratch registers. You can't expect their values to be preserved, UNLESS it is specifically stated in the autodocs. (for example look at Results in this one http://amigadev.elowar.com/read/ADCD.../node0339.html)
alkis is offline  
Old 23 October 2018, 23:41   #5
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,538
Quote:
Originally Posted by roondar View Post
All apart from a6 can end up trashed.
Er... no!
d2-d7 and a2-a7 are guaranteed to be preserved by all OS-calls.
phx is offline  
Old 23 October 2018, 23:42   #6
roondar
Registered User

 
Join Date: Jul 2015
Location: The Netherlands
Posts: 1,474
Quote:
Originally Posted by phx View Post
Er... no!
d2-d7 and a2-a7 are guaranteed to be preserved by all OS-calls.
That is what I said, the ‘all’ I referred to was supposed to indicate the four registers I pointed out

Edit: reading it back I do understand why you thought I meant differently. My mind was ahead of me again...
roondar is offline  
Old 23 October 2018, 23:45   #7
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,538
Quote:
Originally Posted by roondar View Post
That is what I said, the ‘all’ I referred to was supposed to indicate the four registers I pointed out
Ah...ok - now as I reread your sentence. But it could easily be misunderstood.
phx is offline  
Old 23 October 2018, 23:48   #8
roondar
Registered User

 
Join Date: Jul 2015
Location: The Netherlands
Posts: 1,474
Quote:
Originally Posted by phx View Post
Ah...ok - now as I reread your sentence. But it could easily be misunderstood.
You are correct, I have edited the post so it’s clear now.
roondar is offline  
Old 23 October 2018, 23:55   #9
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 46
Posts: 297
Quote:
Originally Posted by roondar View Post
You are correct, I have edited the post so it’s clear now.
Thanks for clearing this up, guys!

And while on the subject of clarity: the libray docs state "Amiga library functions use registers D0, D1, A0 and A1 for work space and use register A6 to hold the library base. Do not expect these registers to be the same after calling a function."

At first, I honestly also read that as "Do not expect any of the aforementioned registers..", and therefor "A6 may also be thrashed". But we've established it's only d0/d1 & a0/a1, right?

Thanks for all the feedback, people, much appreciated!
guy lateur is offline  
Old 23 October 2018, 23:59   #10
roondar
Registered User

 
Join Date: Jul 2015
Location: The Netherlands
Posts: 1,474
AFAIK a6 doesn’t change.
roondar is offline  
Old 24 October 2018, 00:11   #11
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 46
Posts: 297
Quote:
Originally Posted by roondar View Post
AFAIK a6 doesn’t change.
That would definitely make the most sense, alright.

I'm not really sure why it is even mentioned in that part of the docs pertaining to preserved registers, to be honest. Unless the library expects you to set its own base adress there? That's not the case though, is it? I mean, I could just as well call the functions using a5 to contain the base, right?
guy lateur is offline  
Old 24 October 2018, 00:28   #12
Don_Adan
Registered User
 
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 51
Posts: 1,184
Quote:
Originally Posted by guy lateur View Post
That would definitely make the most sense, alright.

I'm not really sure why it is even mentioned in that part of the docs pertaining to preserved registers, to be honest. Unless the library expects you to set its own base adress there? That's not the case though, is it? I mean, I could just as well call the functions using a5 to contain the base, right?
For most library calls A6 as base is not necessary, but for some calls is necessary. And your code will be crashed if A5 will be set as library base. Clean code must have always A6 as library base.
Don_Adan is offline  
Old 24 October 2018, 00:36   #13
Galahad/FLT
Going nowhere

Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 46
Posts: 7,400
Quote:
Originally Posted by roondar View Post
AFAIK a6 doesn’t change.
I don't think thats entirely true.

If you open the dos.library and want to then access functions of that library, it expects you to put the dos.library base address into A6.

Obviously it won't do this by itself, its something you have to specifically do, but just to point out there there are some scenarios where A6 can and has to change and you have to remember that if you then call a different library or go back to exec.
Galahad/FLT is offline  
Old 24 October 2018, 00:39   #14
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 46
Posts: 297
Quote:
Originally Posted by Don_Adan View Post
For most library calls A6 as base is not necessary, but for some calls is necessary. And your code will be crashed if A5 will be set as library base. Clean code must have always A6 as library base.
I see, now that's interesting. Although I've always done this, I didn't know it was a requirement in some cases, so thanks for pointing that out! Can you maybe think of an example where this is the case?
guy lateur is offline  
Old 24 October 2018, 00:42   #15
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 46
Posts: 297
Quote:
Originally Posted by Galahad/FLT View Post
I don't think thats entirely true.

If you open the dos.library and want to then access functions of that library, it expects you to put the dos.library base address into A6.

Obviously it won't do this by itself, its something you have to specifically do, but just to point out there there are some scenarios where A6 can and has to change and you have to remember that if you then call a different library or go back to exec.
Really? So you're saying that sometimes:
1. the base needs to be in a6
2. a6 may have changed after the call
That would explain the way it's frased in the docs, though.
guy lateur is offline  
Old 24 October 2018, 01:00   #16
Don_Adan
Registered User
 
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 51
Posts: 1,184
Quote:
Originally Posted by guy lateur View Post
I see, now that's interesting. Although I've always done this, I didn't know it was a requirement in some cases, so thanks for pointing that out! Can you maybe think of an example where this is the case?
A6 is library base, at plus offset is base data. Some calls reads data from library base, if you call library with other register as base, library routine will be reads data not from library data, but from random A6 value. Anyway something like this is safe to use:

move.l A6,A5
jsr -$xx(a5)

but no big sense for me.
Don_Adan is offline  
Old 24 October 2018, 01:03   #17
Don_Adan
Registered User
 
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 51
Posts: 1,184
Quote:
Originally Posted by Galahad/FLT View Post
I don't think thats entirely true.

If you open the dos.library and want to then access functions of that library, it expects you to put the dos.library base address into A6.

Obviously it won't do this by itself, its something you have to specifically do, but just to point out there there are some scenarios where A6 can and has to change and you have to remember that if you then call a different library or go back to exec.
From my attempt to reworking of some rom libraries/devices etc. A6 is always unchanged after all known me library/device calls. Of course often is changed in middle code, but is always returned to original A6 input value.
Don_Adan is offline  
Old 24 October 2018, 01:11   #18
guy lateur
Registered User

guy lateur's Avatar
 
Join Date: May 2017
Location: Belgium
Age: 46
Posts: 297
Quote:
Originally Posted by Don_Adan View Post
A6 is library base, at plus offset is base data. Some calls reads data from library base, if you call library with other register as base, library routine will be reads data not from library data, but from random A6 value. Anyway something like this is safe to use:

move.l A6,A5
jsr -$xx(a5)

but no big sense for me.
Maybe my question wasn't clear. I understand what the problem is, I was just wondering if you happened to know of any specific library call (eg, like OpenDevice() in exec lib) that expects the base to be in a6. Anyway, it's not that important; I will definitely have my bases in a6, from now on. All bases are belong to a6!

Last edited by guy lateur; 24 October 2018 at 01:14. Reason: Added lame bases joke
guy lateur is offline  
Old 24 October 2018, 01:22   #19
PeterK
Registered User
 
Join Date: Apr 2005
Location: Hangover
Posts: 2,578
Quote:
add.l d0,a1
the contents of d0 is unknown here, too. This could cause random memory trashing instead of terminating your text string.
PeterK is offline  
Old 24 October 2018, 01:26   #20
Don_Adan
Registered User
 
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 51
Posts: 1,184
Quote:
Originally Posted by guy lateur View Post
Maybe my question wasn't clear. I understand what the problem is, I was just wondering if you happened to know of any specific library call (eg, like OpenDevice() in exec lib) that expects the base to be in a6. Anyway, it's not that important; I will definitely have my bases in a6, from now on. All bases are belong to a6!
Yes, I know. You can check DOS.lzx from:

http://wt.exotica.org.uk/test.html
Don_Adan 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
OCS collision and clx registers PiCiJi Coders. Asm / Hardware 11 30 July 2019 07:15
A4000 IDE registers mark_k Coders. Asm / Hardware 6 11 May 2015 18:05
Using FPU registers? oRBIT Coders. General 16 26 April 2010 14:34
Need DA8000-DAFFFF registers documentation BlueAchenar Coders. General 2 13 December 2008 16:39
Gayle Hardware Registers bluea support.Hardware 5 09 July 2006 18:07

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 12:55.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Page generated in 0.09249 seconds with 13 queries