//    Copyright (C) 2015   Martin Dames <martin@bastionbytes.de>
//  
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along
//    with this program; if not, write to the Free Software Foundation, Inc.,
//    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// optimized multiplication (using only "add" and "sub")
//
params=r:$a,rv:$b
work=$h,$xc,$xb,$f,$c
---
// xb = b;
set $xb,$b

// if(a > xb) {
cmp $a,$xb
jbe noswap

//     h = a;
set $h,$a

//     a = xb;
set $a,$xb

//     xb = h;
set $xb,$h

// }
:noswap

// xc = 0;
set $xc,0

// while(a > 0) {
:loop1_start
cmp $a,0
jbe loop1_ende

//     c = xb;
set $c,$xb

//     f = 1;
set $f,1

//     h = f;
set $h,$f

//     h += f;
add $h,$f

//     while(h < a) {
:loop2_start
cmp $h,$a
jge loop2_ende

//         f = h;
set $f,$h

//         h = c;
set $h,$c

//         c += h;
add $c,$h

//     }
jmp loop2_start
:loop2_ende

//     a -= f;
sub $a,$f

//     xc += c;
add $xc,$c

// }
jmp loop1_start
:loop1_ende

// a = xc;
set $a,$xc

