DIY PROM Do It Yourself PROM chip burning help. No PROM begging. No PROMs for sale. No commercial exchange. Not a referral service.

Hopefully simple math question

Thread Tools
 
Search this Thread
 
Old Feb 16, 2004 | 08:22 AM
  #1  
BJM's Avatar
BJM
Thread Starter
Junior Member
 
Joined: Dec 2003
Posts: 46
Likes: 0
Hopefully simple math question

I am having trouble understanding how certain subtractions are represented.

If I have a value like 50 decimal in an accumulator and I subtract 50 from it I have 0 which is obviously 00000000 binary. If instead I subtract 51 from 50 I get -1 which is 11111111 where Bit 7 is the negative sign. I understand that the largest 8 bit negative result is therefore -128 or 10000000.

What I cannot figure out is how larger subtractions are handled like what happens if I do 1-130? Does it just keep going with the value appearing to be a positive value but the carry is set? I think this would be 01111111 with carry set, which without the carry would be +127.

Am I doing this right?
Reply
Old Feb 16, 2004 | 12:10 PM
  #2  
RBob's Avatar
Moderator
iTrader: (1)
 
Joined: Mar 2002
Posts: 18,432
Likes: 233
From: Chasing Electrons
Car: check
Engine: check
Transmission: check
Re: Hopefully simple math question

Originally posted by BJM
I am having trouble understanding how certain subtractions are represented.

If I have a value like 50 decimal in an accumulator and I subtract 50 from it I have 0 which is obviously 00000000 binary. If instead I subtract 51 from 50 I get -1 which is 11111111 where Bit 7 is the negative sign. I understand that the largest 8 bit negative result is therefore -128 or 10000000.

What I cannot figure out is how larger subtractions are handled like what happens if I do 1-130? Does it just keep going with the value appearing to be a positive value but the carry is set? I think this would be 01111111 with carry set, which without the carry would be +127.

Am I doing this right?
Yes, you are correct.

I did find a piece of GM code that did a subtract in a different way. Instead of subtracting 14 from a register, they added 242 to it. Then let the result at that.

Here is another piece of code that may help:
Code:
	LDD	   #1         ; load A = 0, B = 1
	SUBB	 #131         ; sub 131 from B, underflow it
	SBCA	 #$00         ; handle the underflow into reg A
The result in D is: $FF7F. The result in B isn't meaningful by itself without taking into account the carry flag.

At this point the sign bit on A needs to be checked as to whether the double reg D (A&B) is positive or negative.

RBob.
Reply
Old Feb 16, 2004 | 07:49 PM
  #3  
BJM's Avatar
BJM
Thread Starter
Junior Member
 
Joined: Dec 2003
Posts: 46
Likes: 0
Thanks for the reply.

I just thought of something else and a little different than you example using Acc D.

If, after I did the 1-130 and wound up with -129 and the carry is set, I then add some amount to make it fall within the -128 limit will this give the correct answer? Let's say I add 2, I should now be at -127, $81, 10000001. Does this work directly or do I have to do something special with the carry?
Reply
Old Feb 17, 2004 | 06:52 AM
  #4  
RBob's Avatar
Moderator
iTrader: (1)
 
Joined: Mar 2002
Posts: 18,432
Likes: 233
From: Chasing Electrons
Car: check
Engine: check
Transmission: check
Originally posted by BJM
Thanks for the reply.

I just thought of something else and a little different than you example using Acc D.

If, after I did the 1-130 and wound up with -129 and the carry is set, I then add some amount to make it fall within the -128 limit will this give the correct answer? Let's say I add 2, I should now be at -127, $81, 10000001. Does this work directly or do I have to do something special with the carry?
For this example it works as you show. However, doing math in this manner is playing with fire. After each 8-bit ADD or SUB the carry flag should be tested. This is because after the first 1 - 130 the result is invalid. So even though it works out in this case it may not in another (such as sub 134 instead of 130, then add 2).

RBob.
Reply
Old Feb 17, 2004 | 08:07 PM
  #5  
BJM's Avatar
BJM
Thread Starter
Junior Member
 
Joined: Dec 2003
Posts: 46
Likes: 0
Point taken. Thanks again.
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Fronzizzle
Electronics
3
Aug 17, 2015 02:52 PM
Jake_92RS
Tech / General Engine
3
Aug 17, 2015 09:42 AM
redmaroz
LTX and LSX
7
Aug 16, 2015 11:40 PM
Armored91Camaro
DIY PROM
3
Aug 12, 2015 09:41 AM
camaroken83
Exhaust
6
Aug 10, 2015 11:35 AM




All times are GMT -5. The time now is 04:14 PM.