Archive: Using registers and stacks for input/output


Using registers and stacks for input/output
Guys, I have a fundamental question about using stacks and registers. I will give a simple example.
I have a function that takes no arguments and returns a status of 1 or 0 in $R0
Inside this function, I am going to use
2 registers, $R1, $R2 (So I push them on stack, so that
my caller does not get messed up should he be using the
same registers)
Now I want to return the vaule in $R0 to the caller.
So should I be calling Exch on $R0 first thing in the code. Here is what I think my code should be:

Function MyFunction
Exch $R0
Push $R1
Push $R2
;Use $R1, $R2 etc.
;Now put the value in $R0
StrCpy $R0 "whatever"
Pop $R2 ;In reverse order
Pop $R1
Exch $R0 ; Is that correct?
FunctionEnd

Also, if I call another function MyFunction2, which takes, e.g. a single input,
I could use any of $R1, $R2 by pushing it on stack again ? Does that make sense?


If the return value needs to be in $R0, then just do a StrCpy $R0 "1|0". Otherwise, 1 or 0 just needs to be returned on the top of the stack and you would pop the value after your function call.


...
Call MyFunc
Pop $R0
...
Function MyFunc
Push $R0
Push $R1
Push $R2
; do a lot of stuff and store a correct value in $R0
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

Thanks, goldy1064. I can not do Exch at the beginning of the function since the stack is empty, right? -- That would be error.
But I must do a push since I want to save the original value of the caller.
If I wanted to pass $R2 to another function (which is expecting a single input parameter), I would do a push on $R2 again within MyFunc correct?


That's correct.