EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Machine code relative offsets

Started by AlD October 21, 2009
I am trying to understand how assembly is translated into machine code. I understand how to get the machine coding formats but I'm not sure how to calculate 'rr' in all cases. For the example below 'bra' is 20 rr or 20 03, I know that the relative offset is 03 (to get past the two bytes required by the 'inc' instruction)to get to the 'iny' instruction.

But I'm not sure why the rr = F3 for the 'dbne' instruction, how does that relative offset get me to the Again label?
Dissassembled code for program:
[CODE] org $600 ldy #Array ;CD B6 15 ldaa #N ;86 07 clr 7,y ;69 47 Again brclr 0,y %11110000, Not_gtr ;0F 40 F0 02 bra Update ;20 03 Not_gtr inc Count ;72 B6 1C Update iny ;02 dbne a,Again ;04 30 F3 swi ; Array fdb 8,7,2 N equ 7 Count rmb 1[/CODE]

rrin dbne has most significant bit set, which means rr displacement is
negative, F3 = -0D. 13 bytes from Again label to location after dbne (swi
instruction).

The rest can be found in CPU12/(S)/(SX) reference manuals.

Edward
----- Original Message -----
From: "AlD"
To: <6...>
Sent: Wednesday, October 21, 2009 11:07
Subject: [68HC12] Machine code relative offsets
>I am trying to understand how assembly is translated into machine code. I
>understand how to get the machine coding formats but I'm not sure how to
>calculate 'rr' in all cases. For the example below 'bra' is 20 rr or 20 03,
>I know that the relative offset is 03 (to get past the two bytes required
>by the 'inc' instruction)to get to the 'iny' instruction.
>
> But I'm not sure why the rr = F3 for the 'dbne' instruction, how does that
> relative offset get me to the Again label?
> Dissassembled code for program:
> [CODE] org $600 > ldy #Array ;CD B6 15 > ldaa #N ;86 07 > clr 7,y ;69 47 > Again brclr 0,y %11110000, Not_gtr ;0F 40 F0 02 > bra Update ;20 03 > Not_gtr inc Count ;72 B6 1C > Update iny ;02 > dbne a,Again ;04 30 F3 > swi ; > > Array fdb 8,7,2 > N equ 7 > Count rmb 1[/CODE]
>
>
The inc instruction is 3 bytes. Yes, rr = F3 for the dbne instruction is correct.

A formula which might help: PCnew = PCbranch + len(branch) + rr

Or learn to count forwards and backwards in hexadecimal with zero being the first byte of the instruction after the branch.

Emmett Redd Ph.D. mailto:E...@missouristate.edu
Professor (417)836-5221
Department of Physics, Astronomy, and Materials Science
Missouri State University Fax (417)836-6226
901 SOUTH NATIONAL Lab (417)836-3770
SPRINGFIELD, MO 65897 USA Dept (417)836-5131

"In theory there is no difference between theory and practice. In practice there is." -- Yogi Berra or Jan van de Snepscheut

________________________________________
From: 6... [6...] On Behalf Of AlD [a...@cox.net]
Sent: Wednesday, October 21, 2009 3:07 AM
To: 6...
Subject: [68HC12] Machine code relative offsets

I am trying to understand how assembly is translated into machine code. I understand how to get the machine coding formats but I'm not sure how to calculate 'rr' in all cases. For the example below 'bra' is 20 rr or 20 03, I know that the relative offset is 03 (to get past the two bytes required by the 'inc' instruction)to get to the 'iny' instruction.

But I'm not sure why the rr = F3 for the 'dbne' instruction, how does that relative offset get me to the Again label?
Dissassembled code for program:
[CODE] org $600 ldy #Array ;CD B6 15 ldaa #N ;86 07 clr 7,y ;69 47 Again brclr 0,y %11110000, Not_gtr ;0F 40 F0 02 bra Update ;20 03 Not_gtr inc Count ;72 B6 1C Update iny ;02 dbne a,Again ;04 30 F3 swi ; Array fdb 8,7,2 N equ 7 Count rmb 1[/CODE]



Thanks, do I include the byte(s) for the instruction after the dbne, in this case for the swi instruction or stop with the 3 bytes of the dbne?

--- In 6..., "Edward Karpicz" wrote:
>
> rrin dbne has most significant bit set, which means rr displacement is
> negative, F3 = -0D. 13 bytes from Again label to location after dbne (swi
> instruction).
>
> The rest can be found in CPU12/(S)/(SX) reference manuals.
>
> Edward
> ----- Original Message -----
> From: "AlD"
> To: <6...>
> Sent: Wednesday, October 21, 2009 11:07
> Subject: [68HC12] Machine code relative offsets
> >I am trying to understand how assembly is translated into machine code. I
> >understand how to get the machine coding formats but I'm not sure how to
> >calculate 'rr' in all cases. For the example below 'bra' is 20 rr or 20 03,
> >I know that the relative offset is 03 (to get past the two bytes required
> >by the 'inc' instruction)to get to the 'iny' instruction.
> >
> > But I'm not sure why the rr = F3 for the 'dbne' instruction, how does that
> > relative offset get me to the Again label?
> >
> >
> > Dissassembled code for program:
> > [CODE] org $600 > > ldy #Array ;CD B6 15 > > ldaa #N ;86 07 > > clr 7,y ;69 47 > > Again brclr 0,y %11110000, Not_gtr ;0F 40 F0 02 > > bra Update ;20 03 > > Not_gtr inc Count ;72 B6 1C > > Update iny ;02 > > dbne a,Again ;04 30 F3 > > swi ; > > > > Array fdb 8,7,2 > > N equ 7 > > Count rmb 1[/CODE]
> >
> >
> >
> >
> >
> >
No, rr is relative to the address of instruction (or address of the first
byte of instruction) that follows immediately after dbne. If we add label
Next like this:

>> > Again brclr 0,y %11110000, Not_gtr ;0F 40 F0 02
>> > bra Update ;20 03
>> > Not_gtr inc Count ;72 B6 1C
>> > Update iny ;02
>> > dbne a,Again ;04 30 F3 <--- rr = $F3
>> > = -$0D
Next
>> > swi ;

, then rr will equal Next - Again. There are thirteen bytes between Again
and Next. rr is negative because target label is above the 'next' (Again is
at more lower address than Next).

Edward

----- Original Message -----
From: "AlD"
To: <6...>
Sent: Wednesday, October 21, 2009 20:39
Subject: [68HC12] Re: Machine code relative offsets
> Thanks, do I include the byte(s) for the instruction after the dbne, in
> this case for the swi instruction or stop with the 3 bytes of the dbne?
>
> --- In 6..., "Edward Karpicz" wrote:
>>
>> rrin dbne has most significant bit set, which means rr displacement
>> is
>> negative, F3 = -0D. 13 bytes from Again label to location after dbne (swi
>> instruction).
>>
>> The rest can be found in CPU12/(S)/(SX) reference manuals.
>>
>> Edward
>> ----- Original Message -----
>> From: "AlD"
>> To: <6...>
>> Sent: Wednesday, October 21, 2009 11:07
>> Subject: [68HC12] Machine code relative offsets
>> >I am trying to understand how assembly is translated into machine code.
>> >I
>> >understand how to get the machine coding formats but I'm not sure how to
>> >calculate 'rr' in all cases. For the example below 'bra' is 20 rr or 20
>> >03,
>> >I know that the relative offset is 03 (to get past the two bytes
>> >required
>> >by the 'inc' instruction)to get to the 'iny' instruction.
>> >
>> > But I'm not sure why the rr = F3 for the 'dbne' instruction, how does
>> > that
>> > relative offset get me to the Again label?
>> >
>> >
>> > Dissassembled code for program:
>> > [CODE] org $600 >> > ldy #Array ;CD B6 15 >> > ldaa #N ;86 07 >> > clr 7,y ;69 47 >> > Again brclr 0,y %11110000, Not_gtr ;0F 40 F0 02 >> > bra Update ;20 03 >> > Not_gtr inc Count ;72 B6 1C >> > Update iny ;02 >> > dbne a,Again ;04 30 F3 >> > swi ; >> > >> > Array fdb 8,7,2 >> > N equ 7 >> > Count rmb 1[/CODE]
>> >
>> >
>> >
>> >
>> >
>> >

Memfault Beyond the Launch