L
L
lutokris2021-03-13 18:54:36
Microcontrollers
lutokris, 2021-03-13 18:54:36

Is it really possible to read from the stack inside a subroutine?

All good. I read a book on AVR, taking notes on the quiet. So I got to the description of the stack pointer. There is such a quote - "If you write data to the stack and go to the subroutine, then it will be impossible to read this data in the body of the subroutine. Because when the subroutine is called, the current address from the program counter is automatically written to the stack and when we try to read from the subroutine inside the stack, we will extract this address and then we will not be able to exit the subroutine".

But after all I can stupidly so make in the subroutine:
push [A]
push [B] ; push two 8-bit numbers onto the stack

call Spat ; the address of the call to this procedure entered the stack last
call IdiSrat

Spat:
pop dx ; store address in dx register
pop ax ; extract variable A
pop bx ; extract variable B
add ax, bx ; oh yes, the procedure adds two 8-bit numbers from the stack))
push dx ; again push the address onto the stack
ret ; we return to the address and go to sleep

Is it possible to do this, or was it in the book that it was simply "wrong" to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-03-13
@freeExec

Why use popwhen you can just read from the stack with an offset pointing to the desired argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question