Forum: µC & Digital Electronics subtract in assembler different than C? (dsPic)

Author: Dieter (Guest)
Posted on:

Rate this post
0 useful
not useful
Hi Guys,

Seems like a stupid question, but I'm facing a strange problem: The 
sub.w opcode doesn't seem to subtract like expected. The C-code (see 
below) works fine, but when I express the same thing in asm, it doesn't 
work, the results are completely wrong. May it be that I have to test 
the operands of the subtraction for their respective sign and then need 
to add/sub accordingly??? All variables are 16-bit signed integers.

Following C lines should be expressed in assembler:
x_n_re[b_index] = x_n_re[a_index]-resultMulReCos+resultMulImSin;
x_n_re[a_index] = x_n_re[a_index]+resultMulReCos-resultMulImSin;
x_n_im[b_index] = x_n_im[a_index]-resultMulReSin-resultMulImCos;
x_n_im[a_index] = x_n_im[a_index]+resultMulReSin+resultMulImCos;

which looks like this when I write it in asm:
(a_index and b_index are passed as array indices[3] and indices[2] to 
the asm-function (indices[0] -> w0), &x_n_re[0] and &x_n_im[0] are 
passed as array into w2)

w1,w3,w4,w7 are being loaded with the (correct) values of ReCos, ImCos, 
ReSin and ImSin before this.
  mov.w [w0+0x4], w12    ; b_index -> w12
  sl     w12,#1,w12    ; b_index *= 2 for 16-bit increment

  mov.w [w0+0x6], w9    ; a_index -> w9
  sl     w9,#1,w9    ; a_index *= 2 for 16-bit increment

  mov.w [w2], w5      ; &x_n_re[0] -> w5
  mov.w [w5+w9], w8    ; x_n_re[a_index] -> w8

  add w1,w4,w6       ; ReCos+ImSin
  sub w8,w6,w6       ; w6=x_n_re[a_index]-recos+imsin
  mov w6,[w5+w12]     ; x_n_re[b_index]=w6-recos+imsin

  sub w1,w4,w6       ; ReCos-ImSin
  add w8,w6,w6       ; w6 = x_n_re[a_index]+recos-imsin
  mov w6, [w5+w9]     ; x_n_re[a_index]=w6+recos-imsin

  mov.w [w2+0x2], w5    ; &x_n_im[0] -> w5
  mov.w [w5+w9], w8    ; x_n_im[a_index] -> w8

  sub w7,w3,w6       ; w6=ReSin-ImCos
  sub w8,w6,w6       ; w6=x_n_im[a_index]-resin-imcos
  mov w6, [w5+w9]     ; x_n_im[b_index]=w6-resin-imcos

  add w7,w3,w6       ; w6=ReSin+ImCos
  add w8,w6,w6       ; w6=x_n_im[a_index]+resin+imcos
  mov w6, [w5+w9]      ; x_n_im[a_index]=w6+resin+imcos

Am I missing something?

Thanks a lot!

Author: Michael (Guest)
Posted on:

Rate this post
0 useful
not useful
What about your Carry-flag?

Author: Dieter (Guest)
Posted on:

Rate this post
0 useful
not useful
Yeah, that's the point, I don't care about it, yet. Do I need to? I 
thought after the subtraction if the result is negative, it's in 
2-complement anyway and shouldnt be a problem...

Author: Dieter Gens (sanji)
Posted on:

Rate this post
0 useful
not useful
I thought the Carry bit is only used if you intend to work further with 
the result of the operation and not loose resolution.

Or do I need to do something with the carry bit here as well?


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.