Skip to content

Sure – But Now What? Branch Optimization

February 6, 2018


The 1802 has two types of branch operations: short branches can only branch within a page(the top byte of the program counter stays the same); and long branches which can go anywhere in memory.

The long branches are slower and they disturb some video timing so programmers traditionally avoid them but the C compiler output is littered with them because I just have no control over where things will be in memory.

I had this idea a while ago that i could run over the code after the fact substituting short branches where they would work. It’s not quite as simple as figuring out where each branch is going because the act of shortening branches will move things. Anyway, I made a stab at processing the compiler output with python a while ago but it just got to seem too hard because the compiler generates the branches inside macros like “JNZU2” which generate multiple instructions including sometimes multiple branches. I went back at it yesterday using the .i output from the assembler which has all the macros expanded to ordinary instructions. As of this morning I can process the output of compiling “blink.c” which generates 1100 lines of assembler instructions (because it has the olduino library included) and my python program is assigning the correct locations to the labels.

Now I just have to remember the rest of my idea of how to convert the branches. Also, there are a number of tricky bits left such as the reuse of temporary labels like $$xx and + and -.

The code is way beyond awful but it’s not super-long so if i can figure out what i’m doing I’ll rewrite it completely.

It’s a bit interesting to note that in the 1100 lines resulting from blink there are only 40-ish branches but over 200 labels. Probably 20 or 30 of the labels are register equates or similar and a lot of the rest must be function or data labels that would never be ref’d in branches.

{'bnz': 14, 'lbr': 5, 'br': 8, 'b3': 1, 'bn3': 0, 'lbnz': 2, 
'lbz': 0, 'bnf': 12, 'bz': 6}

{'_setqOff': 213, '$$bitloop': 1208, '_olduinoincluder': 106, 
'_digitalWrite': 128, '$$cknext': 1287, '_call': 241, 
'$$ney0y': 628, 'SUBOVHD': 272, '_inp': 1349, '_delay': 45, 
'rwork': 177, '$$norestore': 861, '_modI2': 223, '_modI4': 1310, 
'R4': 0, 'R5': 0, 'R6': 0, 'R7': 0, 'R0': 0, 'R1': 0, 'R2': 0, 
'R3': 0, '_dubdabx': 1143, 'R8': 0, 'R9': 0, '$$setbitoff': 151, 
'$$_00000': 203, '_mulU2': 512, '_setqOn': 211, '$$mulrshft': 540, 
'$$upnxt': 1264, '_main': 6, '_mulu4': 870, 'retVal': 0, 
'memAddr': 0, 'L12': 176, '$$loop': 783, '$$mulrlp': 516, 
'_out': 1375, '_divu2': 549, '_divu4': 768, '$$done': 1297, 
'R14': 0, 'R15': 0, 'R12': 0, 'R13': 0, 'R10': 0, 'R11': 0, 
'+': 1392, 'Rt1': 0, 'Rt2': 0, '$$clrlp': 1174, '$$dnoadd': 1219, 
'$$_die': 206, '$$again': 581, 'L4': 13, '$$mulrdone': 548, '
_PIN4': 3, '_digitalRead': 168, 'RL8': 0, 'RL6': 0, 'RL0': 0, 
'_out4': 231, '_out5': 1344, 'RL10': 0, 'RL12': 0, '$$mulrnoadd': 534, '$$testexit': 618, '_oneMs': 272, '$$check_sub': 918, '$$dcklp': 1211, 
'L11': 74, '$$mloop': 887, '$$pos1': 1062, '$$pos2': 1101, 
'$$bvtable': 159, '_modU4': 1298, '_return': 257, 'SP': 0, 
'_modU2': 215, '$$cktop': 1189, '$$endlp': 865, '_putc': 1344, 
'RCALL': 0, '$$shift': 942, '$$dshlp': 1239, 'RPC': 0, 
'lcc1802init': 177, '_divI4': 1024, '_divI2': 651, '$$out': 642, '$$computequot': 603, '$$mslp': 274, 'L7': 92, '_mysetqOn': 4, 
'L5': 37, 'L3': 40, 'L1': 5, '$$outit': 156, 'L8': 70, 'L9': 73, 
'retAddr': 0, 'regArg1': 0, 'RRET': 0, 'Rp1p2': 0, 'regArg2': 0}
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: