Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Carry Questions

Reply to topic
Author Message
Consolemu
  • Guest
Reply with quote
Carry Questions
Post Posted: Mon May 01, 2000 7:24 pm
I took what Helophope said into consideration of hitting the books and that's what I did. I took a break from the coding part and decided to "enlighten" my intelegence with the old books. I searched for the ~ operator and really read the paragraph word for word and it hit me like a brick wall. It kept saying NOT this, NOT that. So I went to the Microsoft Assembly book, looked up NOT, and *bam* I discovered this phonomenon called NOT. It's an assembly instruction that inverts all the bits in a variable (register or whatever). Example...

00000100

After the NOT or invertion process it will equal

11111011

I don't know where in the hell they thought of using NOT for Invertion (would've been better to say INV or VERT) but it really doesn't fit it's description. C is a crazy symbolic language anyway so the ~ doesn't bother me one bit. Just another symbol to memorize.

I continued my venture through the books and I decided to tackle the Carry instructions like ADC and SBC. I kinda see what they were getting at, by using an extra bit to represent values beyond what the register could handle, but it's confusing when you have to simulate that. Like a normal ADD will add two values, a normal Shift will shift bits a certain amount of places, and a normal Rotate will rotate bits a certain amount of places. But when these instructions include Carry this is where I get stumped. I'll start with ADC. In the book it says "Add with Carry", it's diagram is like this...

value1 + value2 + C = final value

How am I adding Carry? Does the processor simply add 1 if the carry bit is set or does it do something special like adding the already carried value plus another value? What if the carry bit is set, and the addition of the two values carry again? And SBC...the book says "Subtract with Carry", it's diagram is like this...

value1 - value2 - NOTC = final value

Why am I inverting Carry with NOT? If the inverted C equals 1, does the processor subtract by 1 or does it do something special like subtracting the already undercarried value minus another value?

Last one that twisted my nose was RO(L or R) (Rotate with Carry). This one I'll try to guess on my own. It works like this...

if carry bit is 0, then perform a normal rotation...

[ BYTE ] MSB to LSB

But, if carry bit is 1, then perform a special rotation...

[ BYTE ] C to LSB, then MSB to C


In arithmetic instructions, does the carry pose as the MSB and the variable is normal unsigned data? Is it like being able to represent values positive or negative up to 256? Like if I wanted to represent 256 positive, I could make the register equal 0xFF and make the carry bit 0. But if I wanted to represent -256 then I make the register equal 0 and set the carry bit?

Chris :o)
 
  • Joined: 24 Jun 1999
  • Posts: 1732
  • Location: Paris, France
Reply with quote
Post Posted: Mon May 01, 2000 8:58 pm
Quote
> I don't know where in the hell they thought of using NOT for Invertion (would've been better to say INV or VERT) but it really doesn't fit it's description.

One is not zero.
Zero is not one.
  View user's profile Send private message Visit poster's website
  • Joined: 18 Sep 1999
  • Posts: 498
  • Location: Portland, Oregon USA
Reply with quote
Post Posted: Mon May 01, 2000 9:28 pm
Quote
> I continued my venture through the books and I decided to tackle the Carry instructions like ADC and SBC. I kinda see what they were getting at, by using an extra bit to represent values beyond what the register could handle, but it's confusing when you have to simulate that. Like a normal ADD will add two values, a normal Shift will shift bits a certain amount of places, and a normal Rotate will rotate bits a certain amount of places. But when these instructions include Carry this is where I get stumped. I'll start with ADC. In the book it says "Add with Carry", it's diagram is like this...
>
> value1 + value2 + C = final value
>

> How am I adding Carry? Does the processor simply add 1 if the carry bit is set or does it do something special like adding the already carried value plus another value?

It simply adds 1 if CARRY is SET.


Quote
> What if the carry bit is set, and the addition of the two values carry again?

The CARRY flag is simply set again.

Quote
> And SBC...the book says "Subtract with Carry", it's diagram is like this...
>
> value1 - value2 - NOTC = final value
>

> Why am I inverting Carry with NOT? If the inverted C equals 1, does the processor subtract by 1 or does it do something special like subtracting the already undercarried value minus another value?

This case is really too technical to explain quickly on this messageboard. I recommend really studying two's complement representation and learn how subtraction (without CARRY works). Then come back if you still don't understand SBC.


Quote
> In arithmetic instructions, does the carry pose as the MSB and the variable is normal unsigned data?

First, as you know, there's a difference between SIGNED and UNSIGNED arithmetic. I cannot emphasize enough how important it is to distinguish between the two (This distinction was a constant source of difficulty for students in the class I taught.)

There's one rule you should never forget: CARRY is for UNSIGNED arithmetic. OVERFLOW is for SIGNED. If you can remember that, you're set.

CARRY and OVERFLOW basically tell you the same thing: the result of the operation does not fit in the space provided (8 or 16 bits depending on the operation.)

When doing UNSIGNED addition/subtraction, the CARRY bit can be considered a "ninth" (or 17th) bit. It would be the value of the most-significant bit of the result if there had been room to store it.

It makes no sense to consider CARRY when doing UNSIGNED arithmetic. Remember, it is up to the programmer to know whether he/she is doing unsigned/signed arithmetic and to observe the CARRY or OVERFLOW flags accordingly.


Quote
> Is it like being able to represent values positive or negative up to 256? Like if I wanted to represent 256 positive, I could make the register equal 0xFF and make the carry bit 0. But if I wanted to represent -256 then I make the register equal 0 and set the carry bit?

There's no way to represent 256 in 8-bits. The largest 8-bit number is 255. If the result of adding two (UNSIGNED) numbers is 256, the register would be 0x00, and CARRY would be SET.

Hope this helps, for now.

Eric Quinn
  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Tue May 02, 2000 4:59 am

Quote
> I don't know where in the hell they thought of using NOT for Invertion (would've been better to say INV or VERT) but it really doesn't fit it's description. C is a crazy symbolic language anyway so the ~ doesn't bother me one bit. Just another symbol to memorize.

See, NOT makes sense to me. It keeps in line with the convention of using english prepositions for logical operations, and, as Zoop said, 1 is not 0, 0 is not 1.


Let me just present you with a quick guide to binary mathematics:

The purpose of the borrow/carry flag is (most commonly) used as an aid for doing 16bit (or higher) math in 8 bit steps.

For add with carry:

just like in decimal, the digit that is 'carried' is the portion of the sum of the two digits that overflowed into the next digit up:

in decimal:

9 + 5 = 14 (the 1 is carried to the next digit)

99 + 99 = 198 (two carries, once into the tens digit, and then again into the hundreds).

in binary it works the same way:

1 + 1 = 10 (a bit is carried into the 2's digit)

111 + 010 = 1001 (carry into the 4's digit and again into the 8's).

Since the z80 and 6502 are 8-bit processors, they can add two 8-bit numbers at a time. The carry across these digits are nothing to concern yourself with.

Carry between bytes, however, is significant: note:

$01ff + $0101
If we didn't use carry, we'd add it up in 8 bit chunks like so:
(least signif bytes)
$ff + $01 = $00 (whoops, $ff rolled over into $0. it should be $100 (256 dec) but that's out of the range of a byte.
(most sig bytes)
$01 + $01 = $02
answer is $0200. Like hell it is, it should be $300.
With carry, the cpu detects that result of the first operation ($ff + $01) carried over into the next digit (the phantom inth digit that would represent 256)
So, when you add the second set of bytes, and use an add with carry instruction, it'll add that extra bit to the result of addition, if the carry was set.
So, short version: The add with carry instruction add the operands, plus '1' if the carry was set. The above explains why.

my explainations are getting sloppy, I tried to clarify subtract with carry and it got very confusing. ugh. tired. I'll clarify more later if you ask. In the meantime, read through your assembly books on binary mathmatics, the concepts used are universal.

"Don't stop, you can sleep when you're dead"



Quote
> Last one that twisted my nose was RO(L or R) (Rotate with Carry). This one I'll try to guess on my own. It works like this...

roll/shift with carry means:
Do the normal shift:
The bit shifted in should be one if the carry was set, zero if it wasn't.
The carry should be set to the bit that was shifted out.
Pow, that's it.



Quote
> In arithmetic instructions, does the carry pose as the MSB and the variable is normal unsigned data? Is it like being able to represent values positive or negative up to 256? Like if I wanted to represent 256 positive, I could make the register equal 0xFF and make the carry bit 0. But if I wanted to represent -256 then I make the register equal 0 and set the carry bit.

"A pint cannot hold a quart. When it's holding a pint, it's doing the best hat it can!"
-V.I.N. Cent, _The Black Hole_

The carry doesn't change the value of the accumulator or any other byte, it's just an aid for doing math across several bytes.
Looks like you're a bit confused about how negative values are stored in memory.

$ff is -1, but only in the context of an 8bit signed value. Otherwise it's 255.
$80 is -128 in the context of an 8 bit signed value, positive 128 otherwise.
$ffff is -1 in the context of a 16bit signed value, 65536.
It's up to you to determine how it is interpreted.

  View user's profile Send private message Visit poster's website
Reply to topic



Back to the top of this page

Back to SMS Power!