Skip to content

Instruction 8 NDUP

HoverCatz edited this page Feb 21, 2024 · 4 revisions

This will DUP (aka duplicate) the last element (the top/the right-most/the last added element) on the stack, and add a copy of it to the top of the stack, N times.

We start by checking the value N. We then POP the top element alone from the stack. We then push to the top of the stack, the popped object, N times.


Example N: ${\space\color{#947cdd}2\color{white}\space}$

Stack before: ${[\space\color{#947cdd}A\space\color{white}, \space\color{#947cdd}B\color{white}\space]}$

NDUP

Stack after: ${[\space\color{#947cdd}A\space\color{white}, \space\color{#947cdd}B\color{white}\space, \space\color{#947cdd}B\color{white}\space]}$

Value duplicated: ${[\space\color{#947cdd}B\color{white}\space]}$


In the example above, since N was 2, this happens in order:

  • Stack: [ A, B ]
  • pop() = B
  • Stack: [ A ]
  • push (N=2):
    • B
    • B
  • Stack: [ A, B, B ]
Clone this wiki locally