assembly - Why does conditional branching in ASM 6502 have limit of 128 bytes -


what hardware reasons why routine must within 128 or -127 bytes of issued branching instruction?

the hardware reasons two-fold:

  • first, 6502 8-bit processor, means single byte can hold unsigned values 0 255, or if bit 7 used indicate sign (two's complement) -128 +127.

  • second, chuck peddle designed branch instruction two-byte operation - first byte represents branch condition (opcode) , second signed offset value (operand) added program counter [pc] if condition true.

as apparent, use of single signed offset byte branch operand means maximum 'jump' range brx instruction can accommodate either 128 locations current pc, or 127 locations ahead.

this limitation can cumbersome overcome in cases need branch (as opposed jump, see below) further range allows; however, practice , experience in 6502 assembly programming techniques , deep understanding of flow , organisation of code permit artful design avoids need branch on larger distances.

the cpu architecture more compensates range limit having brx incredibly fast instruction - 2 cycles if branch not taken, , 1 more cycle if (i.e. 2 cycles read opcode , operand , set compare mask in internal register, , 1 more add operand pc if condition true).

the jmp instruction, comparison, allows program flow jump anywhere in 16-bit address range uses two-byte address operand - exhibits flat 3-cycle cost , unconditional. careful construction of jump table or self-modifying code using jmp , indexed branch allows programmer capitalise on speed , conditionality of brx , incur 'far jump' cost when condition true.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -