about summary refs log tree commit diff stats
path: root/edit
Commit message (Expand)AuthorAgeFilesLines
...
* 2604 - clearing line was sometimes hiding cursorKartik K. Agaram2016-01-251-1/+1
* 2602Kartik K. Agaram2016-01-241-1/+1
* 2598 - bugfix: scrolling after editKartik K. Agaram2016-01-231-0/+72
* 2595 - bugfix: update state when restoring sandboxesKartik K. Agaram2016-01-231-0/+3
* 2594 - bugfixes: managing state when deletingKartik K. Agaram2016-01-233-93/+306
* 2593 - bugfix: editing a sandbox resets scrollKartik K. Agaram2016-01-221-0/+54
* 2592 - bugfix: sandbox title bar managementKartik K. Agaram2016-01-221-1/+85
* 2591Kartik K. Agaram2016-01-221-1/+7
* 2590 - support scrolling through sandboxesKartik K. Agaram2016-01-227-13/+387
* 2589 - tweak color for sandbox labelsKartik K. Agaram2016-01-221-2/+2
* 2588 - bugfix: show *first* sandbox with errorKartik K. Agaram2016-01-221-1/+55
* 2587 - bugfix: show-screen on warningsKartik K. Agaram2016-01-221-1/+0
* 2586 - show first sandbox with error in statusKartik K. Agaram2016-01-224-17/+69
* 2585 - label sandboxes with a numberKartik K. Agaram2016-01-226-29/+38
* 2580 - check product type of 'maybe-convert'Kartik K. Agaram2016-01-201-10/+10
* 2576 - distinguish allocated addresses from othersKartik K. Agaram2016-01-1911-1257/+1257
* 2558Kartik K. Agaram2016-01-121-2/+5
* 2548 - teach 'print' to print integersKartik K. Agaram2015-12-286-21/+38
* three bugs fixedKartik K. Agaram2015-12-152-0/+65
* rest of edit/ fixedKartik K. Agaram2015-12-154-8/+8
* layer 3 of edit/ now workingKartik K. Agaram2015-12-151-6/+8
* layers 1 and 2 of edit/ now workingKartik K. Agaram2015-12-152-5/+5
* 2480Kartik K. Agaram2015-11-271-2/+2
* 2477Kartik K. Agaram2015-11-271-2/+2
* 2476Kartik K. Agaram2015-11-271-1/+1
* 2474 - overload 'copy' and 'equal' for textKartik K. Agaram2015-11-221-1/+1
* 2471Kartik K. Agaram2015-11-221-4/+0
* 2468 - overload print-character as just 'print'Kartik K. Agaram2015-11-217-31/+31
* 2467 - rename 'string' to 'text' everywhereKartik K. Agaram2015-11-216-24/+24
* 2466 - eliminate ':string' from scenariosKartik K. Agaram2015-11-211-1/+1
* 2464: edit/: update errors in shape-shifting recipesKartik K. Agaram2015-11-191-0/+49
* 2460 - headers for remaining recipesKartik K. Agaram2015-11-181-3/+3
* 2458 - edit/: recipe side free of sandbox errorsKartik K. Agaram2015-11-181-0/+35
* 2451 - drop zoom/maximize featureKartik K. Agaram2015-11-151-125/+0
* 2446 - drop '-duplex' namespacing in recipesKartik K. Agaram2015-11-156-77/+77
* 2442Kartik K. Agaram2015-11-152-4/+2
* 2441 - never miss any specializationsKartik K. Agaram2015-11-152-2/+4
* 2428 - sandbox/ working againKartik K. Agaram2015-11-121-1/+0
* 2422 - a bugfix from the last lessonKartik K. Agaram2015-11-101-1/+59
* 2417 - support mutable ingredients in headersKartik K. Agaram2015-11-105-19/+20
* 2404 - ah, finally a useful assertionKartik K. Agaram2015-11-081-3/+0
* 2397Kartik K. Agaram2015-11-081-1/+1
* 2388 - final layer of edit/ loaded successfullyKartik K. Agaram2015-11-071-37/+36
* 2376Kartik K. Agaram2015-11-051-7/+7
* 2375 - layer 9 doneKartik K. Agaram2015-11-051-6/+5
* 2374 - now edit works until layer 8Kartik K. Agaram2015-11-054-79/+73
* 2371 - layer 5 of editKartik K. Agaram2015-11-054-68/+45
* 2370 - layers 1-4 of edit are backKartik K. Agaram2015-11-054-353/+312
* 2369 - layer 1 of edit is backKartik K. Agaram2015-11-051-27/+16
* 2368 - start getting edit working againKartik K. Agaram2015-11-051-16/+11


































                                         
## compute the factorial of 5, and return the result in the exit code
#
# Uses syntax sugar for:
#   rm32 operands
#
# To run:
#   $ ./translate_subx init.linux 0*.subx apps/factorial.subx -o apps/factorial
#   $ ./bootstrap run apps/factorial
# Expected result:
#   $ echo $?
#   120
#
# You can also run the automated test suite:
#   $ ./bootstrap run apps/factorial test
# Expected output:
#   ........
# Every '.' indicates a passing test. Failing tests get a 'F'.

== code

Entry:  # run tests if necessary, compute `factorial(5)` if not
    # . prologue
    89/<- %ebp 4/r32/esp

    # initialize heap
    # . Heap = new-segment(Heap-size)
    # . . push args
    68/push Heap/imm32
    ff 6/subop/push *Heap-size
    # . . call
    e8/call new-segment/disp32
    # . . discard args
    81 0/subop/add %esp 8/imm32

    # - if argc > 1 and argv[1] == "test", then return run_tests()
    # if (argc <= 1) goto run-main
    81 7/subop/compare *ebp 1/imm32
    7e/jump-if-<= $run-main/disp8
    # if (!kernel-string-equal?(argv[1], "test")) goto run-main
    # . eax = kernel-string-equal?(argv[1], "test")
    # . . push args
    68/push "test"/imm32
    ff 6/subop/push *(ebp+8)
    # . . call
    e8/call kernel-string-equal?/disp32
    # . . discard args
    81 0/subop/add %esp 8/imm32
    # . if (eax == false) goto run-main
    3d/compare-eax-and 0/imm32/false
    74/jump-if-= $run-main/disp8
    # run-tests()
    e8/call run-tests/disp32
    # syscall(exit, *Num-test-failures)
    8b/-> *Num-test-failures 3/r32/ebx
    eb/jump $main:end/disp8
$run-main:
    # - otherwise return factorial(5)
    # eax = factorial(5)
    # . . push args
    68/push 5/imm32
    # . . call
    e8/call factorial/disp32
    # . . discard args
    81 0/subop/add %esp 4/imm32
    # syscall(exit, eax)
    89/<- %ebx 0/r32/eax
$main:end:
    e8/call  syscall_exit/disp32

factorial:  # n: int -> int/eax
    # . prologue
    55/push-ebp
    89/<- %ebp 4/r32/esp
    53/push-ebx
    # if (n <= 1) return 1
    b8/copy-to-eax 1/imm32
    81 7/subop/compare *(ebp+8) 1/imm32
    7e/jump-if-<= $factorial:end/disp8
    # var ebx: int = n-1
    8b/-> *(ebp+8) 3/r32/ebx
    4b/decrement-ebx
    # var eax: int = factorial(n-1)
    # . . push args
    53/push-ebx
    # . . call
    e8/call factorial/disp32
    # . . discard args
    81 0/subop/add %esp 4/imm32
    # return n * factorial(n-1)
    f7 4/subop/multiply-into-eax *(ebp+8)
    # TODO: check for overflow
$factorial:end:
    # . epilogue
    5b/pop-to-ebx
    89/<- %esp 5/r32/ebp
    5d/pop-to-ebp
    c3/return

test-factorial:
    # factorial(5)
    # . . push args
    68/push 5/imm32
    # . . call
    e8/call factorial/disp32
    # . . discard args
    81 0/subop/add %esp 4/imm32
    # check-ints-equal(eax, 120, msg)
    # . . push args
    68/push "F - test-factorial"/imm32
    68/push 0x78/imm32/expected-120
    50/push-eax
    # . . call
    e8/call check-ints-equal/disp32
    # . . discard args
    81 0/subop/add %esp 0xc/imm32
    # end
    c3/return