Passing parameters on the stack

When you pass function parameters to the processor stack,

You push the parameters, then JSR pushes the return address onto the stack. Thus, this means that in your function you must take the top element of the stack (return address) before you can take others away)

The return value is stored in a conditional expression in the register D0 .

For example, the following correct way:

 ... |Let's do some addition with a function, MOVE.L #4, -(SP) MOVE.L #5, -(SP) JSR add |the result of the addition (4+5) is in D0 (9) ... add: MOVE.L (SP)+, A1 |store the return address |in a register MOVE.L (SP)+, D0 |get 1st parameter, put in D0 MOVE.L (SP)+, D2 |get 2nd parameter, put in D2 ADD.L D2, D0 |add them, |storing the result in D0 MOVE.L A1, -(SP) |put the address back on the |Stack RTS |return 
+4
source share
3 answers

Not.

The caller (objective function) is usually not responsible for deleting its own arguments. The caller called there, and the one who knows best how to remove them.

And at 68000, it is easy to read using relative offset in the stack, there is no need to physically remove (call) the arguments from the stack. This is due to the problem of "double buffering" the return address pretty well.

So your code should read something like this:

  MOVE.L #4, -(SP) MOVE.L #5, -(SP) JSR add ADDQ.L #8, SP |remove the arguments from the stack, both at once. ... add: MOVE.L 4(SP), D0 |get 1st parameter, put in D0 ADD.L 8(SP), D0 |add the 2nd parameter RTS |return 
+6
source

You do not “pop” parameters from the stack, in the sense that you do not push them. Usually you assign a frame register to indicate the top of the stack at the procedure entry point and gain access to parameters with constant known offsets from the frame pointer. Then your index simply “skips” the return address, which, as you know, is.

eg. in some hypothetical assembly when you are in the procedure. Suppose the stack grows:

 ... argument2 argument1 ret addr <---- stack pointer 

Thus, just argument1 can be obtained at offset sp+4 (assuming 32-bit), argument2 at offset sp+8 , etc. Since these calling conventions are known, these offsets are hard-coded in code and are efficient for computation.

The frame pointer is very useful, since you also push local variables to the stack, and you do not want the indexing of parameters to change in different places, so the frame pointer provides a stable anchor throughout the entire procedure.

+8
source

No, there is no need to push parameters from the stack to look at them; The usual procedure is to use the "frame pointer" registry, as @eli says. In fact, 68k even has an instruction ( LINK ) that is designed to facilitate this: this is one command that (a) saves the previous frame pointer, (b) copies the current stack pointer to the frame pointer and (c) reduces the stack pointer by the specified number to leave room for local variables.

Here is an example of C code and the corresponding assembler 68000 .

+3
source

Source: https://habr.com/ru/post/1306445/


All Articles