Skip to content

Instruction 12 NDUP_ALL

HoverCatz edited this page Feb 21, 2024 · 1 revision

This will DUP (aka duplicate) all 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, N times.

We start by checking the value N. We then POP all elements from the stack. We then push to the top of the stack, the popped objects, 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, \space\color{#947cdd}C\color{white}\space, \space\color{#947cdd}D\color{white}\space]}$

NDUP_ALL < ${\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}A\color{white}\space, \space\color{#947cdd}B\color{white}\space, \space\color{#947cdd}C\color{white}\space, \space\color{#947cdd}D\color{white}\space]}$

Values duplicated: ${[\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]}$


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

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