How I can store the multiplication result in global vriable?

Hi I am trying to write a piece of code in assembly for multiplying two 8-bit numbers here is my code:

int var; asm("ldi r20,128"); asm("ldi r21,2");

asm("muls r20,r21"); asm("movw r20,r0"); asm("sts var,r20"); asm("sts var + 1 ,r21");

Serial.println(var);

i got strange characters on my serial. I tried to read values for r1 and r0 separately like asm("sts var,r0"); actually after muls instruction when I store any register in variable , it prints out strange charterers. can someone please help me why this happens and how I can fix it? thanks

I am trying to write a piece of code in assembly for multiplying two 8-bit numbers

Why? Do you really think you can do it better than professional compiler writers?

Ask this question from my university teacher....

Yes. You could propose your original question to your professor. I am sure he could help you with the contents of your course that you are taking and he is teaching you. And I am sure he will have open office hours for just that purpose.

As an alternative, you could see how the compiler handles

byte a = 14;
byte c = 22;
byte c = a * b;

http://www.gammon.com.au/tips#info1

r1 is "magic" WRT to the avr-gcc compiler. You can't trash it with a muls instruction without saving it before and restoring it afterward. (also, do you really want "muls" if one of your arguments is 128?)

westfw: (also, do you really want "muls" if one of your arguments is 128?)

I thought the whole point of twos-complement negaitive numbers was that they behave identically to positive numbers under basic arithmetic operations. It shouldn't matter.

It's a sign-extension thing when your result is bigger than your operands.

128 in an 8bit signed number is -128 -128 * 2 = -256 = 0xFF00 (signed 8bit multiply) 128 * 2 = 256 = 0x0100 (unsigned 8bit multiply) (You'll note that the low 8bits are the same, as you said.)

PaulMurrayCbr: I thought the whole point of twos-complement negaitive numbers was that they behave identically to positive numbers under basic arithmetic operations. It shouldn't matter.

Only addition and subtraction.

@kiavash:

Please use code tags.

Read this before posting a programming question

How to use this forum

Ask this question from my university teacher....

The old handball, eh?