idiv:
        stmfd   sp!, {fp, lr}
        add     fp, sp, $4

        mov     r3, r1
        cmp     r3, r0, lsr $1
.Lloop2:
        movls   r3, r3, lsl $1
        cmp     r3, r0, lsr $1
        bls     .Lloop2

        mov     r2, $0

.Lloop3:
        cmp     r0, r3
        subhs   r0, r0, r3
        adc     r2, r2, r2

        mov     r3, r3, lsr $1
        cmp     r3, r1
        bhs     .Lloop3

        ldmfd   sp!, {fp, pc}
.ltorg

_print_character:
        stmfd sp!, {r3, r7, fp, lr}
        add   fp, sp, $4
        mov   r0, r2
        bl    putchar
        mov   r0, $0
        bl    fflush
        ldmfd sp!, {r3, r7, fp, pc}
.ltorg

_read_char:
        stmfd  sp!, {r2, r3, r7, fp, lr}
        add    fp, sp, $4
@ (my_read)
        bl     getchar
        @ returns character in r0
        ldmfd  sp!, {r2, r3, r7, fp, pc}
.ltorg

signed_quotient:
        @ input r0 - numerator
        @ input r1 - denominator
        @ output r0 - quotient
        stmfd  sp!, {r2, r3, fp, lr}
        add    fp, sp, $4
@ (< N 0)
        cmp    r0, $0
        bge    .L2
@ (< D 0)
        cmp    r1, $0
        bge    .L4
@ (set! N (negate N 0))
        mov    r2, $0
        sub    r0, r2, r0
@ (set! D (negate D 0))
        sub    r1, r2, r1
@ (set! Q (quotient N D))
        bl     idiv
        mov    r0, r2
        b      .L5
.L4:
@ (set! N (negate N 0))
        mov    r2, $0
        sub    r0, r2, r0
@ (set! Q (quotient N D))
        bl     idiv
        mov    r0, r2 @ ???? can remove
@ (set! Q (negate Q 0))
        mov    r2, $0
        sub    r0, r2, r0
.L5:
        b      .L3
.L2:
@ (< D 0)
        cmp    r1, $0
        bge    .L6
@ (set! D (negate D 0))
        mov    r2, $0
        sub    r1, r2, r1
@ (set! Q (quotient N D))
        bl     idiv
        mov    r0, r2 @ ???? can remove
@ (set! Q (negate Q 0))
        mov    r2, $0
        sub    r0, r2, r0
        b      .L7
.L6:
@ (set! Q (quotient N D))
        bl     idiv
        mov    r0, r2
.L7:
.L3:
        ldmfd  sp!, {r2, r3, fp, pc}
.ltorg

signed_remainder:
        @ input r0 - numerator
        @ input r1 - denominator
        @ output r0 - remainder
        stmfd  sp!, {r2, r3, fp, lr}
        add    fp, sp, $4
@ (set! R (remainder N D))
        bl     idiv
        ldmfd  sp!, {r2, r3, fp, pc}
.ltorg

