commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,143 @@
|
||||
---
|
||||
title: "Rendered views"
|
||||
subtitle: "Programs beside the page, run while the page renders"
|
||||
---
|
||||
|
||||
Every view on this page was made by `quarto render`: the shortcode handed the files beside the page to Programming+'s own engines, ran them, and wrote what its views drew into the page. There is no iframe here and nothing to load. The views print, and they work offline. A program that stops printing what its `EXPECT` lines say fails the render (this site sets `pplus-strict: true`).
|
||||
|
||||
The programs are in the `programs/` folder beside this page, one file per notation: `count-base.pseudo`, `count-base.lingo`, `mean.tex` and the others. Each one starts with the lines the checker reads:
|
||||
|
||||
```pseudo
|
||||
// CALL: COUNT_BASE('A', "GATTACA")
|
||||
// EXPECT: COUNT_BASE returned 3
|
||||
```
|
||||
|
||||
## Run it
|
||||
|
||||
`pplus-run` runs the program and shows what it printed. When the program has `EXPECT` lines, the view checks the output against them.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-run pseudo="programs/count-base.pseudo" code="1" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-run pseudo="programs/count-base.pseudo" code="1" >}}
|
||||
|
||||
A Mojo file runs on the real `mojo` compiler when the machine that renders the page has one (inside `pixi`, the pinned one). Without one, Programming+'s interpreter runs it, and the view says which of the two printed the output.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-run mojo="programs/count-base.mojo" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-run mojo="programs/count-base.mojo" >}}
|
||||
|
||||
## Step through it
|
||||
|
||||
`pplus-steps` puts the listing beside a stepper. Each step shows the line it ran, what it wrote, and the values after it. The run happened at render time, so the page only moves through it: drag the slider, or click into the view and use ← and →.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-steps pseudo="programs/count-base.pseudo" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-steps pseudo="programs/count-base.pseudo" >}}
|
||||
|
||||
The same procedure in plain English. `lingo` runs as written, with the stepper on its own lines:
|
||||
|
||||
{{< pplus-steps lingo="programs/count-base.lingo" t="6" title="count_base, in lingo, parked at step 6" >}}
|
||||
|
||||
Down at the machine, the stepper follows the registers each instruction writes:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-steps asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" max="40" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-steps asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" max="40" >}}
|
||||
|
||||
## Pictures of the run
|
||||
|
||||
```markdown
|
||||
{{{< pplus-table pseudo="programs/count-base.pseudo" >}}}
|
||||
{{{< pplus-flow pseudo="programs/binary-search.pseudo" >}}}
|
||||
{{{< pplus-dataflow pseudo="programs/count-base.pseudo" >}}}
|
||||
```
|
||||
|
||||
The table of values is the one you would draw by hand: a column for each step that changed something, and a row for each name.
|
||||
|
||||
{{< pplus-table pseudo="programs/count-base.pseudo" >}}
|
||||
|
||||
The flowchart is drawn from the program's structure. Each box is shaded by how often its line ran, and each arrow is labeled with how often it was taken.
|
||||
|
||||
{{< pplus-flow pseudo="programs/binary-search.pseudo" caption="CONTAINS_SORTED([2, 5, 8, 13, 21], 13): three passes of the loop." >}}
|
||||
|
||||
The data-flow graph shows which names feed which:
|
||||
|
||||
{{< pplus-dataflow pseudo="programs/count-base.pseudo" >}}
|
||||
|
||||
Every rendered view can also go live. `live="click"` adds a button that swaps in the interactive view, so a reader can edit the program and run it again:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-flow pseudo="programs/count-base.pseudo" live="click" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-flow pseudo="programs/count-base.pseudo" live="click" >}}
|
||||
|
||||
## Mojo values, made and destroyed
|
||||
|
||||
`pplus-lifetimes` gives each value its own row. The row shows when the value is made, copied, moved and destroyed, as Programming+'s interpreter records it. The file's `# expect-stdout:` lines hold what the real compiler printed for it on Compiler Explorer, so the run below is checked against that.
|
||||
|
||||
{{< pplus-run mojo="programs/lifecycle.mojo" >}}
|
||||
|
||||
{{< pplus-lifetimes mojo="programs/lifecycle.mojo" >}}
|
||||
|
||||
## Say what must be true
|
||||
|
||||
`pplus-proof` checks the claims a specification makes: a pseudo program's `ENSURE` and `INVARIANT` lines, or a `.speco` file woven into it.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-proof pseudo="programs/count-base.pseudo" speco="programs/count-base.speco" code="1" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-proof pseudo="programs/count-base.pseudo" speco="programs/count-base.speco" code="1" >}}
|
||||
|
||||
`pplus-tests` shows the fuzzer's report for every routine. It lists the inputs it tried, the failures it found, and each failure shrunk to the smallest input that still fails.
|
||||
|
||||
{{< pplus-tests pseudo="programs/match-off-by-one.pseudo" >}}
|
||||
|
||||
## What the checker says
|
||||
|
||||
`pplus-check` lists what Programming+ finds in each file before anything runs, on the file's own line numbers:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-check pseudo="programs/five-bugs.pseudo" code="1" numbers="1" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-check pseudo="programs/five-bugs.pseudo" code="1" numbers="1" >}}
|
||||
|
||||
## One procedure, every notation
|
||||
|
||||
`pplus-translate` takes one file and writes the procedure in every notation it can be written in. The Mojo is generated from the pseudo and labeled as generated. With one equation:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-translate matho="programs/mean.tex" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-translate matho="programs/mean.tex" >}}
|
||||
|
||||
`layout="grid"` shows the same panes side by side, highlighted the way the app highlights them:
|
||||
|
||||
{{< pplus-translate lingo="programs/count-base.lingo" layout="grid" to="lingo,pseudo,mojo" >}}
|
||||
|
||||
## The machine
|
||||
|
||||
`pplus-machine` runs the x86-64 on the model machine and counts what it cost: instructions, cycles on two kinds of core, cache misses and branches.
|
||||
|
||||
{{< pplus-machine asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" >}}
|
||||
|
||||
## Every view by name
|
||||
|
||||
`pplus-view` takes the view as an option, for templates that choose it:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-view view="table" lingo="programs/count-base.lingo" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-view view="table" lingo="programs/count-base.lingo" >}}
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
title: "Live views and listings"
|
||||
subtitle: "The app in the page, and the programs as text"
|
||||
---
|
||||
|
||||
A live view is the app itself, in an `<iframe>`, with the program carried in its URL. The reader can edit the program, step it and run it again. Their edits stay in their own browser, and **Reset** brings back the page's version. Live views need the network, where rendered views do not (see [Rendered views](index.qmd)).
|
||||
|
||||
## The whole playground
|
||||
|
||||
```markdown
|
||||
{{{< pplus-playground lingo="programs/count-base.lingo" pseudo="programs/count-base.pseudo" panel="flow" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-playground lingo="programs/count-base.lingo" pseudo="programs/count-base.pseudo" panel="flow" >}}
|
||||
|
||||
Arrange it the way the playground's own controls do: the area, the tab it opens on, the build, the dock and the splits.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-playground mojo="programs/count-base.mojo" area="dev" panel="compiler" dock="open" editor="0.6" >}}}
|
||||
```
|
||||
|
||||
## One view of it
|
||||
|
||||
`pplus-embed` shows one view with the code it reads. The views are `trace`, `table`, `flow`, `data`, `life`, `problems`, `proof`, `tests`, `compiler` and `asm`, or the name of a pane to show only that editor.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-embed pseudo="programs/count-base.pseudo" view="trace" t="9" >}}}
|
||||
{{{< pplus-embed pseudo="programs/binary-search.pseudo" view="flow" height="auto" showcall="0" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-embed pseudo="programs/count-base.pseudo" view="trace" t="9" >}}
|
||||
|
||||
The original shortcode, `{{{< pplus >}}}`, still works as it did. It takes the same options as `pplus-embed`, plus `show="listing"` and `show="both"`.
|
||||
|
||||
## Listings
|
||||
|
||||
`pplus-listing` prints the files as code blocks, highlighted by the syntax definitions the extension ships. The header lines (`// CALL:`, `// EXPECT:`) are left out, and a `.tex` file is typeset as equations.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-listing pseudo="programs/count-base.pseudo" numbers="1" >}}}
|
||||
{{{< pplus-listing matho="programs/mean.tex" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-listing pseudo="programs/count-base.pseudo" numbers="1" >}}
|
||||
|
||||
{{< pplus-listing matho="programs/mean.tex" >}}
|
||||
|
||||
`pplus-tabs` gives the files one tab each:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-tabs lingo="programs/count-base.lingo" pseudo="programs/count-base.pseudo" mojo="programs/count-base.mojo" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-tabs lingo="programs/count-base.lingo" pseudo="programs/count-base.pseudo" mojo="programs/count-base.mojo" >}}
|
||||
|
||||
## A link
|
||||
|
||||
```markdown
|
||||
{{{< pplus-link pseudo="programs/count-base.pseudo" text="Open count_base in Programming+" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-link pseudo="programs/count-base.pseudo" text="Open count_base in Programming+" >}}
|
||||
|
||||
## Folded away
|
||||
|
||||
Any shortcode takes `collapse="1"`, which folds it into a block the reader opens:
|
||||
|
||||
{{< pplus-steps pseudo="programs/binary-search.pseudo" collapse="1" title="Show the binary search, step by step" >}}
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: "The machine"
|
||||
subtitle: "x86-64 on a model machine, every step drawn with the page"
|
||||
---
|
||||
|
||||
These shortcodes show what the Hardware area's board shows: the registers, memory, the caches, the pipeline and the core, running an x86-64 file beside the page. While Quarto renders, the extension runs the program on the model machine and draws each view at every step of the run. The page carries those drawings, compressed, and the stepper moves through them. Nothing runs in the reader's browser, and the page opens on the step you choose, so it prints too.
|
||||
|
||||
The program is `programs/two-pointers.asm`: it counts the values two sorted lists have in common. Its registers and memory are set by `programs/two-pointers.setup`:
|
||||
|
||||
```
|
||||
rdi = 0x1000 rsi = 6 rdx = 0x2000 rcx = 7
|
||||
memory 0x1000 bytes 1 0 0 0 0 0 0 0 4 0 ...
|
||||
```
|
||||
|
||||
## The board
|
||||
|
||||
`pplus-hw` puts the listing beside a set of cards. With no options, it uses the board's first arrangement: registers, flags, address arithmetic, memory and the stack.
|
||||
|
||||
```markdown
|
||||
{{{< pplus-hw asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-hw asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" >}}
|
||||
|
||||
`preset=` picks one of the board's other arrangements: `caches`, `pipeline` or `chip`. `views=` names the cards yourself, in order, and `t=` is the step the page opens on:
|
||||
|
||||
```markdown
|
||||
{{{< pplus-hw asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" preset="caches" t="12" >}}}
|
||||
{{{< pplus-hw asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" views="explain,registers,encoding" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-hw asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" preset="caches" t="12" title="The caches, at step 12" >}}
|
||||
|
||||
## One view at a time
|
||||
|
||||
Each card of the board is also a shortcode of its own, `pplus-hw-<view>`:
|
||||
|
||||
| shortcode | shows |
|
||||
|---|---|
|
||||
| `pplus-hw-code` | the machine code: each instruction with its address and bytes |
|
||||
| `pplus-hw-explain` | the last instruction in words: what it read, computed and wrote |
|
||||
| `pplus-hw-registers`, `pplus-hw-flags` | the registers the program touches, and ZF, SF, CF, OF, PF |
|
||||
| `pplus-hw-address` | base + index × scale + displacement, part by part |
|
||||
| `pplus-hw-memory`, `pplus-hw-stack` | memory byte by byte, and the stack's slots |
|
||||
| `pplus-hw-dataflow`, `pplus-hw-alu` | where the last instruction's values came from, and the ALU at bit level |
|
||||
| `pplus-hw-branches`, `pplus-hw-vectors` | each branch and the predictor's guess, and the xmm registers lane by lane |
|
||||
| `pplus-hw-lines`, `pplus-hw-cache`, `pplus-hw-hierarchy` | the cache line each access touched, the L1 data cache, and the path through L1, L2, L3 and memory |
|
||||
| `pplus-hw-paging` | the page walk and the TLB (it switches virtual memory on) |
|
||||
| `pplus-hw-pipeline`, `pplus-hw-uops`, `pplus-hw-timeline`, `pplus-hw-bottleneck` | the five-stage pipeline, micro-ops and ports, the out-of-order timeline, and what limits the speed |
|
||||
| `pplus-hw-counters` | the run as totals, in the style of `perf stat` |
|
||||
| `pplus-hw-chip`, `pplus-hw-core`, `pplus-hw-encoding` | the whole core lit up, inside the out-of-order core, and the instruction's bytes field by field |
|
||||
|
||||
```markdown
|
||||
{{{< pplus-hw-pipeline asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" listing="0" max="40" >}}}
|
||||
```
|
||||
|
||||
{{< pplus-hw-pipeline asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" listing="0" max="40" >}}
|
||||
|
||||
The whole core, with the parts the last instruction used lit up:
|
||||
|
||||
{{< pplus-hw-chip asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" t="9" >}}
|
||||
|
||||
Virtual memory: the page walk and the TLB. `pplus-hw-paging` turns virtual memory on for the run.
|
||||
|
||||
{{< pplus-hw-paging asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" t="10" >}}
|
||||
|
||||
`stepper="0"` draws one step and nothing else, which suits a figure in a printed handout:
|
||||
|
||||
{{< pplus-hw-encoding asm="programs/two-pointers.asm" setup="programs/two-pointers.setup" t="9" stepper="0" listing="0" caption="Step 9, mov r11, qword ptr [rdx + r9*8], byte by byte." >}}
|
||||
|
||||
## From Mojo to the machine
|
||||
|
||||
`pixi run quarto-export demo <folder>` writes a Mojo program's x86-64, from the real compiler's listing, beside a page, along with a setup for its arguments. Here it is the `cell` function of the demo's Smith-Waterman alignment (`programs/cell.asm`): the best of three scores, with three branch-free `cmovg`s. Each instruction's comment names the Mojo line it came from.
|
||||
|
||||
{{< pplus-hw asm="programs/cell.asm" setup="programs/cell.setup" views="explain,registers,flags" >}}
|
||||
|
||||
{{< pplus-hw-counters asm="programs/cell.asm" setup="programs/cell.setup" listing="0" stepper="0" >}}
|
||||
@@ -0,0 +1,24 @@
|
||||
// CALL: CONTAINS_SORTED([2, 5, 8, 13, 21], 13)
|
||||
// EXPECT: CONTAINS_SORTED returned TRUE
|
||||
// Is X in the sorted list L? Halve the range that could hold it each time.
|
||||
FUNCTION CONTAINS_SORTED(L : LIST OF INTEGER, X : INTEGER) RETURNS BOOL
|
||||
REQUIRE FORALL K IN [0, LENGTH(L) - 1) : L[K] <= L[K + 1]
|
||||
DECLARE Lo : INTEGER
|
||||
DECLARE Hi : INTEGER
|
||||
DECLARE Mid : INTEGER
|
||||
Lo <- 0
|
||||
Hi <- LENGTH(L)
|
||||
WHILE Lo < Hi
|
||||
INVARIANT 0 <= Lo AND Lo <= Hi AND Hi <= LENGTH(L)
|
||||
DECREASES Hi - Lo
|
||||
Mid <- (Lo + Hi) DIV 2
|
||||
IF L[Mid] == X THEN
|
||||
RETURN TRUE
|
||||
ELSEIF L[Mid] < X THEN
|
||||
Lo <- Mid + 1
|
||||
ELSE
|
||||
Hi <- Mid
|
||||
ENDIF
|
||||
ENDWHILE
|
||||
RETURN FALSE
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,9 @@
|
||||
cell: ; cell(Int, Int, Int)
|
||||
xor eax, eax ; mojo line 9
|
||||
cmp rdi, 0 ; mojo line 9
|
||||
cmovg rax, rdi ; mojo line 9
|
||||
cmp rsi, rax ; mojo line 11
|
||||
cmovg rax, rsi ; mojo line 11
|
||||
cmp rdx, rax ; mojo line 13
|
||||
cmovg rax, rdx ; mojo line 13
|
||||
ret ; mojo line 15
|
||||
@@ -0,0 +1,5 @@
|
||||
# cell(Int, Int, Int): the arguments arrive in rdi, rsi, rdx (System V)
|
||||
# the diagonal 4, a gap in the reference 9, a gap in the read -2: the cell keeps 9
|
||||
rdi = 4
|
||||
rsi = 9
|
||||
rdx = -2
|
||||
@@ -0,0 +1,7 @@
|
||||
// CALL: COUNT_BASES('A', "GATTACA")
|
||||
// EXPECT: COUNT_BASES returned 3
|
||||
To count the B bases in T:
|
||||
Start with Count at 0.
|
||||
For each letter c of T:
|
||||
If c is B, add 1 to Count.
|
||||
The answer is Count.
|
||||
@@ -0,0 +1,14 @@
|
||||
# origin: Programming+ exercise count-base (was translations.mojo, by course staff)
|
||||
# status: unverified; Stage 1 verifies it on the pinned compiler
|
||||
# probe: does the course's hand-written count_base compile and agree with the pseudo?
|
||||
# expect-stdout: 3
|
||||
def count_base(b: String, t: String) -> Int:
|
||||
var total = 0
|
||||
for i in range(t.count_codepoints()):
|
||||
if String(t[codepoint=i]) == b:
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_base("A", "GATTACA"))
|
||||
@@ -0,0 +1,26 @@
|
||||
// CALL: COUNT_BASE('A', "GATTACA")
|
||||
// EXPECT: COUNT_BASE returned 3
|
||||
// How many positions of T hold the base B.
|
||||
FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER
|
||||
|
||||
// Set up
|
||||
DECLARE N : INTEGER
|
||||
DECLARE Total : INTEGER
|
||||
N <- LENGTH(T)
|
||||
Total <- 0
|
||||
|
||||
// Visit each position
|
||||
FOR I <- 0 TO N
|
||||
|
||||
// Test this position
|
||||
IF T[I] == B THEN
|
||||
|
||||
// Update the running total
|
||||
Total <- Total + 1
|
||||
|
||||
ENDIF
|
||||
NEXT I
|
||||
|
||||
// Report
|
||||
RETURN Total
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,4 @@
|
||||
SPEC COUNT_BASE(B, T)
|
||||
ENSURE result = |{ K ∈ [0, |T|) : T[K] = B }|
|
||||
LOOP FOR I
|
||||
INVARIANT Total = |{ K ∈ [0, I) : T[K] = B }|
|
||||
@@ -0,0 +1,15 @@
|
||||
// CALL: COUNT_GC("GATTACA")
|
||||
// ERRORS: 2
|
||||
// Count the G and C bases in T. This version has five mistakes;
|
||||
// the coach finds all of them before anything runs.
|
||||
FUNCTION COUNT_GC(T : STRING) RETURNS INTEGER
|
||||
DECLARE Total : INTEGER
|
||||
DECLARE Unused : FLOAT
|
||||
FOR I <- 0 TO LENGTH(T) - 1
|
||||
Total <- 0
|
||||
IF T[I] == "G" OR T[I] == 'C' THEN
|
||||
Total = Total + 1
|
||||
ENDIF
|
||||
NEXT I
|
||||
RETURN Total
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,38 @@
|
||||
# probe: copy and move constructors through var arguments and transfers
|
||||
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
||||
# expect-stdout: new a
|
||||
# expect-stdout: copy a
|
||||
# expect-stdout: consumed a+
|
||||
# expect-stdout: free a+
|
||||
# expect-stdout: move a
|
||||
# expect-stdout: holding a
|
||||
# expect-stdout: free a
|
||||
struct Box(Copyable, Movable):
|
||||
var label: String
|
||||
|
||||
def __init__(out self, label: String):
|
||||
self.label = label
|
||||
print("new", label)
|
||||
|
||||
def __init__(out self, *, copy: Self):
|
||||
self.label = copy.label + "+"
|
||||
print("copy", copy.label)
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.label = move.label^
|
||||
print("move", self.label)
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print("free", self.label)
|
||||
|
||||
|
||||
def consume(var b: Box):
|
||||
print("consumed", b.label)
|
||||
|
||||
|
||||
def main():
|
||||
var a = Box("a")
|
||||
var b = a.copy()
|
||||
consume(b^)
|
||||
var c = a^
|
||||
print("holding", c.label)
|
||||
@@ -0,0 +1,33 @@
|
||||
// CALL: MATCH("ATT", "GATTACA")
|
||||
// STOPS: index
|
||||
// One bound is wrong. Every position where pattern P occurs exactly in text T.
|
||||
FUNCTION MATCH(P : STRING, T : STRING) RETURNS LIST OF INTEGER
|
||||
|
||||
// Set up
|
||||
DECLARE N : INTEGER
|
||||
DECLARE M : INTEGER
|
||||
DECLARE Matched : BOOL
|
||||
DECLARE Answer : LIST OF INTEGER
|
||||
N <- LENGTH(T)
|
||||
M <- LENGTH(P)
|
||||
|
||||
// Visit each legal start
|
||||
FOR I <- 0 TO N
|
||||
|
||||
// Test: does the pattern match here?
|
||||
Matched <- TRUE
|
||||
FOR J <- 0 TO M
|
||||
IF T[I + J] != P[J] THEN
|
||||
Matched <- FALSE
|
||||
BREAK
|
||||
ENDIF
|
||||
NEXT J
|
||||
|
||||
// Update
|
||||
IF Matched THEN
|
||||
APPEND(Answer, I)
|
||||
ENDIF
|
||||
NEXT I
|
||||
|
||||
RETURN Answer
|
||||
ENDFUNCTION
|
||||
@@ -0,0 +1,3 @@
|
||||
% CALL: X_BAR(3, [1.0, 2.0, 6.0])
|
||||
% EXPECT: X_BAR returned 3.0
|
||||
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
|
||||
@@ -0,0 +1,27 @@
|
||||
; rdi = list A rsi = |A| rdx = list B rcx = |B|
|
||||
; rax = how many values appear in both
|
||||
xor eax, eax
|
||||
xor r8, r8
|
||||
xor r9, r9
|
||||
.step:
|
||||
cmp r8, rsi
|
||||
jae .done
|
||||
cmp r9, rcx
|
||||
jae .done
|
||||
mov r10, qword ptr [rdi + r8*8]
|
||||
mov r11, qword ptr [rdx + r9*8]
|
||||
cmp r10, r11
|
||||
je .both
|
||||
jb .advanceA
|
||||
add r9, 1 ; the second list is behind
|
||||
jmp .step
|
||||
.advanceA:
|
||||
add r8, 1 ; the first list is behind
|
||||
jmp .step
|
||||
.both:
|
||||
add rax, 1
|
||||
add r8, 1
|
||||
add r9, 1
|
||||
jmp .step
|
||||
.done:
|
||||
ret
|
||||
@@ -0,0 +1,6 @@
|
||||
rdi = 0x1000
|
||||
rsi = 6
|
||||
rdx = 0x2000
|
||||
rcx = 7
|
||||
memory 0x1000 bytes 1 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0
|
||||
memory 0x2000 bytes 2 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 31 0 0 0 0 0 0 0
|
||||
Reference in new issue
Block a user