Skip to main content
Visitor II
January 1, 2009
Question

Assembler code

  • January 1, 2009
  • 4 replies
  • 1205 views
Posted on January 01, 2010 at 08:30

Assembler code

    This topic has been closed for replies.

    4 replies

    abelmegAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:06

    I'm using the STM8s for my application on motor control and here is the code I am confused about.

    ld A,_Average_Zero_Cross_Time

    ld XL,A

    ld A,_tmp_u8

    mul X,A

    ldw _tmp_u16,X

    ld A,_Average_Zero_Cross_Time + 1

    ld XL,A

    ld A,_tmp_u8

    mul X,A

    ld A,XH

    clrw X

    ld XL,A

    addw X,_tmp_u16

    ldw _Demag_Time,X

    It tries to do: Demag_Time = (Average_Zero_Cross_Time * tmp_u8) >> 8;

    Can't I do it by using the code below:

    ld A,_Average_Zero_Cross_Time

    ld XL,A

    ld A,_tmp_u8

    mul X,A

    ld A,XH

    clrw X

    ld XL,A

    ldw _Demag_Time,X

    regards...

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:06

    abelmeg,

    if Average_Zero_Cross_Time is a two byte quantity, its product by tmp_u8 (which I suppose is an eight bit quantity) is a three byte quantity.

    The most significant byte of this product is:

    HIGH_BYTE( HIGH_BYTE(_Average_Zero_Cross_Time) * _tmp_u8)

    The middle byte is:

    LOW_BYTE( HIGH_BYTE(_Average_Zero_Cross_Time) * _tmp_u8) + HIGH_BYTE( LOW_BYTE(_Average_Zero_Cross_Time) * _tmp_u8)

    Finally the least significant byte of this product is:

    LOW_BYTE( LOW_BYTE(_Average_Zero_Cross_Time) * _tmp_u8)

    So, only your first code is correct.

    _tmp_u16 isn't however needed, since it can be replaced with _Demag_Time.

    Happy new year!

    EtaPhi

    abelmegAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:06

    Dear EtaPhi,

    First of all I would like to thank you for your reply, But Average_Zero_Cross_Time is only an 8-bit quantity and the result of multiplication would be in the 16-bit index register X.

    Regards..

    abelmegAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 15:06

    I got it now! I didn't consider the addition(+1) as address.

    thank you and have a prosperous new year!