Skip to content

Instruction 11 NDUP_N

HoverCatz edited this page Feb 21, 2024 · 2 revisions

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

We start by checking the value N2. We then POP the top N1 elements from the stack. We then push to the top of the stack, the popped objects, N2 times.


N1: Number of elements from the stack to duplicate N2: Number of times to duplicate


Example N1: ${\space\color{#947cdd}1\color{white}\space}$ Example N2: ${\space\color{#947cdd}2\color{white}\space}$

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

NDUP_N < ${\space\color{#947cdd}1\color{white}\space}$ >< ${\space\color{#947cdd}2\color{white}\space}$ >

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

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


In the example above, since N1 was 1 and N2 was 2, this happens in order:

  • Stack: [ A, B, C, D ]
  • pop (N1=1):
    • D
  • Stack: [ A, B, C ]
  • push (N2=2):
    • D
    • D
  • Stack: [ A, B, C, D, D ]
Clone this wiki locally