EmbDev.net

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


von Dieter (Guest)


Rate this post
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:
1
x_n_re[b_index] = x_n_re[a_index]-resultMulReCos+resultMulImSin;
2
x_n_re[a_index] = x_n_re[a_index]+resultMulReCos-resultMulImSin;
3
x_n_im[b_index] = x_n_im[a_index]-resultMulReSin-resultMulImCos;
4
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.
1
  mov.w [w0+0x4], w12    ; b_index -> w12
2
  sl     w12,#1,w12    ; b_index *= 2 for 16-bit increment
3
4
  mov.w [w0+0x6], w9    ; a_index -> w9
5
  sl     w9,#1,w9    ; a_index *= 2 for 16-bit increment
6
7
  mov.w [w2], w5      ; &x_n_re[0] -> w5
8
  mov.w [w5+w9], w8    ; x_n_re[a_index] -> w8
9
10
  add w1,w4,w6       ; ReCos+ImSin
11
  sub w8,w6,w6       ; w6=x_n_re[a_index]-recos+imsin
12
  mov w6,[w5+w12]     ; x_n_re[b_index]=w6-recos+imsin
13
14
  sub w1,w4,w6       ; ReCos-ImSin
15
  add w8,w6,w6       ; w6 = x_n_re[a_index]+recos-imsin
16
  mov w6, [w5+w9]     ; x_n_re[a_index]=w6+recos-imsin
17
18
  mov.w [w2+0x2], w5    ; &x_n_im[0] -> w5
19
  mov.w [w5+w9], w8    ; x_n_im[a_index] -> w8
20
21
  sub w7,w3,w6       ; w6=ReSin-ImCos
22
  sub w8,w6,w6       ; w6=x_n_im[a_index]-resin-imcos
23
  mov w6, [w5+w9]     ; x_n_im[b_index]=w6-resin-imcos
24
25
  add w7,w3,w6       ; w6=ReSin+ImCos
26
  add w8,w6,w6       ; w6=x_n_im[a_index]+resin+imcos
27
  mov w6, [w5+w9]      ; x_n_im[a_index]=w6+resin+imcos

Am I missing something?

Thanks a lot!

von Michael (Guest)


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

von Dieter (Guest)


Rate this post
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...

von Dieter G. (sanji)


Rate this post
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?

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.