# (c) 2021 Aaron Taylor <ataylor at subgeniuskitty dot com>
# See LICENSE.txt file for copyright and license details.
# This program performs binary long division, halting when complete.
# Dividend, chosen for alternating bit pattern to make it easily identifiable
# whenever it appears somewhere on the stack.
# Divisor, also chosen for a unique, visually recognizable bit pattern.
# The stack is initialized. Time to run the program.
##########################################################################################
##########################################################################################
# Given a pair of twos-complement signed integers X and Y, generates a sign flag.
# Flag is non-zero if the product of X and Y would be negative, otherwise flag is zero.
##########################################################################################
# Place Return PC at bottom of stack.
##########################################################################################
##########################################################################################
# Returns the additive inverse of a twos-complement operand.
##########################################################################################
##########################################################################################
##########################################################################################
# Returns the absolute value of a twos-complement operand.
##########################################################################################
##########################################################################################
##########################################################################################
# Performs X-Y and returns result on TOS.
##########################################################################################
# Place Return PC at bottom of stack.
##########################################################################################
##########################################################################################
# Division with remainder.
##########################################################################################
# Move Return PC to bottom of stack.
# Generate a sign flag and store it behind the operands.
# Sign flag (nonzero for negative result, 0 for positive result)
# Convert both Dividend and Divisor to absolute value.
# Sign flag (nonzero for negative result, 0 for positive result)
# Prepare stack for division algorithm.
IM_31 # Cycle Counter - Loop from n-1 -> 0 where n = 32 bits.
# Check Cycle Counter for end-of-loop condition (CC = -1).
# While Cycle Counter >= 0
# Left-shift Remainder by 1 bit.
IM_4 # Address of 0x80000000 register
# Set LSB of Remainder equal to bit (Cycle Counter) of Dividend.
# Check if Remainder >= Divisor
# If Remainder >= Divisor
# Set Remainder = Remainder - Divisor
# Set bit (Cycle Counter) of Quotient equal to 1.
# Decrement Cycle Counter
# Loop over next division cycle
# Sign flag (nonzero for negative result, 0 for positive result)
# For now we can only HALT on errors and let the PC inform our debugging.