<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>8-bit MicrocontrollersのトピックAdding a value to the Program Counter</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Adding-a-value-to-the-Program-Counter/m-p/124096#M164</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;SPAN style="color: #ff0000;"&gt;This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Aug 19, 2005 3:48 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi,&lt;BR /&gt;&lt;BR /&gt; We are translating some PIC assembly code to 08. We need to add a value in the low nybble after a mask operation to the PC. The resultant value will then increment the PC to the correct location. Here is an example...&lt;BR /&gt;&lt;BR /&gt; lda DATA&lt;BR /&gt; and #$0F ;and with mask to enable low nibble&lt;BR /&gt; add PCL ;add resultant value to low byte of program counter&lt;BR /&gt;&lt;BR /&gt; The PC would immediatly go to one of the branch locations below.&lt;BR /&gt;&lt;BR /&gt; bra ERROR_1&lt;BR /&gt; bra ERROR_2&lt;BR /&gt; bra ERROR_3&lt;BR /&gt; bra ERROR_4&lt;BR /&gt; bra ERROR_5&lt;BR /&gt; bra ERROR_6&lt;BR /&gt;&lt;BR /&gt; This routine is very timing critical so I cannot use a CBEQA to compare a incrementing value as the execution time would vary. Any suggestions?&lt;BR /&gt;&lt;BR /&gt; &lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Aug 19, 2005 8:06 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi,&lt;BR /&gt;&lt;BR /&gt; why don´t you use an indexed jump ?.&lt;BR /&gt; This is a clean solution and has one extra cycle in the worst case.&lt;BR /&gt;&lt;BR /&gt; &lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Aug 19, 2005 9:15 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi&lt;BR /&gt;&lt;BR /&gt;first i tought on a routin to calculate the new PC with some stack an HX calculating. But Eckhard is rigth here the code (i tested it with the EBS08_RC8 template):&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! INDEX_JUMP: LDHX #ERROR_1X ;Error table in HX STHX POINTER ;for calculating LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 ADD POINTER+1 ;calculate the new PC value STA POINTER+1 LDA POINTER ADC #0 STA POINTER LDHX POINTER ;Adress in HX JMP ,X ;JUMP to the adress ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE:&lt;/PRE&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Sun Aug 21, 2005 10:59 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi&lt;BR /&gt;&lt;BR /&gt; I have been thinking about solutions for the problem!&lt;BR /&gt;&lt;BR /&gt; Here some of them:&lt;BR /&gt;&lt;BR /&gt; First try how Paul51 askd for:&lt;BR /&gt;&lt;BR /&gt; Erste Lösung mit berechnen des neuen Programm-Counters:&lt;BR /&gt;&lt;BR /&gt; This solution changes the PC; with the RTS command the programm continues&lt;BR /&gt; with the new PC value.&lt;BR /&gt;&lt;BR /&gt; Is uses 57 cycles to preform the operation!&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;**** PCTEST PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start JSR PC_CHANGE PC_CHANGE: LDHX #ERROR_1X ;Error table in HX STHX POINTER ;for calculating LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 ADD POINTER+1 ;calculate the new PC value STA POINTER+1 LDA POINTER ADC #0 STA POINTER LDHX POINTER PULA ;to move the stack pointer to PULA ;the right position PSHX ;to put the new PC value onto the stack PSHH RTS ;now we jump to the right address!!!! ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;Second solution after the tip&lt;BR /&gt;&lt;BR /&gt;2. Lösung mit indiziertem Sprung und 16bit Berechnung:&lt;BR /&gt;&lt;BR /&gt;A fast shot (modifing the first solution)&lt;BR /&gt;&lt;BR /&gt;It uses 43 cycles (14 less than the first sulution)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;**** PCTEST PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #ERROR_1X ;Error table in HX STHX POINTER ;for calculating LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 ADD POINTER+1 ;calculate the new PC value STA POINTER+1 LDA POINTER ADC #0 STA POINTER LDHX POINTER ;Adress in HX JMP ,X ;JUMP to the adress ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;Ok but we can make it faster by using only a 8 bit value so here&lt;BR /&gt;the third try:&lt;BR /&gt;&lt;BR /&gt;It uses 20 cycles (much faster)&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #0 ;Clear HX LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 TAX ;Accu -&amp;gt; X (offset to ERROR_1X) ; JMP ERROR_1X,X ;JUMP to the adress ERROR_1X + offset ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;To save more cycles we can eliminate the SUB #1 (DECA would be also a solution but)&lt;BR /&gt;when change the offset to ERROR_1X-2 we can save 2 cycles&lt;BR /&gt;&lt;BR /&gt;This solution uses 18 cycles now!&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #0 ;Clear HX LDA TMPA ;your variable DATA AND #$0F LSLA ;Offset *2 TAX ;Accu -&amp;gt; X (offset to ERROR_1X) ; JMP ERROR_1X-2,X ;JUMP to the adress ERROR_1X - 2 + offset ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;A othe way to do it with a table. Unfortunatly we can not&lt;BR /&gt;load the HX register indexed with X so we have to load the POINTER variable!&lt;BR /&gt;&lt;BR /&gt;it uses 32 cycles an jumps directly to the desired address!&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #0 ;Clear HX LDA TMPA ;your variable DATA AND #$0F LSLA ;Offset *2 TAX ;Accu -&amp;gt; X (offset to ERROR_1X) LDA ERROR_1X-2,X ;Load the Pointer with the STA POINTER ;desired error address LDA ERROR_1X-1,X STA POINTER+1 LDHX POINTER ;Pointer to HX JMP ,X ;and Jump to the address ERROR_1X: DC.W ERROR_1 ;Error routine table ERROR_2X: DC.W ERROR_2 ERROR_3X: DC.W ERROR_3 ERROR_4X: DC.W ERROR_4 ERROR_5X: DC.W ERROR_5 ERROR_6X: DC.W ERROR_6 ERROR_7X: DC.W ERROR_7 ERROR_8X: DC.W ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;I hope that helps&lt;BR /&gt;&lt;BR /&gt;Saluti&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Thu Dec 15, 2005 9:05 pm&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi,&lt;BR /&gt;&lt;BR /&gt; Can some one give me an example of an indexed jump? This seems to be the best solution but I have only used the X register in the past with 05 parts. Do not know how to use the HX register.&lt;BR /&gt;&lt;BR /&gt; Thanks to everyone for all of your help.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Dec 16, 2005 1:50 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;G'day,&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="j-rte-table"&gt;&lt;TABLE align="center" border="0" cellpadding="3" cellspacing="1" width="90%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD style="border:0px solid black;"&gt;&lt;SPAN class="genmed"&gt;&lt;STRONG&gt;Quote:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="quote" style="border:0px solid black;"&gt;Can some one give me an example of an indexed jump? This seems to be the best solution but I have only used the X register in the past with 05 parts. Do not know how to use the HX register.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="postbody"&gt;&lt;/SPAN&gt;&lt;P&gt;&lt;BR /&gt;Here is a simple example of an indexed jump that will work with both 05 and 08 parts (in the latter case, the H register needs to be set to zero). It is assumed that the DATA register would contain the value 0, 1, ... etc. There is no protection against going beyond the end of the table.&lt;BR /&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="j-rte-table"&gt;&lt;TABLE align="center" border="0" cellpadding="3" cellspacing="1" width="90%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD style="border:0px solid black;"&gt;&lt;P&gt;&lt;SPAN class="genmed"&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="genmed"&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;LDX&amp;nbsp; DATA LSLX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; Since BRA instruction uses 2 bytes CLRH JMP&amp;nbsp; TABLE,X TABLE: BRA&amp;nbsp; ERROR_1 BRA&amp;nbsp; ERROR_2 etc. &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;'TABLE' can be located anywhere in memory, but needs to be within relative jump range of 'ERROR_1', etc. If you needed to do other things following the execution of one of the error routines, you could substitute JSR TABLE,X for the JMP instruction.&lt;BR /&gt;&lt;BR /&gt;I hope this clarifies the situation.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 25 Jan 2006 05:12:59 GMT</pubDate>
    <dc:creator>RChapman</dc:creator>
    <dc:date>2006-01-25T05:12:59Z</dc:date>
    <item>
      <title>Adding a value to the Program Counter</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Adding-a-value-to-the-Program-Counter/m-p/124096#M164</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;SPAN style="color: #ff0000;"&gt;This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Aug 19, 2005 3:48 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi,&lt;BR /&gt;&lt;BR /&gt; We are translating some PIC assembly code to 08. We need to add a value in the low nybble after a mask operation to the PC. The resultant value will then increment the PC to the correct location. Here is an example...&lt;BR /&gt;&lt;BR /&gt; lda DATA&lt;BR /&gt; and #$0F ;and with mask to enable low nibble&lt;BR /&gt; add PCL ;add resultant value to low byte of program counter&lt;BR /&gt;&lt;BR /&gt; The PC would immediatly go to one of the branch locations below.&lt;BR /&gt;&lt;BR /&gt; bra ERROR_1&lt;BR /&gt; bra ERROR_2&lt;BR /&gt; bra ERROR_3&lt;BR /&gt; bra ERROR_4&lt;BR /&gt; bra ERROR_5&lt;BR /&gt; bra ERROR_6&lt;BR /&gt;&lt;BR /&gt; This routine is very timing critical so I cannot use a CBEQA to compare a incrementing value as the execution time would vary. Any suggestions?&lt;BR /&gt;&lt;BR /&gt; &lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Aug 19, 2005 8:06 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi,&lt;BR /&gt;&lt;BR /&gt; why don´t you use an indexed jump ?.&lt;BR /&gt; This is a clean solution and has one extra cycle in the worst case.&lt;BR /&gt;&lt;BR /&gt; &lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Aug 19, 2005 9:15 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi&lt;BR /&gt;&lt;BR /&gt;first i tought on a routin to calculate the new PC with some stack an HX calculating. But Eckhard is rigth here the code (i tested it with the EBS08_RC8 template):&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! INDEX_JUMP: LDHX #ERROR_1X ;Error table in HX STHX POINTER ;for calculating LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 ADD POINTER+1 ;calculate the new PC value STA POINTER+1 LDA POINTER ADC #0 STA POINTER LDHX POINTER ;Adress in HX JMP ,X ;JUMP to the adress ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE:&lt;/PRE&gt;&lt;DIV&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Sun Aug 21, 2005 10:59 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi&lt;BR /&gt;&lt;BR /&gt; I have been thinking about solutions for the problem!&lt;BR /&gt;&lt;BR /&gt; Here some of them:&lt;BR /&gt;&lt;BR /&gt; First try how Paul51 askd for:&lt;BR /&gt;&lt;BR /&gt; Erste Lösung mit berechnen des neuen Programm-Counters:&lt;BR /&gt;&lt;BR /&gt; This solution changes the PC; with the RTS command the programm continues&lt;BR /&gt; with the new PC value.&lt;BR /&gt;&lt;BR /&gt; Is uses 57 cycles to preform the operation!&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;**** PCTEST PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start JSR PC_CHANGE PC_CHANGE: LDHX #ERROR_1X ;Error table in HX STHX POINTER ;for calculating LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 ADD POINTER+1 ;calculate the new PC value STA POINTER+1 LDA POINTER ADC #0 STA POINTER LDHX POINTER PULA ;to move the stack pointer to PULA ;the right position PSHX ;to put the new PC value onto the stack PSHH RTS ;now we jump to the right address!!!! ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;Second solution after the tip&lt;BR /&gt;&lt;BR /&gt;2. Lösung mit indiziertem Sprung und 16bit Berechnung:&lt;BR /&gt;&lt;BR /&gt;A fast shot (modifing the first solution)&lt;BR /&gt;&lt;BR /&gt;It uses 43 cycles (14 less than the first sulution)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;**** PCTEST PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #ERROR_1X ;Error table in HX STHX POINTER ;for calculating LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 ADD POINTER+1 ;calculate the new PC value STA POINTER+1 LDA POINTER ADC #0 STA POINTER LDHX POINTER ;Adress in HX JMP ,X ;JUMP to the adress ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;Ok but we can make it faster by using only a 8 bit value so here&lt;BR /&gt;the third try:&lt;BR /&gt;&lt;BR /&gt;It uses 20 cycles (much faster)&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #0 ;Clear HX LDA TMPA ;your variable DATA AND #$0F SUB #1 ;for offsetError_1 is 0! LSLA ;Offset *2 TAX ;Accu -&amp;gt; X (offset to ERROR_1X) ; JMP ERROR_1X,X ;JUMP to the adress ERROR_1X + offset ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;To save more cycles we can eliminate the SUB #1 (DECA would be also a solution but)&lt;BR /&gt;when change the offset to ERROR_1X-2 we can save 2 cycles&lt;BR /&gt;&lt;BR /&gt;This solution uses 18 cycles now!&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #0 ;Clear HX LDA TMPA ;your variable DATA AND #$0F LSLA ;Offset *2 TAX ;Accu -&amp;gt; X (offset to ERROR_1X) ; JMP ERROR_1X-2,X ;JUMP to the adress ERROR_1X - 2 + offset ERROR_1X: ;Error routine table BRA ERROR_1 ERROR_2X: BRA ERROR_2 ERROR_3X: BRA ERROR_3 ERROR_4X: BRA ERROR_4 ERROR_5X: BRA ERROR_5 ERROR_6X: BRA ERROR_6 ERROR_7X: BRA ERROR_7 ERROR_8X: BRA ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;A othe way to do it with a table. Unfortunatly we can not&lt;BR /&gt;load the HX register indexed with X so we have to load the POINTER variable!&lt;BR /&gt;&lt;BR /&gt;it uses 32 cycles an jumps directly to the desired address!&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;PC_TEST: LDA #1 ;for test only STA TMPA ;your variable DATA! * Start INDEX_JUMP: LDHX #0 ;Clear HX LDA TMPA ;your variable DATA AND #$0F LSLA ;Offset *2 TAX ;Accu -&amp;gt; X (offset to ERROR_1X) LDA ERROR_1X-2,X ;Load the Pointer with the STA POINTER ;desired error address LDA ERROR_1X-1,X STA POINTER+1 LDHX POINTER ;Pointer to HX JMP ,X ;and Jump to the address ERROR_1X: DC.W ERROR_1 ;Error routine table ERROR_2X: DC.W ERROR_2 ERROR_3X: DC.W ERROR_3 ERROR_4X: DC.W ERROR_4 ERROR_5X: DC.W ERROR_5 ERROR_6X: DC.W ERROR_6 ERROR_7X: DC.W ERROR_7 ERROR_8X: DC.W ERROR_8 ERROR_1: JMP PC_TESTE ERROR_2: JMP PC_TESTE ERROR_3: JMP PC_TESTE ERROR_4: JMP PC_TESTE ERROR_5: JMP PC_TESTE ERROR_6: JMP PC_TESTE ERROR_7: JMP PC_TESTE ERROR_8: PC_TESTE: &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;I hope that helps&lt;BR /&gt;&lt;BR /&gt;Saluti&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Thu Dec 15, 2005 9:05 pm&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;Hi,&lt;BR /&gt;&lt;BR /&gt; Can some one give me an example of an indexed jump? This seems to be the best solution but I have only used the X register in the past with 05 parts. Do not know how to use the HX register.&lt;BR /&gt;&lt;BR /&gt; Thanks to everyone for all of your help.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;STRONG&gt;Posted: Fri Dec 16, 2005 1:50 am&lt;SPAN class="gen"&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;&lt;SPAN class="postdetails"&gt;&lt;SPAN class="postbody"&gt;G'day,&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="j-rte-table"&gt;&lt;TABLE align="center" border="0" cellpadding="3" cellspacing="1" width="90%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD style="border:0px solid black;"&gt;&lt;SPAN class="genmed"&gt;&lt;STRONG&gt;Quote:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="quote" style="border:0px solid black;"&gt;Can some one give me an example of an indexed jump? This seems to be the best solution but I have only used the X register in the past with 05 parts. Do not know how to use the HX register.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN class="postbody"&gt;&lt;/SPAN&gt;&lt;P&gt;&lt;BR /&gt;Here is a simple example of an indexed jump that will work with both 05 and 08 parts (in the latter case, the H register needs to be set to zero). It is assumed that the DATA register would contain the value 0, 1, ... etc. There is no protection against going beyond the end of the table.&lt;BR /&gt;&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="j-rte-table"&gt;&lt;TABLE align="center" border="0" cellpadding="3" cellspacing="1" width="90%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD style="border:0px solid black;"&gt;&lt;P&gt;&lt;SPAN class="genmed"&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="genmed"&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;LDX&amp;nbsp; DATA LSLX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; Since BRA instruction uses 2 bytes CLRH JMP&amp;nbsp; TABLE,X TABLE: BRA&amp;nbsp; ERROR_1 BRA&amp;nbsp; ERROR_2 etc. &lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;P&gt;'TABLE' can be located anywhere in memory, but needs to be within relative jump range of 'ERROR_1', etc. If you needed to do other things following the execution of one of the error routines, you could substitute JSR TABLE,X for the JMP instruction.&lt;BR /&gt;&lt;BR /&gt;I hope this clarifies the situation.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 25 Jan 2006 05:12:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Adding-a-value-to-the-Program-Counter/m-p/124096#M164</guid>
      <dc:creator>RChapman</dc:creator>
      <dc:date>2006-01-25T05:12:59Z</dc:date>
    </item>
  </channel>
</rss>

