subtract in assembler different than C? (dsPic)

Guest #2462648
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!
Guest #2462652
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...
#2462754
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?

Reply

A question of your own? This thread is over 14 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.