From b44dcdc91a59a7d4fd725d9bbfead5d6d544a100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kujawa?= Date: Thu, 5 Apr 2018 00:32:53 +0200 Subject: [PATCH] Add example routine finding min among 3 numbers. --- examples/min3.s | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 examples/min3.s diff --git a/examples/min3.s b/examples/min3.s new file mode 100644 index 0000000..0b2a9c2 --- /dev/null +++ b/examples/min3.s @@ -0,0 +1,28 @@ +; min3 +; Takes 3 numbers (A, B, C), passed on stack, finds the minimum. +; Result is also passed on stack. Assumes it is being called via jsr. +.set retl,0x10 +.set reth,0x11 +.set res,0x12 +min3: pla ; pull low byte of return address + sta retl + pla + sta reth + pla ; pull C from stack + sta res ; save C into res + pla ; pull B from stack + cmp res ; compare B and C + bpl bltc ; branch if B > C + sta res ; if C is smaller, save it to res +bltc: pla ; pull A from stack + cmp res ; compare A and whatever is in res + bpl ret ; branch if A > res + sta res ; otherwise save A to res +ret: lda res ; load res into accumulator + pha ; save to the stack + lda reth ; restore return address + pha + lda retl + pha + rts ; return from function +