4 ms·
I think this would implement factorial. I can't remember some of the specific mnemonics, but this is the gist: # entering factorial: argument n is b0
by kinghajj 10y ago
I think this would implement factorial. I can't remember some of the specific mnemonics, but this is the gist:
# entering factorial: argument n is b0
# drop initial accumulator to belt; bail early if n==0
con(1), cmpeq(b1, 0), brtrue(end);
# now, b0 == 1, b1 == n
loop:
# multiply accumulator by n, then decrement n, then check if n stil not 0
mul(b0, b1), sub(b1, 1), cmpeq(b1, 0), brfalse(loop);
end:
# no matter what, the most recent accumulator is b0
ret(b0);
- igodard 10y agoConAsm (model-dependent assembler) for "Silver" model: F("fact") %0; sub(b0 %0, 1) %1; gtrsb(b0 %1, 1) %2, retnfl(b2 %0), inner("fact$1_1", b1 %1, b2 %0); L("fact$1_1") %3 %4; mul(b1 %3, b0 %4) %5; sub(b0 %4, 1) %6; gtrsb(b0 %6, 1) %7, retnfl(b1 %5), br("fact$1_1", b2 %6, b1 %5); Note that integer multiply is specified as 3 cycles in this model, so the recurrence in the loop takes three cycles. Other ops are one cycle. Each semicolon is an instruction (and cycle) boundary, ops separated by commas issue and execute together.
- kinghajj 10y agoAh, of course. I've watched your videos so much, how could I have forgotten that multiply is 3 cycles? Thanks for chiming in!