4 Replies - 493 Views - Last Post: 18 April 2014 - 08:36 AM Rate Topic: -----

#1 stuffthings   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-April 14

Calling conventions for passing arguments to functions

Posted 18 April 2014 - 08:06 AM

I know most calling conventions have specific registers to pass arguments to functions, but what problems would there be if you just stored the value to some memory location and then had the function load that value from memory?
Is This A Good Question/Topic? 0
  • +

Replies To: Calling conventions for passing arguments to functions

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,461
  • Joined: 12-June 08

Re: Calling conventions for passing arguments to functions

Posted 18 April 2014 - 08:10 AM

Isn't that just a basic 'global variable'?
Was This Post Helpful? 0
  • +
  • -

#3 stuffthings   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-April 14

Re: Calling conventions for passing arguments to functions

Posted 18 April 2014 - 08:14 AM

Well yes a global variable would just be able to be read from memory, but I was wondering if there would be any problems, more specifically with regard to separate compilation, if arguments to functions were passed through memory.
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 593
  • View blog
  • Posts: 3,823
  • Joined: 12-January 10

Re: Calling conventions for passing arguments to functions

Posted 18 April 2014 - 08:16 AM

as modi said -- you are speaking of a global variable

class myClass

dim myVariable as string


sub page_load()

callMethods()
end sub

sub method1()
myVariable = 'I like cars'

end sub
sub method2()
myVariable =  ''
end sub

sub callMethods()
method1()
method2()
end sub




quick typed stuff but you get the idea
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: Calling conventions for passing arguments to functions

Posted 18 April 2014 - 08:36 AM

No, you can't implement function calls using a fixed memory address to pass the arguments. I mean you could, but then it would be impossible for functions to be recursive.

However that doesn't mean you can't store the arguments in memory - it just can't be at a fixed address. Usually arguments and local variables are stored using a call stack. How this works is you store a memory address in a register (called the stack pointer) and when you call a function, the stack pointer is incremented by the amount of local memory needed by that function and the function's parameters, local variables and temporary values that can't be stored in registers will be stored in the section of memory between where the stack pointer previously pointed to and where it points to now. Then when you call another (or possibly the same) function from your function, the stack pointer will be incremented again, so none of your locals will be overwritten by the other function's locals. Then when the function returns, the stack pointer just gets decremented again, so the next time a function is called, the returning function's locals are overwritten because you don't need them anymore.

The reason that arguments are passed in registers rather than on the stack where possible is the same reason that we generally store stuff in registers rather than in memory when we have enough free registers: Accessing registers is faster than accessing memory.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1