0

I'm trying to build my first ever CHIP-8 emulator from scratch using C. While writing necessary code for the instructions, I came across this opcode:

00EE - RET
Return from a subroutine.

The interpreter sets the program counter to the address at the top of the stack, then subtracts 1 from the stack pointer.

(http://devernay.free.fr/hacks/chip8/C8TECH10.HTM)

I know that a subroutine is basically a function, but what does it mean to 'return' from a subroutine? And what is happening to the program counter, stack, and the stack pointer respectively?

(One additional question): If I created an array that can hold 16 values to represent the stack, will the 'top of the stack' be STACK[0] or STACK[15]? And where should my stack pointer be?

Joshua Jang
  • 107
  • 2
  • 10
  • Additional question should be...well... another question, not appended here. You don't pay to ask questions so please feel free to ask several. – spectras Jul 07 '17 at 07:07
  • It clearly states in the document and text you linked. when a call to a function is made the program counter (to return to) is put on the top of the stack, upon return from the function the address to return to is on the top of the stack. – old_timer Jul 16 '17 at 04:30
  • what does it mean to return from a function in the classic C hello world you call printf from main() printf does stuff then returns to main. same thing here. a function calls another function and upon return you expect it to keep running the code in your function right after that function call... – old_timer Jul 16 '17 at 04:31
  • the documentation you provided as well as the text explains the stack, on a call it increments and on a return it decrements the stack pointer, one would assume it rolls around when it hits 15 and is incremented it goes to 0 but perhaps it was not designed that way. the top of the stack by definition is the address the stack pointer points at (even for downward growing stacks). – old_timer Jul 16 '17 at 04:33
  • Thanks for the reply. When I posted this question I did not have a full, exact understanding of how a stack operates. Looking back at the reference manual, I can now clearly see why the stack is important for continuing the program after the execution of a function. BTW, it also seems like the 'Top boundary where no more data can be pushed' is STACK[15], but is there an official term for explaining this particular boundary? – Joshua Jang Jul 16 '17 at 04:43

1 Answers1

2

To return from a subroutine is to return code execution to the point it was at before the subroutine was called.

Therefore, given that calling a subroutine pushes the current address PC+2 (+2 to jump past the call instruction) onto the stack. Returning from a subroutine will return execution to the address that was pushed to the stack by popping the address from the stack. (e.g. pc=stack[sp]; sp-=2;)

As for the additional question, it really depends on whether you define your stack as being ascending or descending. For the CHIP-8 the choice is not specified.