Defining variables in assembler

cancel
Showing results for 
Search instead for 
Did you mean: 

Defining variables in assembler

Jump to solution
327 Views
roberthiebert
Contributor IV

MC9S12XEP100, CW 5.9.0 SE. I'm just starting out trying to create a simple test program in absolute assembler and I can't seem to define my variables correctly. For example

  varbyte   DS.B 1  Works just fine with values from 0 to 255

  Varword  DS.W 1 behaves like a signed variable and will only allow values from 0 to 32767 before going negative. What am I doing wrong?

Robert 

 

 

 

 

 

 

 

0 Kudos
1 Solution
261 Views
StanoA
NXP Employee
NXP Employee

Hi Robert,

There is no possibility to define “signed” or “unsigned” variables in assembler. It is up to user to take care to use right functions for the required operation and also consider the possibility to receive negative result of that operation (flags N & C).

It is possible select “HEX” format to view variables also and take care about highest bit meanings. There is also option uDec – I suppose “unsigned decimal” but it shows the same as DEC. I think it is due to show the right results not without sign – it would lead to mistakes.

I hope it helps you.

Best Regards,

Stano.

View solution in original post

0 Kudos
6 Replies
316 Views
StanoA
NXP Employee
NXP Employee

Hello Robert,

Your definition is right. But in asm code you need to take care to use the right instructions and take care about flags also. It’s up to you if the “negative” flag (“N” and “C” in CCR) is accepted or not in the code. It depends on variable usage – as signed or as unsigned.

If such variables are defined as signed or unsigned in C language, it is the compiler responsibility to translate is right into asm code. It depends on the variable definition in C code.

You can read more on web about this topic also:

https://stackoverflow.com/questions/18984515/x86-asm-assembler-difference-between-signed-and-unsigne...

https://stackoverflow.com/questions/19596729/signed-and-unsigned-convention-in-assembly-language

https://stackoverflow.com/questions/11205522/identifying-signed-and-unsigned-values-in-assembly/1120...

I think it could help you to solve your issue.

Best Regards,

Stano.

0 Kudos
302 Views
roberthiebert
Contributor IV

Hi Stano,

Thanks for your suggestions but it really didn't help me, probably because I don't think I posed my question correctly.

I composed another small test program:


ORG RAMStart
; Insert here your data definition.
multiplicand   DS.W 1 ; Memory location $2000:$2001
multiplier       DS.W 1 ; Memory location $2002:$2003
product         DS.W 2 ; Memory location $2004:$2005:$2006:$2007


; code section
ORG ROMStart


Entry:
_Startup:
LDS #RAMEnd+1 ; initialize the stack pointer

CLI ; enable interrupts


; Test Code
MOVW #$FFFF,multiplicand    ; Decimal 65535 -> "multiplicand"($2000:$2001)
MOVW #$FFFF,multiplier         ; Decimal 65535 -> "multiplier" ($2002:$2003)

LDD multiplicand             ; "multiplicand" -> Accu D
LDY multiplier                  ; "multiplier" -> Accu Y
EMUL                              ; (D) x (Y) -> Y:D
STY product                    ; Result high word -> "product"($2004:$2005) ; should be #$FFFE
STD product+2                ; Result low word -> "product+2"($2006:$2007)  ; should be #$0001

When I use the debugger and step through the program everything works as it should. The registers have the correct values and the memory locations have the correct values. My problem is with the "Data" window.

In that window it shows:

multiplicand  -1  int

multiplier      -1  int

product   <4>  array  [2]  of int

var00001   16384  int

It looks to me that in the Data window it is displaying the variables as signed words. How can I change that?

 

Regards,

Robert

 

0 Kudos
273 Views
StanoA
NXP Employee
NXP Employee

Hi Robert,

OK, just go to Data window, select such variable (e.g. multiplier – will be in blue row) , then right click, -> Format -> Selected/All -> and select format in which you want to see the variables.

I hope it helps you.

Best Regards,

Stano.

0 Kudos
268 Views
roberthiebert
Contributor IV

Hi Stano,

Thanks for your reply, but changing the displayed format really isn't my problem. The problem is that the Data window displays my word variables as signed variables but I want them to be unsigned. I just don't know how I can communicate this to CodeWarrior.

Regards,

Robert

0 Kudos
262 Views
StanoA
NXP Employee
NXP Employee

Hi Robert,

There is no possibility to define “signed” or “unsigned” variables in assembler. It is up to user to take care to use right functions for the required operation and also consider the possibility to receive negative result of that operation (flags N & C).

It is possible select “HEX” format to view variables also and take care about highest bit meanings. There is also option uDec – I suppose “unsigned decimal” but it shows the same as DEC. I think it is due to show the right results not without sign – it would lead to mistakes.

I hope it helps you.

Best Regards,

Stano.

View solution in original post

0 Kudos
257 Views
roberthiebert
Contributor IV

Hi Stano,

That did it! I didn't know what uDec meant so I never tried to change the format to that. If I had tried HEX I would have seen that the results were correct. So, what I learned from this is that DEC assumes a signed integer, uDec assumes an unsigned integer and HEX is just hex, and always correct. Thanks so much for being patient with me.

Regards,

Robert

0 Kudos