It is currently Thu Dec 14, 2017 6:28 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Thu Dec 01, 2016 1:20 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5896
Location: Canada
koitsu wrote:
Can you provide some insights into why you use this method in the first place, rather than jmp? I would love to know, as I'm sure readers would as well. I just can't see the justification other than size savings, but there may be cases I'm not thinking of thinking of; I'm not very good at thinking "outside the box".

Because branching is relative and jumping is absolute, if you want to write "relocatable" code that can be copied to multiple places in RAM, or maybe if you want to provide a library as a binary blob that can be placed anywhere rather than assembly source (portability without having to reformat for different assemblers?).

I've never wanted to do that, personally, but I've seen it done, and it's a situation where JMP is unavailable. Similarly if you want a branch but are just over the relative limit, if you're lucky you might be able to convert a few JMP instructions inside the loop to make the branch fit (eliminating a branch over a jump saves 3 bytes and 3 cycles).


In general, though, I would just use JMP. Trying to do an "unconditional" branch means it will take slightly longer to deal with when the code must be edited later (or unslightly longer if I don't notice and break the condition). Usually not having to think about it is worth the extra byte for me.

Unless I'm in a situation that needs to be optimized for size, I don't see the point. You're saving a byte, but for what? Why do you need that byte? If I ever do run out of space, and I'm near the end of a project and by some miracle I only need 100 more bytes, maybe then it would actually be worth scouring the code to convert jumps to branches. Until that particular obscure case arises, I consider it "premature optimization". There has to be a specific size goal that seems within reach of the savings I'll get.


If dougeff is reading this thread, you might ask him how much extra time and effort he had to spend on his Famitone modifications because of the unconditional branches peppered throughout its code. I know it consumed quite a bit of extra time for me when I was doing similar work with it. ;P

In the rare case where I do want to do it, this is the notation I tend to use:
Code:
;clc
bcc label


Top
 Profile  
 
PostPosted: Thu Dec 01, 2016 1:26 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19342
Location: NE Indiana, USA (NTSC)
rainwarrior wrote:
In the rare case where I do want to do it, this is the notation I tend to use:
Code:
;clc
bcc label

Same here. Sometimes I'll spell it out a bit more by adding a comment within a comment:
Code:
  ;sec  ; carry set by previous CMP
  bcs label


Top
 Profile  
 
PostPosted: Thu Dec 01, 2016 1:39 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10164
Location: Rio de Janeiro - Brazil
koitsu wrote:
Can you provide some insights into why you use this method in the first place, rather than jmp? I would love to know, as I'm sure readers would as well. I just can't see the justification other than size savings, but there may be cases I'm not thinking of thinking of; I'm not very good at thinking "outside the box".

The only reason I can think of is indeed the fact that a branch uses 2 bytes while a jump uses 3. It might not make such a difference when you have 32KB of PRG-ROM (still, it's one free byte - free as long as you don't cross a page boundary), but I believe I picked up this habit when designing Atari 2600 kernels, and that machine only has 4KB of ROM (without bankswitching), so every byte counts. In that case it was also easier to ensure that the branches wouldn't cross a page boundary and end up taking longer than a JMP.


Top
 Profile  
 
PostPosted: Thu Dec 01, 2016 2:17 pm 
Offline
Formerly WheelInventor

Joined: Thu Apr 14, 2016 2:55 am
Posts: 1124
Location: Gothenburg, Sweden
Garth wrote:
I get up this morning and find a whole lot of new posts here. I'm glad this idea has quickly generated this level of interest here! It's refreshing, because what I've found in the past is that it's hard to get people past their preconceived notions and keep their attention long enough to clarify that macros offer absolutely zero penalty in most cases, only make the source code more readable and maintainable and make it easier to steer free of bugs and avoid losing control of a big project.


I can only speak for myself, but this option is super helpful to me - i'm still translating and examining your macros but i recognise they'll let me keep the focus on learning how to actually make something, rather than have me spend most of the time finding where i randomly wrote something wrong. It's been kind of fun disassembling programs, print out portions of them and draw lines to see what goes where, though. It may matter a little less when you've already developed a working codebase and/or a certain way with hand-writing code, but even so - less human factor this way, and better view.

Oh, and all code shared would be depending on a macro attachment. But that's a fairly low price to pay. Better yet if it is a semi-standard. Additionally, everyone can easily read and understand a nest consisting of if-else-endif or for-next or rts-if-eq macros if the code's just up for peer review.

Quote:
LIST "ON" | LIST "OFF" in the C32 assembler turns the generation of list code on and off in the .lst file. Since this assembler didn't have any way to set up an array of assembler variables to use as a stack and be able to index into it, I had to do this mickey-mouse workaround
Unless it's a hidden feature, it doesn't seem asm6 can set up an array like that either (then again, i'm green), but maybe ENUM/.enum | ENDE/.ende can be used to cover for LIST, since it is suppressing the output. Only weird thing is that it also sets PC. Maybe it is optional, or can withstand a nonsense declaration like ENUM $. We'll see in a while.

EDIT: Got everything to assemble in asm6! just writing .enum throws an error because it expects an operand, but .enum $ seems to do the trick and the rest of it seems to "work" so far. :P It's past bedtime here but i'll get back to it tomorrow. Would it be ok to share the translation here when i've cleaned a few bits up, Garth?

rainwarrior wrote:
Because branching is relative and jumping is absolute, if you want to write "relocatable" code that can be copied to multiple places in RAM, or maybe if you want to provide a library as a binary blob that can be placed anywhere rather than assembly source (portability without having to reformat for different assemblers?).
Would it because of this be sensible for a general macro package to have a default set using JMP, and a secondary using oVerflow branches, possibly with a conditional warning sent to the list file? Or would one rather write branch-using code case-for-case?

_________________
http://www.frankengraphics.com - personal NES blog


Top
 Profile  
 
PostPosted: Sat Apr 01, 2017 8:09 pm 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 93
Location: Southern California
FrankenGraphics wrote:
EDIT: Got everything to assemble in asm6! just writing .enum throws an error because it expects an operand, but .enum $ seems to do the trick and the rest of it seems to "work" so far. :P It's past bedtime here but i'll get back to it tomorrow. Would it be ok to share the translation here when i've cleaned a few bits up, Garth?

My apologies—I never saw the edit until now, several months later. I'm sure that would be fine. I would like to see this programming tool get more widespread use to promote the 6502! :D Again, the explanations are in main article on this subject at http://wilsonminesco.com/StructureMacros/, with additional explanation of what the assembler does at assembly time in the "assembling program structures" chapter in the 6502 stacks treatise, at http://wilsonminesco.com/stacks/pgmstruc.html .

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Top
 Profile  
 
PostPosted: Mon Apr 17, 2017 9:52 am 
Offline
User avatar

Joined: Thu Apr 23, 2009 11:21 pm
Posts: 806
Location: cypress, texas
tokumaru wrote:
koitsu wrote:
Can you provide some insights into why you use this method in the first place, rather than jmp? I would love to know, as I'm sure readers would as well. I just can't see the justification other than size savings, but there may be cases I'm not thinking of thinking of; I'm not very good at thinking "outside the box".

The only reason I can think of is indeed the fact that a branch uses 2 bytes while a jump uses 3. It might not make such a difference when you have 32KB of PRG-ROM (still, it's one free byte - free as long as you don't cross a page boundary), but I believe I picked up this habit when designing Atari 2600 kernels, and that machine only has 4KB of ROM (without bankswitching), so every byte counts. In that case it was also easier to ensure that the branches wouldn't cross a page boundary and end up taking longer than a JMP.

There have been multiple times where I've used an unconditional branch and it not only saved me a byte of space, but it also allowed the branch to not cross page boundaries. Branches crossing page boundaries are a disease on the nes causing randomness to happen on screen, code to malfunction, and an extra cycle needed for that branch. That's what I've experienced so far. :)

edit: kotisu said that branch instructions take 3 cycles if the branch is taken, otherwise 2. That's great to know, thanks kotisu!! :D I'll have to go and make sure my unconditional branches always fail... whitch would be easy I think... just replace the bpl with a bmi and reorder the code... reordering code can save cycles and bytes. :mrgreen:


Top
 Profile  
 
PostPosted: Mon Apr 17, 2017 10:28 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5896
Location: Canada
unregistered wrote:
Branches crossing page boundaries are a disease on the nes causing randomness to happen on screen, code to malfunction, and an extra cycle needed for that branch. That's what I've experienced so far. :)

The only one of those things they really cause is the extra cycle for the branch.

I don't know how you came to experience those other things, but it was probably only a coincidence that a branch crossing a page was involved. Page crossing branches are perfectly functional and extremely commonplace. There is no normal need to avoid them except to save that cycle for performance reasons or precise timing.


Top
 Profile  
 
PostPosted: Mon Apr 17, 2017 10:48 am 
Offline

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 93
Location: Southern California
To make sure it's clear:
A branch that is taken normally takes 3 clock cycles, but it's 4 if it crosses a page boundary. Most branches do not cross page boundaries.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Top
 Profile  
 
PostPosted: Mon Apr 17, 2017 11:59 am 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5896
Location: Canada
Garth wrote:
To make sure it's clear:
A branch that is taken normally takes 3 clock cycles, but it's 4 if it crosses a page boundary. Most branches do not cross page boundaries.

I think most is true, but it's still very common for them to cross page boundaries. I'd ballpark maybe something like 10% of branches are page-crossers? They're something you can safely not think about almost all of the time.

(Hard to estimate, if branch lengths were evenly distributed it would be 25%, I think, but shorter branches are more commonly used, so in practice it must skew a lot lower.)


Top
 Profile  
 
PostPosted: Wed Apr 19, 2017 8:12 am 
Offline
User avatar

Joined: Thu Apr 23, 2009 11:21 pm
Posts: 806
Location: cypress, texas
rainwarrior wrote:
I don't know how you came to experience those other things, but it was probably only a coincidence that a branch crossing a page was involved.
I guess it is because God is helping me to use my sister's animations in our nes game. There was definitely a time when I fixed branches crossing page problems and that solved something weird that was happening with the animation frames. :) Consequently, almost all of my branches don't cross page boundaries.


Top
 Profile  
 
PostPosted: Wed Apr 19, 2017 9:27 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10164
Location: Rio de Janeiro - Brazil
unregistered wrote:
I guess it is because God is helping me to use my sister's animations in our nes game.

You know, I was gonna make a joke about this, but I actually have something serious to say: if you really do believe in an entity that created everything and helps people, don't you find it offensive to say you're getting help with your little NES project when there are people suffering from diseases, violence and starvation all around the world? Do you really think a real, benevolent God would care about your silly little game while neglecting all those who need actual help?

If your game is coming along well, I'm pretty sure it's because of your own merit. It'd also be your own fault if it wasn't going well, not someone punishing you. You decided to learn NES programming and you've been working hard to accomplish your goals, this is your doing.

I don't mean to start discussing religion here (that never ends well), but I was surprisingly offended by the fact that someone believes that with so much shit going on in the world, a higher being is somehow spending time on stupid little blessings such as retro computer games. I find it disrespectful even to the God you believe in, dragging his name into something as silly as this.


Top
 Profile  
 
PostPosted: Wed Apr 19, 2017 9:34 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19342
Location: NE Indiana, USA (NTSC)
tokumaru wrote:
if you really do believe in an entity that created everything and helps people, don't you find it offensive to say you're getting help with your little NES project when there are people suffering from diseases, violence and starvation all around the world?

No, because you are arguing the fallacy of relative privation.

Image
Straw man says hi


See articles on RationalWiki, Logically Fallacious, All The Tropes, and Wikipedia.

tokumaru wrote:
Do you really think a real, benevolent God would care about your silly little game while neglecting all those who need actual help?

It's possible if unreg is using this "silly little game" as a means of training for a paying job in video game development, ten percent of whose paycheck goes to charity for "those who need actual help."


Top
 Profile  
 
PostPosted: Wed Apr 19, 2017 9:48 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10164
Location: Rio de Janeiro - Brazil
Yeah, I'm out.


Top
 Profile  
 
PostPosted: Wed Apr 19, 2017 11:33 am 
Offline
User avatar

Joined: Sat Jan 09, 2016 9:21 pm
Posts: 263
Location: Central Illinois, USA
unregistered wrote:
Branches crossing page boundaries are a disease on the nes causing randomness to happen on screen, code to malfunction, and an extra cycle needed for that branch. That's what I've experienced so far. :)


Oh man, you think it's bad on the NES, page boundaries really mess everything up if you cross them in an Atari display kernel....you make one little tweak and suddenly nothing displays correctly.

_________________
My games: http://www.bitethechili.com


Top
 Profile  
 
PostPosted: Wed Apr 19, 2017 4:47 pm 
Offline
User avatar

Joined: Thu Apr 23, 2009 11:21 pm
Posts: 806
Location: cypress, texas
God's will for my life is that I make this nes game. Following His will brings me so much joy! There's an inner peace that makes life outstanding to experience. Hope this makes sense. I mentioned God because he deserves the credit; he made me, knows everything about me, like the number of hairs on my head. God is not suprized at what is going on in the world; he is there with the people.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 7 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 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:  
Powered by phpBB® Forum Software © phpBB Group