Skip to main content
Visitor II
July 7, 2018
Question

Issues(?) with command ADDW and SUBW on STM8S105C6

  • July 7, 2018
  • 2 replies
  • 1338 views
Posted on July 07, 2018 at 08:08

Hi

I use STM8S-Discovery Board with STM8S105C6 and ST Visual Develop v4.3.10, Windows 10.

If I use command ADDW or SUBW this way:

------------------------------------------------------------------------------

Example 1

                  ldw           X,&sharp400

         ;Load 400 into X

                  addw       X,&sharp100

          ;Add direct 100 to word X

test            cpw         X,&sharp500

         ;Compare X with 500

             

      jreq         test

               ;Jump to 'test' if X=500

------------------------------------------------------------------------------

it works properly. The content X is 500.

But if I do this

------------------------------------------------------------------------------

Example 2

k                equ           $0365

        ;RAM register is called k

                  mov          k,&sharp100

       ;Move 100 into k

                  ldw           X,&sharp400

       ;Load 400 into X

                  addw        X,k

             ;Add k to word X

test            cpw         X,&sharp500

       ;Compare X with 500

             

      jreq         test

             ;Jump to 'test' if X=500

------------------------------------------------------------------------------

it doesn�t work. The content of X is not 500.

SUBW behave the same way.

According the programming manual PM004 Doc ID 13590 Rev3, page 78 and 152,

Example 2 should actually work. Issue or do I miss something?

Thanks for reply.

Ivan 

#addw #subw #stm8s105c6 #assembly #basic-arithmetic
    This topic has been closed for replies.

    2 replies

    Graduate II
    July 7, 2018
    Posted on July 07, 2018 at 17:45

    Dupe

    https://community.st.com/0D50X00009XkVvrSAF

     

    Doesn't seem to be getting much traction, options would be to contact local ST office, FAE for your account, or post an Online Support Request.

    Visitor II
    July 8, 2018
    Posted on July 08, 2018 at 13:46

    Thanks anyway.

    Visitor II
    July 13, 2018
    Posted on July 13, 2018 at 10:01

    I think the problem is that the

    instruction

     'ADDW' is word 

    instruction,

    and the variable 'k' is byte. In this example, on the $0365 address is #100=#$64 number. When ADDW X, k is executed, 'k' loaded  as #$64xx. 'xx' means we do not know what the number is at $0366 adress.

    One repair option:

    k equ $0365      ; the RAM register is called k 

       ldw X, # 100   ; Move 100 to X

       ldw k,X            ; move 100 to k address

       ldw X, # 400   ; Load 400 in X

       addw X, k         ; Add k to word X

    test   cpw X, # 500; Compare X with 500 

       jreq test            ; Jump to 'test' if X = 500

    Visitor II
    July 16, 2018
    Posted on July 16, 2018 at 09:28

     ,

     ,

    Thanks for reply.

    It seems I didn´t understood the loading mechanism with word and byte.

    I thought k is added as byte to the lower part (XL) of X which acctualy is a byte too.

    If I clear address $0366 first then k is loaded as ♯ $6400 when ADDW is executed. ,

    ADDW X,k would then lead to X= ♯ 26000 ( ♯ $0190 + ♯ $6400 = ♯ $6590)

    LDW  , , , , , ,X, ♯ 100 , , , ,,X loaded with ♯ $0064

    LDW  , , , , , ,k,X  , , , , , , , , ,,leads to address $0365 with content ♯ $00 and $0366 with ♯ $64

    LDW , , , , , ,X, ♯ 400 , , , , , ,,X loaded with ♯ $0190

    ADDW , , , ,X,k , , , , , , , , , , , ,, ♯ $0190 + ♯ $0064 = ♯ $01F4 = ♯ 500

    It works now.

    Thanks!