Welcome, Guest. Please login or register.

Author Topic: Couple asm questions (Subr parameter passing, return codes and errors, and register preservation)  (Read 1014 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
3 questions:

1- Why is it that most of the example asm code I've seen uses diferent registers to pass on the parameters to the subroutine, and then the subroutine has to copy the parameters somewhere else where it's suposed to be? This makes less efficient code. Is it purely for simplicity? Why not putting the parameters to their respective places in the first place?

2- Do all CLI progs have to return #0 in D0 when finishing?Maybe programs runs from WB too? If I don't put 0# in D0 at the end the thing (some prog I was runing from shell..) runs but there's a messages saying program failed return code xxx ? In this case it was return code 78, wich is the value the write function returns in D0 (nr. of bytes writen)...  

3- Is register preservation a must? (e.g. move.l a0-a6/do-d7,-(sp) )
Doesn't the OS take care of this automatically when switching between tasks?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline tormedhammaren

  • Full Member
  • ***
  • Join Date: Mar 2003
  • Posts: 153
    • Show only replies by tormedhammaren
Answers:

1. I don't know

2. It's probably the same as return 0; in C. 0 is a RETURN_OK (edited). You have the answer yourself... 78 was stored in your d0, and everything returned to DOS (via d0) except 0 is considered a fail code. So just return 0.

3. All other registers than A0, A1, D0 and D1 must have the same value on program exit as they had on entry. So it's a must to restore these registers (a2-a7 and d2-d7)!
tormedhammaren/toddi ||==
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
1. The reason is that the code is easier to maintain when there is consistency in argument passing. Also, C typically uses d0-> for numeric arguments, and a0-> for pointers, and the same is "natural" for asm too.

Also d0/d1/a0/a1 are scratch registers, so they're "free" when passing arguments (that is: not used by the main app as these registers are trashed by the OScall / subroutine anyway).

2. CLI programs must return the ReturnCode in d0. The result is the success/warn/failure status of the program. 0 means success (RETURN_OK), 5 means warning (RETURN_WARN), 10 means error (RETURN_ERROR), 20 means failure (RETURN_FAIL). dos/dos.i defines these. Workbench ignores the value returned in d0.

3. Register preservation is not needed, except in your own subroutines (so that you don't trash the registers of the main program). How and which registers you save is upto you. OS calls trash d0/d1/a0/a1, unless if otherwise stated in the function AutoDoc.

At program exit a7 (sp) must be the same value as when the program was entered. For CLI programs d0 must be the ReturnCode as explained above.

Quote
Doesn't the OS take care of this automatically when switching between tasks?

Yes it does. Task switching saves all register automagically. What you need to do is to save registers in your own subroutines so you don't trash registers of the main app.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Ahh, yes now it makes sense. I've seen plenty needless register preservation in examples of tutorials without explanation... I guess they just intend to show how it's done.

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline tormedhammaren

  • Full Member
  • ***
  • Join Date: Mar 2003
  • Posts: 153
    • Show only replies by tormedhammaren
Hi

It's good that someone can correct me. I based my third answer on what I found on the subject in the "Amiga Guru Book" which covers the AmigaOS up to ROM 2.04. Is the automatic register preservation a feature of AmigaOS > 2.04?

Regards,
tormedhammaren/toddi ||==
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Quote
AmigaOS up to ROM 2.04. Is the automatic register preservation a feature of AmigaOS > 2.04?

Task switching has always and always will preserve all registers. All AmigaOS versions.

The need not to preserve any registers at program exit (except a7) is separate from the above. It has always been like this in AmigaOS and will continue to be like that.