#### 16 bit Subtraction

Craig Ledbetter

Has anyone got a routine that does two byte (16 bit) integer subtraction? Unsigned.
I can do 8bit subtractions easily.
But I need to subtract two sets of two bytes, and I just can’t make my routine work in all situations.
And it would be cool to actually know how t implement signed match functions.
I am watching YouTube explain the process, but when I try and implement it on a machine code level, I am missing some things.
Thanks for any help.

Craig Ledbetter

bill rowe

craig: my 16 bit math routines are in macros but maybe you can get the idea.  for a 16 bit subtract of reg 9 from reg 10 with the result in reg 10 it would be  "alu2 R10,R9,R10,sm,smb"

alu2: macro tgt,src1,src2,op1,op2 ;2 byte register/register alu operation
glo src2
str sp
glo src1
op1 ;calculate the low order byte
plo tgt
ghi src2
str sp
ghi src1
op2 ;calculate the high byte
phi tgt
endm

what do you mean by match? compare?

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of Craig Ledbetter <craig1611@...>
Sent: July 18, 2020 8:05 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: [cosmacelf] 16 bit Subtraction

Has anyone got a routine that does two byte (16 bit) integer subtraction? Unsigned.
I can do 8bit subtractions easily.
But I need to subtract two sets of two bytes, and I just can’t make my routine work in all situations.
And it would be cool to actually know how t implement signed match functions.
I am watching YouTube explain the process, but when I try and implement it on a machine code level, I am missing some things.
Thanks for any help.

Craig Ledbetter

--
Bill Rowe
Olduino - an arduino for the first of us

Craig Ledbetter

Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with all numbers. Borrowing seems too complicated for some reason. I am having to do base 16 additions across the bytes for the carries, and when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I am missing.

On 18 Jul 2020, at 14:52, bill rowe <bill_rowe_ottawa@...> wrote:

craig: my 16 bit math routines are in macros but maybe you can get the idea.  for a 16 bit subtract of reg 9 from reg 10 with the result in reg 10 it would be  "alu2 R10,R9,R10,sm,smb"

alu2: macro tgt,src1,src2,op1,op2 ;2 byte register/register alu operation
glo src2
str sp
glo src1
op1 ;calculate the low order byte
plo tgt
ghi src2
str sp
ghi src1
op2 ;calculate the high byte
phi tgt
endm

what do you mean by match? compare?

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of Craig Ledbetter <craig1611@...>
Sent: July 18, 2020 8:05 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: [cosmacelf] 16 bit Subtraction

Has anyone got a routine that does two byte (16 bit) integer subtraction? Unsigned.
I can do 8bit subtractions easily.
But I need to subtract two sets of two bytes, and I just can’t make my routine work in all situations.
And it would be cool to actually know how t implement signed match functions.
I am watching YouTube explain the process, but when I try and implement it on a machine code level, I am missing some things.
Thanks for any help.

Craig Ledbetter

--
Bill Rowe
Olduino - an arduino for the first of us

bill rowe

craig: these will always work.  just possibly you have a different mental model of what the binary result should be.

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of Craig Ledbetter <craig1611@...>
Sent: July 18, 2020 9:59 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: Re: [cosmacelf] 16 bit Subtraction

Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with all numbers. Borrowing seems too complicated for some reason. I am having to do base 16 additions across the bytes for the carries, and when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I am missing.

On 18 Jul 2020, at 14:52, bill rowe <bill_rowe_ottawa@...> wrote:

craig: my 16 bit math routines are in macros but maybe you can get the idea.  for a 16 bit subtract of reg 9 from reg 10 with the result in reg 10 it would be  "alu2 R10,R9,R10,sm,smb"

alu2: macro tgt,src1,src2,op1,op2 ;2 byte register/register alu operation
glo src2
str sp
glo src1
op1 ;calculate the low order byte
plo tgt
ghi src2
str sp
ghi src1
op2 ;calculate the high byte
phi tgt
endm

what do you mean by match? compare?

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of Craig Ledbetter <craig1611@...>
Sent: July 18, 2020 8:05 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: [cosmacelf] 16 bit Subtraction

Has anyone got a routine that does two byte (16 bit) integer subtraction? Unsigned.
I can do 8bit subtractions easily.
But I need to subtract two sets of two bytes, and I just can’t make my routine work in all situations.
And it would be cool to actually know how t implement signed match functions.
I am watching YouTube explain the process, but when I try and implement it on a machine code level, I am missing some things.
Thanks for any help.

Craig Ledbetter

--
Bill Rowe
Olduino - an arduino for the first of us

--
Bill Rowe
Olduino - an arduino for the first of us

David Schultz

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.
Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

--
(Web pages available only at the Wayback Machine because Earthlink
terminated that service.)

bill rowe

and I had a h*ll of a time understanding the whole DF and twos complement math business - I tore my hair out over signed comparisons.

As david says, post some examples and somebody can help.

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of David Schultz <david.schultz@...>
Sent: July 18, 2020 10:12 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: Re: [cosmacelf] 16 bit Subtraction

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
> Thanks Bill.
> The word ‘match” should have been MATH. Signed math functuons.
> Your macro looks very similar to my routine, but it does not work with
> all numbers. Borrowing seems too complicated for some reason. I am
> having to do base 16 additions across the bytes for the carries, and
> when I get negative numbers, they are not correct. Grrr.
> I think I am over-thinking this, and hope there is a basic step that I
> am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

--
(Web pages available only at the Wayback Machine because Earthlink
terminated that service.)

--
Bill Rowe
Olduino - an arduino for the first of us

Lee Hart

Craig Ledbetter wrote:
Has anyone got a routine that does two byte (16 bit) integer subtraction? Unsigned.
I can do 8bit subtractions easily.
But I need to subtract two sets of two bytes, and I just can’t make my routine work in all situations.
And it would be cool to actually know how t implement signed match functions.
I am watching YouTube explain the process, but when I try and implement it on a machine code level, I am missing some things.
Thanks for any help.
There's one in Tiny BASIC, and one I wrote in 8TH (a FORTH variant). Source for both are in the cosmacelf files section.

A big part of the problem is deciding on the format. The way the 1802 instruction set uses the DF flag makes 3's complement easy.

Lee Hart

--
If happiness is on your mind, here's a daily list to find:
- something to do
- something to look forward to
- someone to love
- someone to take good care of
- and misbehave, just a little
--
Lee Hart, 814 8th Ave N, Sartell MN 56377, www.sunrise-ev.com

gregory simmons

Lee, 3's complement is a new one for me.

On Saturday, July 18, 2020, 12:35:35 PM EDT, Lee Hart <leeahart@...> wrote:

Craig Ledbetter wrote:
> Has anyone got a routine that does two byte (16 bit) integer subtraction? Unsigned.
> I can do 8bit subtractions easily.
> But I need to subtract two sets of two bytes, and I just can’t make my routine work in all situations.
> And it would be cool to actually know how t implement signed match functions.
> I am watching YouTube explain the process, but when I try and implement it on a machine code level, I am missing some things.
> Thanks for any help.

There's one in Tiny BASIC, and one I wrote in 8TH (a FORTH variant).
Source for both are in the cosmacelf files section.

A big part of the problem is deciding on the format. The way the 1802
instruction set uses the DF flag makes 3's complement easy.

Lee Hart

--
If happiness is on your mind, here's a daily list to find:
- something to do
- something to look forward to
- someone to love
- someone to take good care of
- and misbehave, just a little
--
Lee Hart, 814 8th Ave N, Sartell MN 56377, www.sunrise-ev.com

I found 2's Complement to make sense when I applied it to digital counters like the 74191.
This 4 bit counter, when counting down from 0000 goes to 1111.  So if counting down from 0 is -1, then 1111 is -1 in 4 bit signed nibble.  Later, in learning electronics, there are Adder Circuits that will add binary numbers, but afaik, there are no subtracter circuits.  I believe all subtraction in electronics is done by 2's complementing the 2nd number.  And if memory serves me, it does not matter if the numbers you are using are signed or unsigned, this will always work the same, however, if the result can't be stored in the width of bits available, you will have an over(under)flow error.

As an example, if you wish to calculate 240 - 10

00001010 = 10
11110101 = 1's complement of 10 (just invert)
11110110 = 2's complement of 10 (just add 1 more)

11110000 = 240
11110110   = (-10)
11100110   = 230   (ignore and drop the carry)

Now, if the number was a signed -16 the results would look the same... but you'd just interpret the result as -26

11110000 = -16
11110110   = (-10)
11100110   = -26   (ignore and drop the carry)

Just my 10b cents,
Josh

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of bill rowe <bill_rowe_ottawa@...>
Sent: Saturday, July 18, 2020 11:18 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: Re: [cosmacelf] 16 bit Subtraction

and I had a h*ll of a time understanding the whole DF and twos complement math business - I tore my hair out over signed comparisons.

As david says, post some examples and somebody can help.

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of David Schultz <david.schultz@...>
Sent: July 18, 2020 10:12 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: Re: [cosmacelf] 16 bit Subtraction

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
> Thanks Bill.
> The word ‘match” should have been MATH. Signed math functuons.
> Your macro looks very similar to my routine, but it does not work with
> all numbers. Borrowing seems too complicated for some reason. I am
> having to do base 16 additions across the bytes for the carries, and
> when I get negative numbers, they are not correct. Grrr.
> I think I am over-thinking this, and hope there is a basic step that I
> am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

--
(Web pages available only at the Wayback Machine because Earthlink
terminated that service.)

--
Bill Rowe
Olduino - an arduino for the first of us

Lee Hart

gregory simmons via groups.io wrote:
Lee, 3's complement is a new one for me.
Oops! That should be 2's complement. Must be inflation...

For fun, there are lots of strange/interesting number formats that are useful in certain situations. Like 2's complement for binary math, or hex (base 16) for better human readability.

But take a look at number systems with a negative base, where negative numbers "disappear". For example, base -2, where the digits represent +1, -2, +4, -8 etc. In this representation, there are no special requirements for adding signed numbers; it happens automatically.

Or base 2i (where i is the imaginary number square root of -1). This number system does vector math with normal +-/* operations.

On one project, I used base2 add/subtract/log/antilog as the basic operators. With logs, muliplication and division are as simple as add and subtract.

The advantage of alternate number systems is that the CPU thinks youj're still doing simple addition and subtraction; but the way you interpret the numbers lets you handle negative or multi-dimensional numbers easier.

--
If happiness is on your mind, here's a daily list to find:
- something to do
- something to look forward to
- someone to love
- someone to take good care of
- and misbehave, just a little
--
Lee Hart, 814 8th Ave N, Sartell MN 56377, www.sunrise-ev.com

Craig Ledbetter

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.
Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

--
(Web pages available only at the Wayback Machine because Earthlink
terminated that service.)

Hmm,  I don't know how common it is to talk about negative hex numbers.  ie -A.
Personally, I don't see much purpose in it, because we live in a decimal world.
I mean to say, HEX is great for many things, but not for quantifying a number for people.

I wonder if I got pulled over for speeding if I could use the excuse that I thought the posted limit was in HEX?
Or would they take me away in handcuffs?

But, you are correct to say F5 is meaningless until you subtract it from 00 to see the binary weighted value of B.
0x10 = 16,  16 - 5 = 11, 11 = 0xB
I like to put the hex digits down as fingers (since my first ELF used 8 push buttons for Data)

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of Craig Ledbetter <craig1611@...>
Sent: Sunday, July 19, 2020 10:45 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: Re: [cosmacelf] 16 bit Subtraction

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
> Thanks Bill.
> The word ‘match” should have been MATH. Signed math functuons.
> Your macro looks very similar to my routine, but it does not work with
> all numbers. Borrowing seems too complicated for some reason. I am
> having to do base 16 additions across the bytes for the carries, and
> when I get negative numbers, they are not correct. Grrr.
> I think I am over-thinking this, and hope there is a basic step that I
> am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

--
(Web pages available only at the Wayback Machine because Earthlink
terminated that service.)

I don't know it's there is confusion in notation here or what. Hex 10 from hex 05 is in fact hex F5 (which is two's complement notation for hex -0B).

Even with possible confusion over two's complement I can't think of any scenario in which the answer would be -A.

In decimal, 5 minus 16 is -11, in hex 5 minus 10  is -B. To convert from two's complement, invert all the bits giving F4 then add one giving F5.

David

On July 19, 2020 10:45:35 AM Craig Ledbetter <craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

--
(Web pages available only at the Wayback Machine because Earthlink
terminated that service.)

Craig Ledbetter

Well,
If I wanted to then multiply the answer from 05-10 (hex), would I use the answer F5 or 0A?
Now you might start to see my confusion?

And below are the actual numbers I am working with, and the answers I checked…
IT gave me a -A as an answer!
So am still confused. On 19 Jul 2020, at 15:45, Craig Ledbetter <craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

Jeff Truck

Craig,

For what it is worth, I've uploaded the library I use to perform 16 bit math.   You will find it in  Files / Jeff Truck / Code Libraries.

Some may find it is overkill but my need to code things in a structured format makes it easier for me to figure out what I did wrong and what I did in the past.

Jeff

gregory simmons

Craig, you might want to research "two's complement".

When you subtract hex 10 from hex 05, it is decimal 16 subtracted from decimal 5 which is decimal -11 which would be -0B, not -0A.

The answer would not be 0A.  0A is 10 (dec).
The answer would not be 0B.  0B is 11 (dec).

The answer would be F5, if you're dealing with an signed 8-bit number.  F5 can be interpreted as either a signed number or an unsigned number.

If you're interpreting it as a signed number, here's how the numbers go:

Decimal:  3, 2, 1, 0 -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11

Two's Complement 8 bit hex:  3, 2, 1, 0, FF, FE, FD, FC, FB, FA, F9, F8, F7, F6, F5
It's kind of like an odometer on an old car, where if it's 00000 and you back up 1 mile, it becomes 99999, so 99999 is the representation of "-1".

The easy way to find the magnitude of the 2's complement number is to flip all the bits, then add 1.

F5 (hex) = 1111 0101 (binary).
Flip bits:  0000 1010.
Add 1: 0000 1011 (binary) = 0B (hex) = 11.

I hope I didn't confuse the issue!

greg AB8IM

On Sunday, July 19, 2020, 11:32:16 AM EDT, Craig Ledbetter <craig1611@...> wrote:

Well,
If I wanted to then multiply the answer from 05-10 (hex), would I use the answer F5 or 0A?
Now you might start to see my confusion?

And below are the actual numbers I am working with, and the answers I checked…
IT gave me a -A as an answer!
So am still confused. On 19 Jul 2020, at 15:45, Craig Ledbetter <craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

gregory simmons

greg AB8IM

================

On Sunday, July 19, 2020, 11:50:15 AM EDT, gregory simmons via groups.io <gregorysimmons@...> wrote:

Craig, you might want to research "two's complement".

When you subtract hex 10 from hex 05, it is decimal 16 subtracted from decimal 5 which is decimal -11 which would be -0B, not -0A.

The answer would not be 0A.  0A is 10 (dec).
The answer would not be 0B.  0B is 11 (dec).

The answer would be F5, if you're dealing with an signed 8-bit number.  F5 can be interpreted as either a signed number or an unsigned number.

If you're interpreting it as a signed number, here's how the numbers go:

Decimal:  3, 2, 1, 0 -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11

Two's Complement 8 bit hex:  3, 2, 1, 0, FF, FE, FD, FC, FB, FA, F9, F8, F7, F6, F5
It's kind of like an odometer on an old car, where if it's 00000 and you back up 1 mile, it becomes 99999, so 99999 is the representation of "-1".

The easy way to find the magnitude of the 2's complement number is to flip all the bits, then add 1.

F5 (hex) = 1111 0101 (binary).
Flip bits:  0000 1010.
Add 1: 0000 1011 (binary) = 0B (hex) = 11.

I hope I didn't confuse the issue!

greg AB8IM

On Sunday, July 19, 2020, 11:32:16 AM EDT, Craig Ledbetter <craig1611@...> wrote:

Well,
If I wanted to then multiply the answer from 05-10 (hex), would I use the answer F5 or 0A?
Now you might start to see my confusion?

And below are the actual numbers I am working with, and the answers I checked…
IT gave me a -A as an answer!
So am still confused. On 19 Jul 2020, at 15:45, Craig Ledbetter <craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

wrcooke@...

On July 19, 2020 at 10:32 AM Craig Ledbetter <craig1611@...> wrote:

Well,
If I wanted to then multiply the answer from 05-10 (hex), would I use the answer F5 or 0A?
Now you might start to see my confusion?
Neither.  10h is 16 decimal.  5 -16 = -11.  F5 is -11 decimal.
F5 = 1111 0101 binary
Take the two's complement
0000 1010   complement all bits
+ 0000 0001   then add one
= 0000 1011 = 0b hex = 11 decimal

To multiply, convert all numbers to positive.  Keep track of how many of them were negative.  If there is an ODD number of negative inputs then the output will need to be negated.  If the number of negative inputs is even, the answer will be correct without negating.
5Dec - 16Dec = -11 Dec
To multiply by 3:
-11 * 3 = -33
-11 = F5hex = 1111 0101
negate: 0000 1011   (see above)
3 dec = 3 hex = 0000 0011
multiply the postives:
0000 1011
X  0000 0011
--------------------------
0000 1011
0000 1011
0000 0000
... all zeros from here out
__________________
0000 0000 0010 0001  = 0021 hex = 33 decimal
Since one input was negative (odd number of negative inputs) we need to negate the answer
0000 0000 0010 0001   ---> 1111 1111 1101 1110 + 1 = 1111 1111 1101 1111 = ffcf hex = -33 decimal in two's complement

And below are the actual numbers I am working with, and the answers I checked…
IT gave me a -A as an answer!
So am still confused. On 19 Jul 2020, at 15:45, Craig Ledbetter < craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz < david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away." --  Antoine de Saint-Exupery

"The names of global variables should start with    // "  -- https://isocpp.org

(I am going to jump to C notation for hex here because that’s what’s most natural for me.)

Apples and oranges. You can’t look at 0x05 - 0x10 in itself and compare it to 0x05 - 0x10 in the context of the high byte of a 16-bit operation. This is because when converting 2’s complement you add one, but you only add one to the entire 16-bit number not each byte.

Here is how 0x05b1 - 0x103c would be done (with the 1802 instruction):

Subtract the low-byte first: 0xb1 - 0x3c = 0x75 (SD or SM)

Subtract the high-byte next: 0x05 - 0x10 = 0xf5 (SDB or SMB)

Adjust for any borrow from the low byte: 0xf5 - 0x00 = 0xf5

In this case there was no borrow to adjust for. The 1802 does the second two operations above in the one SDB or SMB instruction.

Therefore, the correct result in 2’s complement is 0xf575.

Multiplication code usually is usually done only on positive numbers and then the result is sign adjusted afterward. To change sign on a 2’s complement number to change this negative result to positive (with a simple example of 1802 instructions; there is more than one way to do this of course):

Invert all the bits in the low byte: 0x75 = 0x8a (XRI 0xff each byte)

Invert all the bits in the high byte: 0xf5 = 0x0a (XRI 0xff each byte)

Then add one to the low byte: 0x8a + 0x01 = 0x8b (ADI 0x01)

Add any carry to the high byte: 0x0a + 0x00 = 0x0a (ADCI 0x00)

In this case there is no carry from the low byte.

And this matches the answer below, 0x0a8b.

David

From: cosmacelf@groups.io <cosmacelf@groups.io> On Behalf Of Craig Ledbetter
Sent: Sunday, July 19, 2020 11:32 AM
To: cosmacelf@groups.io
Subject: Re: [cosmacelf] 16 bit Subtraction

Well,

If I wanted to then multiply the answer from 05-10 (hex), would I use the answer F5 or 0A?

Now you might start to see my confusion?

And below are the actual numbers I am working with, and the answers I checked…

IT gave me a -A as an answer!

So am still confused. On 19 Jul 2020, at 15:45, Craig Ledbetter <craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:

Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.

I like Greg's analogy with the car odometer and I think that's exactly the problem here...

Perhaps we keep saying "think outside the box" too many times....  because to understand 2's complement, you must think "inside the box".  Where the box is, 1 signed byte, 2 signed bytes (a word), 4 bytes (double word), 8 bytes (quad word), 16 bytes (a paragraph??? lol)

look at decimal math, we do not call -1 the number 999999 (in a world of 6 digits).

so, 2's complement is just a means to remap the 8 bits (or 16, etc) into a system that represents plus and minus the range of those bits (less 1 bit for sign).  There was another system, where they used the sign bit and still kept binary weights of positive numbers, so 10000101 was to represent -5.  But this system wasn't clean, because you could have a plus and minus zero, further, it just doesn't work right in electronic hardware.  And computers are hardware!

PS. I guess if we were to use more HEX in real math, -B would be correct and F5 would be wrong.

From: cosmacelf@groups.io <cosmacelf@groups.io> on behalf of gregory simmons via groups.io <gregorysimmons@...>
Sent: Sunday, July 19, 2020 11:50 AM
To: cosmacelf@groups.io <cosmacelf@groups.io>
Subject: Re: [cosmacelf] 16 bit Subtraction

Craig, you might want to research "two's complement".

When you subtract hex 10 from hex 05, it is decimal 16 subtracted from decimal 5 which is decimal -11 which would be -0B, not -0A.

The answer would not be 0A.  0A is 10 (dec).
The answer would not be 0B.  0B is 11 (dec).

The answer would be F5, if you're dealing with an signed 8-bit number.  F5 can be interpreted as either a signed number or an unsigned number.

If you're interpreting it as a signed number, here's how the numbers go:

Decimal:  3, 2, 1, 0 -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11

Two's Complement 8 bit hex:  3, 2, 1, 0, FF, FE, FD, FC, FB, FA, F9, F8, F7, F6, F5
It's kind of like an odometer on an old car, where if it's 00000 and you back up 1 mile, it becomes 99999, so 99999 is the representation of "-1".

The easy way to find the magnitude of the 2's complement number is to flip all the bits, then add 1.

F5 (hex) = 1111 0101 (binary).
Flip bits:  0000 1010.
Add 1: 0000 1011 (binary) = 0B (hex) = 11.

I hope I didn't confuse the issue!

greg AB8IM

On Sunday, July 19, 2020, 11:32:16 AM EDT, Craig Ledbetter <craig1611@...> wrote:

Well,
If I wanted to then multiply the answer from 05-10 (hex), would I use the answer F5 or 0A?
Now you might start to see my confusion?

And below are the actual numbers I am working with, and the answers I checked…
IT gave me a -A as an answer!
So am still confused. On 19 Jul 2020, at 15:45, Craig Ledbetter <craig1611@...> wrote:

Ugh. Okay.
So when I subtract 10 from 05, I get F5.
Obviously it should be -A.
And I supposed to invert the number once it is a negative? (As in subtract F5 from 00)?

On 18 Jul 2020, at 15:12, David Schultz <david.schultz@...> wrote:

On 7/18/20 8:59 AM, Craig Ledbetter wrote:
Thanks Bill.
The word ‘match” should have been MATH. Signed math functuons.
Your macro looks very similar to my routine, but it does not work with
all numbers. Borrowing seems too complicated for some reason. I am
having to do base 16 additions across the bytes for the carries, and
when I get negative numbers, they are not correct. Grrr.
I think I am over-thinking this, and hope there is a basic step that I
am missing.

Provide some test cases where you think that the results are wrong.

Bill's code is exactly correct. Subtract the low order bytes then do it
for the higher order bytes using subtract with borrow. Repeat as many
times as required.

Two's complement can seem a little odd at times but I learned it on the
Elf. So the college course on the subject was a breeze.