8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly

Page Last Altered:

Integer Subtraction Routine

Submitted by Steve Fewell

Routine: iminus
Name: Integer Subtraction
Starting Address: &9ECA
Entry criteria: ?&4 and ?&5 [The BASIC stack pointer] points to the first Integer variable. The IWA contains the integer to subtract. X = The next operator code (or 4, if there is no further operation).
Exit: IWA contains the result of [Integer variable - IWA]

Subtracts the IWA from the 32-bit Integer pointed to by the BASIC Stack Pointer (&4 (lo),&5 (hi)) storing the result in the IWA.  The least significant byte [&2A] is subtracted first, followed by &2B, &2C and &2D (The most significant byte). The carry flag allows any overflow to be borrowed forward and updated to the next significant byte of the number. The carry flag is set at the beginning because its reset state (0) indicates that a borrow has occurred.

The routine &9E80 in iplus is jumped to. This routine stores the result in A back to &2D and updates the Stack pointer, so that it now points to the address after the added number. To do this, 4 bytes are added to the address pointed to by (&4, &5) and then jumps to &9E4F to test the value of X [the next operator]. If X contains the operator code for '+' or '-' then send the result to the appropriate routine, for further calculation, otherwise exit the routine.

Disassembly for the integer subtraction routine

9ECA 8 056 38 SEC
9ECB   178 004 B2 04 LDA (&04)
9ECD * 229 042 E5 2A SBC &2A
9ECF * 133 042 85 2A STA &2A
9ED1   160 001 A0 01 LDY#&01
9ED3   177 004 B1 04 LDA (&04),Y
9ED5 + 229 043 E5 2B SBC &2B
9ED7 + 133 043 85 2B STA &2B
9ED9   200 C8 INY
9EDA   177 004 B1 04 LDA (&04),Y
9EDC , 229 044 E5 2C SBC &2C
9EDE , 133 044 85 2C STA &2C
9EE0   200 C8 INY
9EE1   177 004 B1 04 LDA (&04),Y
9EE3 - 229 045 E5 2D SBC &2D
9EE5   128 153 80 99 BRA -103 --> &9E80

 Back to 8BS