## Hello, new here, and need best recommandations.

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

tokumaru
Posts: 11561
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

### Re: Hello, new here, and need best recommandations.

All numbers are signed and unsigned at the same time. That may sound confusing, but the great thing about 2's complement is that all binary operations are correct for signed and unsigned values. The only difference is how YOU interpret the results and make decisions based on them.

\$FF is 255 unsigned, or -1 signed, both at the same time. If you subtract 2 from it, the result is correct either way:

\$FF - \$02 = \$FD
Unsigned: 255 - 2 = 253
Signed: -1 - 2 = -3

The answer is correct either way. The CPU uses the carry flag to signal unsigned overflows/underflows, and the V flag to signal signed overflows/underflows. Depending on what you need the numbers for, you'll use one flag or the other (or the N flag, which's also useful after signed operations) to make decisions.

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

tokumaru wrote:All numbers are signed and unsigned at the same time. That may sound confusing, but the great thing about 2's complement is that all binary operations are correct for signed and unsigned values. The only difference is how YOU interpret the results and make decisions based on them.

\$FF is 255 unsigned, or -1 signed, both at the same time. If you subtract 2 from it, the result is correct either way:

\$FF - \$02 = \$FD
Unsigned: 255 - 2 = 253
Signed: -1 - 2 = -3

The answer is correct either way. The CPU uses the carry flag to signal unsigned overflows/underflows, and the V flag to signal signed overflows/underflows. Depending on what you need the numbers for, you'll use one flag or the other (or the N flag, which's also useful after signed operations) to make decisions.

So it's like a mirror-mirror style of positive and negative where...............

255 is positive [unsigned]

and 1 is negative [signed]

-

251 is positive [unsigned]

and 5 is negative [signed]

-

190 is positive [unsigned]

and 64 is negative [signed]

255
-65
---
190

?????

I would see the pattern if that's correct..? Which is why BCC and BCS / BPL and BMI exists?

tepples
Posts: 21882
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Hello, new here, and need best recommandations.

In two's complement, 0 through 127 (\$00 through \$7F) are positive, and 128 through 255 (\$80 through \$FF) are treated as negative. They represent -128 through -1. All the positive numbers have bit 7 clear (0), and all the negative numbers have bit 7 set (1). This is why BPL (branch if plus) jumps if the last result had bit 7 clear, and BPL (branch if minus) jumps if the last result had bit 7 set.

BCC and BCS are used if you're comparing two unsigned numbers, and BPL and BMI are used if you're comparing two signed numbers. You also use BPL in a loop that counts down to 0 and stops once the index has passed zero:

Code: Select all

``````  ldy #4
loop:
; Y will equal 4, then 3, then 2, then 1, then 0.
; Do something with Y each time (omitted).

; Now count down: 4 becomes 3, 3 becomes 2, ..., 0 becomes -1
dey
; If Y hasn't become negative, stay in the loop
bpl loop
``````

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

tepples wrote:In two's complement, 0-127 are positive, and 128-255 are treated as negative. They represent -128 through -1.
.......

But that's single.. what about what tokumaru wrote about that they're signed and unsigned at same time?

So in Tokumaru part, it's a mirror of positive and negative of +255 and -1, and it can go down into +3 and -253.

And in your part, it's two parts where 0-127 is positive lever, and 128-255 is negative lever, also known as -1 and -128..?

[Obviously[[?]], zero is a positive number.]

tepples
Posts: 21882
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Hello, new here, and need best recommandations.

The number \$C0 can represent 192, or it can represent -64. The 6502 itself couldn't give a care. In practice, it depends on the context of the code around it. As an assembly language programmer, you are responsible for imbuing numbers with semantics, defining what each value means in context.

Zero is traditionally neither negative nor positive. But the BPL and BMI branches treat a zero result as nonnegative, as if it were positive. To treat zero differently from positive numbers proper, you'll need to use both a BNE or BEQ and a BPL or BMI. You'll need to do this, for example, to distinguish a stopped character from one moving to the right.

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

tepples wrote:The number \$C0 can represent 192, or it can represent -64. The 6502 itself couldn't give a care. In practice, it depends on the context of the code around it. As an assembly language programmer, you are responsible for imbuing numbers with semantics, defining what each value means in context.

Zero is traditionally neither negative or positive. But the BPL and BMI branches treat a zero result as nonnegative, as if it were positive. To treat zero differently from positive numbers proper, you'll need to use both a BNE or BEQ and a BPL or BMI.
So there's an opcode where it let me define a numbers in favor for "Mirror-+" or "half -+" styles? Or what does 6502, in form if NES, support only?

tepples
Posts: 21882
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Hello, new here, and need best recommandations.

If you write lda #<-10, ca65 will treat that as a two's complement number and translate it into lda #246.
1. The # means that lda will load an immediate value, as opposed to reading from a memory address.
2. The < means "keep the low byte". An assembler on a 32-bit platform, such as a PC, will internally process all values as 32-bit values. In 32-bit two's complement, -10 is \$FFFFFFF6, and the < operator tells the assembler to cut the value down to 8-bit.

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

tepples wrote:If you write lda #<-10, ca65 will treat that as a two's complement number and translate it into lda #246.
1. The # means that lda will load an immediate value, as opposed to reading from a memory address.
2. The < means "keep the low byte". An assembler on a 32-bit platform, such as a PC, will internally process all values as 32-bit values. In 32-bit two's complement, -10 is \$FFFFFFF6, and the < operator tells the assembler to cut the value down to 8-bit.
...ca65 is part of Colecovision that Unregistered is part of, or the 6502?..

tepples
Posts: 21882
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Hello, new here, and need best recommandations.

ca65 is a 6502 assembler. It has nothing to do with Coleco.

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

But what about the NESASM6 part? Or ca65 better choice?

tokumaru
Posts: 11561
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

### Re: Hello, new here, and need best recommandations.

There's no "NESASM6"... There's NESASM and ASM6, two separate assemblers. NESASM and ASM6 are much more newbie friendly, but ca65 has more features than both combined. Because NESASM has some weird quirks, I usually suggest ASM6 to beginners.

Keep in mind that any of them can be used to make any kind of NES game, the difference is that ca65 has some features that make larger projects easier to manage, if you know how to use said features.

unregistered
Posts: 1070
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

### Re: Hello, new here, and need best recommandations.

Note: I can't be part of Colecovision; I've never even touched a Colecovision.
DocWaluigean wrote:
tokumaru wrote:All numbers are signed and unsigned at the same time. That may sound confusing, but the great thing about 2's complement is that all binary operations are correct for signed and unsigned values. The only difference is how YOU interpret the results and make decisions based on them.

\$FF is 255 unsigned, or -1 signed, both at the same time. If you subtract 2 from it, the result is correct either way:

\$FF - \$02 = \$FD
Unsigned: 255 - 2 = 253
Signed: -1 - 2 = -3

The answer is correct either way. The CPU uses the carry flag to signal unsigned overflows/underflows, and the V flag to signal signed overflows/underflows. Depending on what you need the numbers for, you'll use one flag or the other (or the N flag, which's also useful after signed operations) to make decisions.

So it's like a mirror-mirror style of positive and negative where...............

255 is positive [unsigned]

and 1 is negative [signed]

-
Note: we will deal with #255 as the hardware (the 6502) sees it... in binary: #11111111b
11111111 is positive [unsigned]

and 11111111 is negative [signed]

because, like tepples said, bit7 is set and so the "signed" way of using 11111111 makes that represent a negative number (-1).

You can experiment with Windows 10's Calculator to better understand signed and unsigned. Open Calculator, click the three horizontal lines in the upper left, click "Programmer's Mode". Click on the QWORD until it reads BYTE. Click on BIN. Type or click 11111111. Notice that the signed decimal value, next to DEC, reads -1. Now click BYTE again and it will goto QWORD. Then click the C to clear the calculator. Next type or click 11111111 again. Now the value next to DEC will read 255 (that's the unsigned version of #11111111b).

Windows 10's Calculator's Programmer's Mode always switches the decimal version (next to DEC) to signed when the left most bit of the value is set. Have to go... sorry.

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

tokumaru wrote:There's no "NESASM6"... There's NESASM and ASM6, two separate assemblers. NESASM and ASM6 are much more newbie friendly, but ca65 has more features than both combined. Because NESASM has some weird quirks, I usually suggest ASM6 to beginners.

Keep in mind that any of them can be used to make any kind of NES game, the difference is that ca65 has some features that make larger projects easier to manage, if you know how to use said features.
Ooooh. What features does ca65 have that ASM6 doesn't?

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

unregistered wrote:Note: I can't be part of Colecovision; I've never even touched a Colecovision.
DocWaluigean wrote:
tokumaru wrote:All numbers are signed and unsigned at the same time. That may sound confusing, but the great thing about 2's complement is that all binary operations are correct for signed and unsigned values. The only difference is how YOU interpret the results and make decisions based on them.

\$FF is 255 unsigned, or -1 signed, both at the same time. If you subtract 2 from it, the result is correct either way:

\$FF - \$02 = \$FD
Unsigned: 255 - 2 = 253
Signed: -1 - 2 = -3

The answer is correct either way. The CPU uses the carry flag to signal unsigned overflows/underflows, and the V flag to signal signed overflows/underflows. Depending on what you need the numbers for, you'll use one flag or the other (or the N flag, which's also useful after signed operations) to make decisions.

So it's like a mirror-mirror style of positive and negative where...............

255 is positive [unsigned]

and 1 is negative [signed]

-
Note: we will deal with #255 as the hardware (the 6502) sees it... in binary: #11111111b
11111111 is positive [unsigned]

and 11111111 is negative [signed]

because, like tepples said, bit7 is set and so the "signed" way of using 11111111 makes that represent a negative number (-1).

You can experiment with Windows 10's Calculator to better understand signed and unsigned. Open Calculator, click the three horizontal lines in the upper left, click "Programmer's Mode". Click on the QWORD until it reads BYTE. Click on BIN. Type or click 11111111. Notice that the signed decimal value, next to DEC, reads -1. Now click BYTE again and it will goto QWORD. Then click the C to clear the calculator. Next type or click 11111111 again. Now the value next to DEC will read 255 (that's the unsigned version of #11111111b).

Windows 10's Calculator's Programmer's Mode always switches the decimal version (next to DEC) to signed when the left most bit of the value is set. Have to go... sorry.
It's alright.. I'm a little confused though.

So...

00000000 is unsigned

00100000 is unsigned

01000000 is signed

10000000 is signed

11000000 is signed

11010101 is signed

Is this what it means?...

DocWaluigean
Posts: 155
Joined: Sun Jun 17, 2018 6:41 pm

### Re: Hello, new here, and need best recommandations.

I got to ask even extremely ridiculous question even if there's chance I got right or not so I can find and understand in the most simplest or elementary explanation even toddler can understand. Just a reminder if I said this before?