boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

013-switch-case.P1pp (1311B)


      1 # tests/p1/switch-case.P1pp -- libp1pp %switch_case dispatch macro.
      2 #
      3 # %switch_case(ctrl, scratch, key, target)
      4 #   if ctrl == key, branch to target. scratch holds the key literal.
      5 #
      6 # A small dispatcher: select(n) returns 100, 200, or 300 for n==1/2/3,
      7 # and 999 for the default. Drive it with three calls and verify.
      8 # Output: "ABC\n".
      9 
     10 %fn(select_n, 0, {
     11     %switch_case(a0, t1, 1, &.case_1)
     12     %switch_case(a0, t1, 2, &.case_2)
     13     %switch_case(a0, t1, 3, &.case_3)
     14     %li(a0, 999)
     15     %b(&.done)
     16     :.case_1 %li(a0, 100) %b(&.done)
     17     :.case_2 %li(a0, 200) %b(&.done)
     18     :.case_3 %li(a0, 300)
     19     :.done
     20 })
     21 
     22 %fn(p1_main, 0, {
     23     %li(a0, 1) %call(&select_n)
     24     %li(t0, 100) %bne(a0, t0, &.fail)
     25     %la(a0, &c_a) %li(a1, 1) %call(&print)
     26 
     27     %li(a0, 2) %call(&select_n)
     28     %li(t0, 200) %bne(a0, t0, &.fail)
     29     %la(a0, &c_b) %li(a1, 1) %call(&print)
     30 
     31     %li(a0, 3) %call(&select_n)
     32     %li(t0, 300) %bne(a0, t0, &.fail)
     33     %la(a0, &c_c) %li(a1, 1) %call(&print)
     34 
     35     %li(a0, 99) %call(&select_n)
     36     %li(t0, 999) %bne(a0, t0, &.fail)
     37 
     38     %la(a0, &c_nl) %li(a1, 1) %call(&print)
     39     %li(a0, 0)
     40     %b(&.done)
     41 
     42     :.fail
     43     %la(a0, &c_x) %li(a1, 1) %call(&print)
     44     %la(a0, &c_nl) %li(a1, 1) %call(&print)
     45     %li(a0, 1)
     46     :.done
     47 })
     48 
     49 :c_a "A"
     50 :c_b "B"
     51 :c_c "C"
     52 :c_x "X"
     53 :c_nl "
     54 "
     55 
     56 :ELF_end