6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 15, 2024 9:01 am

All times are UTC




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: Thu Aug 30, 2018 5:59 am 
Offline

Joined: Tue Jun 19, 2018 8:28 am
Posts: 122
I have to admit, I am a little bit confused. I always thought that situation is straightforward.
Let's assume we have a following situation.

Code:
LDA variable
CMP #$10
BEQ LABEL
; these are instructions executed when variable != 16
; program simply continues to execute, increasing program counter
; you have to take care of it, to not hit LABEL after it's done
; for example by using RTS or JMP instruction
LABEL:
; this is set of instructions executed when variable == 16
; program jumps here after BEQ LABEL


But recently I was looking at a code compiled by CC65. There is a following fragment of a main() function.

Code:
if (variable < 16) {
   hd44780_putc('A');
}
while(1) { }


It is compiled as:

Code:
lda     _variable
cmp     #$10
L001F:   bcs     L001F
lda     #$41
jsr     _hd44780_putc
L001E:   jmp     L001E


Can anyone explain me how does it works? It looks like bcs instruction points at itself, by using L001F label.
Can it be done that way? Will bcs simply jump to the next label (L001E in that case) if variable is less or equal than 0x10?


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 30, 2018 6:45 am 
Offline

Joined: Sun May 07, 2017 3:59 pm
Posts: 21
Code:
lda     _variable
cmp     #$10
L001F:   bcs     L001F

That is simply an endless loop if _variable >= 16. So that is equivalent to your C code. The compiler just decided to implement your endless loop twice.


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 30, 2018 4:36 pm 
Offline

Joined: Mon May 25, 2015 1:12 pm
Posts: 92
No, you were on the right track the first time!

Firstly, the C code you wrote isn't functionally identical to the assembly code and secondly, CC65 doesn't produce very optimal code. CC65 still needs some of it's rough edges and documentation polishing and on my wishlist is support for floats and doubles (and I don't care what the whingers say about floats and doubles being slow on a 6502, they can go away).

BEQ branches when Z is set,
BCS branches when C is set.

you could have said "!=" in C instead.


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 31, 2018 4:46 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
DigitalDunc wrote:
Firstly, the C code you wrote isn't functionally identical to the assembly code and secondly, CC65 doesn't produce very optimal code.

It is a curious situation that the compiler implemented the endless loop twice, however.


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 31, 2018 11:00 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
Atlantis wrote:
Will bcs simply jump to the next label (L001E in that case) if variable is less or equal than 0x10?

I believe that "BCS" on the 65xx is equivalent to "BCC" or "BHS" from 680x/680x0-lands, which means branch if unsigned >= (assuming the flags were preconditioned by a compare or subtract instruction).

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Sat Sep 01, 2018 7:48 pm 
Offline
User avatar

Joined: Fri Dec 12, 2008 10:40 pm
Posts: 1007
Location: Canada
While it would have been no more efficient, it would have read better and been less confusing if the compiler code read:
Code:
lda     _variable
cmp     #$10
L001F:   bcs     L001E
lda     #$41
jsr     _hd44780_putc
L001E:   jmp     L001E

_________________
Bill


Top
 Profile  
Reply with quote  
PostPosted: Sat Sep 01, 2018 8:54 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
barrym95838 wrote:
Atlantis wrote:
Will bcs simply jump to the next label (L001E in that case) if variable is less or equal than 0x10?

I believe that "BCS" on the 65xx is equivalent to "BCC" or "BHS" from 680x/680x0-lands, which means branch if unsigned >= (assuming the flags were preconditioned by a compare or subtract instruction).

BCS on the 6502 branches on carry set and BCC branches on carry clear. Subtraction clears the carry if the subtraction results in a borrow and sets it otherwise. Comparisons have the same effect on the carry flag as subtraction. In the following code snippet it may appear that nothing is subtraced from memory location COUNTER but because the carry flag is cleared, one is subtracted from COUNTER.
Code:
define COUNTER_LO  $0
define COUNTER_HI  $1
LDA #$00
STA COUNTER_LO
LDA #$02
STA COUNTER_HI
CLC
LDA COUNTER_LO
SBC #$00
STA COUNTER_LO
LDA COUNTER_HI
SBC #$00
STA COUNTER_HI
BRK

This example runs on easy 6502 http://skilldrick.github.io/easy6502/ as I think this will be easier to follow than my Forth assembler syntax.

Cheers,
Jim


Top
 Profile  
Reply with quote  
PostPosted: Sat Sep 01, 2018 9:48 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
I forgot to mention when you try this code on easy 6502 http://skilldrick.github.io/easy6502/, after assembling the code, click the monitor button and the debugger button. Single step through the code to see the carry flag and memory locations change.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 19, 2018 9:11 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
I ran across this the other day. Just wish I found it sooner.
The SBC, TSX, and TXS Instructions of the 6502 and 6800
by B.T.G. Tan
Dr Dobbs Journal #79 May 1983 page 67 http://6502.org/documents/publications/dr_dobbs_journal/
It's in Volume 8.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 19, 2018 9:50 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8541
Location: Southern California
JimBoyd wrote:
I ran across this the other day. Just wish I found it sooner.
The SBC, TSX, and TXS Instructions of the 6502 and 6800
by B.T.G. Tan
Dr Dobbs Journal #79 May 1983 page 67 http://6502.org/documents/publications/dr_dobbs_journal/
It's in Volume 8.

page 280 of the .pdf file

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 20, 2018 7:07 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10979
Location: England
BTW another way to read Mike's scans of DDJ is at the internet archive:
https://archive.org/details/dr_dobbs_journal

and then we can link directly to the article:
https://archive.org/stream/dr_dobbs_jou ... 9/mode/1up

Thanks for the tip!


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 27, 2018 7:51 pm 
Offline

Joined: Tue Nov 27, 2018 7:32 pm
Posts: 4
Make _variable a volatile, to force the compiler to read it each time and prevent optimizing the infinite loop.


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 28, 2018 9:20 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10979
Location: England
(Welcome to the forum ghedger42!)


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 03, 2018 9:06 pm 
Offline
Site Admin
User avatar

Joined: Fri Aug 30, 2002 1:08 am
Posts: 281
Location: Northern California
JimBoyd wrote:
I ran across this the other day. Just wish I found it sooner.
The SBC, TSX, and TXS Instructions of the 6502 and 6800
by B.T.G. Tan
Dr Dobbs Journal #79 May 1983 page 67 http://6502.org/documents/publications/dr_dobbs_journal/
It's in Volume 8.

It's great to see the Dr. Dobb's scans are being put to good use.

This particular article is also available as a separate PDF under Dr. Dobb's Selected Articles (suggested by Ed).

_________________
- Mike Naberezny (mike@naberezny.com) http://6502.org


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 03, 2018 9:58 pm 
Offline

Joined: Tue Jul 24, 2012 2:27 am
Posts: 679
It seems quite an omission that it doesn't actually show you how to do stack-relative addressing on the 6502:

Code:
 ; Load (SP+4)
 tsx
 lda $0104,x


Of course, this doesn't support $01ff->0100 wrapping as PHA/PLA would, but that's usually a stack underflow anyway.

_________________
WFDis Interactive 6502 Disassembler
AcheronVM: A Reconfigurable 16-bit Virtual CPU for the 6502 Microprocessor


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: