Welcome! Log In Create A New Profile

Advanced

signed binary representation

Posted by arasium 
signed binary representation
July 20, 2010 10:09AM
Hi everybody.

I've this piece of code in the DI Module:

var_30= -0x30
var_2C= -0x2C
var_28= -0x28

PUSH    {R4-R6,LR}
SUB     SP, SP, #0x20
MOVS    R4, R1
MOVS    R1, #0x58 ; 'X'
MOV     R6, SP
NEGS    R1, R1
LSLS    R3, R3, #0x18
MOVS    R5, R0
STRB    R1, [R6,#0x30+var_30]
STR     R4, [SP,#0x30+var_2C]
STR     R2, [SP,#0x30+var_28]
CMP     R3, #0

Then the SP register is passed to the HandleDiCommand function.
So my question is about R1 and the di command associated.
Why doing "NEGS R1, R1" and then "STRB R1, [R6,#0x30+var_30]"? If i'm correct, the NEGS will only modify the MSBit.

So, i will have: R1 = 0b10000000 00000000 00000000 01011000

But then, if we only store one byte in the [R6,#0x30+var_30], why doing the NEGS before?

Perhaps the signed values aren't just with a sign bit (One's Complement or two's), and in this case, the NEGS have a real goal :)

Please help me.



Edited 2 time(s). Last edit at 07/20/2010 10:33AM by arasium.
Re: signed binary representation
July 20, 2010 10:37AM
I will answer to my question :)

If the signed number user the two's complement representation, we will have (only for the LSB of R1) something like that:

R1 = 0x58
so
R1 = 0b01011000
so
NEGS R1 = 0b10101000
so
NEGS R1 = 0xA8


And A8 is the Read Command (wich is correct, because we are in a reading function).
Re: signed binary representation
July 20, 2010 05:38PM
Almost everything uses 2 complement numbers these days. [en.wikipedia.org]

So, I think the NEG instruction does:
Invert all bits.
Add 1.

Or: R1 = (~R1) + 1;
Or: R1 = -(signed int)(R1);
Re: signed binary representation
July 21, 2010 10:02AM
Yes it was my conclusion too :)
Sorry, only registered users may post in this forum.

Click here to login