PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

ARM Assembler question.

Hi,

I am trying to call a C function (that calls another function, that calls another one) from an assembler routine. But inside the third function there is a malloc() that keeps on giving me a Data misaligned error. The rest of the function seems to run normally.

Here is what I am doing to call it:

Code:
// Push registers to stack
stmfd sp!, {r0-r11}
// Save link register
mov lr, pc
// Assign register pc with [r11, #08]
ldr pc, [r11, #08]
// Pop registers from stack
ldmfd sp!, {r0-r11}


Now, I use r11 as my data storage pointer. The routine pointer is stored into this area. I also use r10 register as a temporary variable here and there.

Is there something wrong with the way I call this C function?

C function:

Code:
Function 1:
Calls Function 2

Function 2:
Calls Function 3

Function 3:
malloc(); // crash here.
Does the C function that you're calling from assembler have any arguments? (Rest looks fine to me after half a bottle of wine at least ;))
Hehe! No, no parameters for the first C function but the 2 others called by that function do have parameters.

I am thinking that it might be the stack pointer somehow that I should be re-assigning to the original one saved when the assembler code is being called. I think there is a mov r12, sp as the first line of the whole assembly code.

Code:
// Push registers to stack
stmfd sp!, {r0-r11}
// Keep current stack pointer in temporary register
mov sp, r10
// Use original stack pointer
mov sp, r12
// Save current stack pointer on original stack
stmfd sp!, {r10}

// Save link register
mov lr, pc
// Assign register pc with [r11, #08]
ldr pc, [r11, #08]

// Restore stack
ldmfd sp!, {sp}
// Pop registers from stack
ldmfd sp!, {r0-r11}


This might not make sense at all, unless each C function uses it's own stack.
I am thinking that it might be the stack pointer somehow that I should be re-assigning to the original one saved when the assembler code is being called.


In the calling conventions, the functions share the stack. As a function is entered, it's responsible for stacking the registers it's going to corrupt (it's allowed to corrupt r0-r3,r12 with impunity).

So basically what you're doing is applying that and synthesizing a BL instruction with the MOV and the LDR:
Code:
stmfd sp!, {r0-r3,r12} // Push registers to stack
mov lr, pc // Save link register
ldr pc, [r11, #08] // Assign register pc with [r11, #08] (i.e. the value loaded from the address (r11 + 8 bytes))
ldmfd sp!,{r0-r3,r12}


The only difference with the first code that you posted is that this one saves R12 as well.

If that still doesn't help, you should post/mail me more context (the assembler+source dump of both of the functions that code calls, and the surrounding context of the above code).

One other thing that sprang to mind is whether this is being run in debug or release mode?
I ran it in debug and release but it crashes in both cases.

Regards,
Alain
What have you got wrapped around the above code in order to call it?

i.e.
Code:

...
... <--- what's this
...
stmfd sp!, {r0-r3,r12} // Push registers to stack
mov lr, pc // Save link register
ldr pc, [r11, #08] // Assign register pc with [r11, #08] (i.e. the value loaded from the address (r11 + 8 bytes))
ldmfd sp!,{r0-r3,r12}
...
... <--- and this
...
Ok, I found the problem. My assembly code calling function has been changed with some crappy code yesterday. I thought it was fine but it was causing all this mess all along. :evil:
Another silly question:

How do I encode the 12bits value for an immediate constant for ARM?

I know that 0x40 << 4 will give me 1024. But how the hell do I find this out if all I have is 1024 from the source code?

0 - 255 -> 0x00 - 0xff
256 - 1020 -> 0x40 << 2 - 0xff << 2
...

I find kinda confusing what the arm assembler will give me as result. For 1024 it gives me 01f or something like that (i don't have it in front of me right now).
rotate_imm = top four bits * 2
immediate = bottom_8_bits rotated right by rotate_imm

If you read the assembler source, it'll put things like:
<pre>
mov r12, #1, 20
</pre>
.. which is r12=1 ROR 20.

It's covered in the <a href="http://www.altera.com/literature/third-party/ddi0100e_arm_arm.pdf">ARM ARM</a> (ARM Architecture Reference Manual), page 222 (A5-6).
I understand this part, (I re-read the manual just to make sure I was on the right track.)

I would like to figure out how to get this 8 bits and 4 bits values from a decimal value passed in the assembler. How can I convert 1024 to 0x01B?

I am not really good with bits manipulation. A little piece of C code that would do this?
If you want it restrained only to what can be done in one immediate constant then it's "easy".

You can just repeatedly rotate the number by 2 and look for the smallest (unsigned) value. By the amount you rotated it you will then also know the rotation constant (32 - whatever_gave_you_smallest).

If you want it to take any 32-bit constant and give you optimised ARM code to synthesize it, that's (much) more difficult.
Ok, lets apply some practice to this! :?

1024 >> 2 = 256
256 >> 2 = 64
64 >> 2 = 16
16 >> 2 = 4
4 >> 2 = 1

I rotated 5 times in total, right?

My smallest unsigned value is 64.

Am i doing it right so far?

0x405 will give me 64 << 5 = 2048.

I am a little confused!

Sorry for being such an idiot!
(Sorry for the formatting -- it seems that the "pre" tag is being ignored and it's adding a br after each line).

Ok, let's do this in binary.

<pre>
10000000000 <- 1024
</pre>

Let's rotate it right (ROR) by 2 until we find our smallest value:

<pre>
10000000000 <- 1024
00100000000 <- 256 (shift 1)
00001000000 <- 64 (shift 2)
00000010000 <- 16 (shift 3)
00000000100 <- 4 (shift 4)
00000000001 <- 1 (shift 5)
</pre>

Right, our minimum value is the last one (with other values you might have to rotate all around to be sure you have the minimum).

So, our constant is 1. We rotated by 2 five times, but that was to move it *down*. When we place it in the instruction, we want to move it the other way. So, we subtract it from 16.

<pre>
16 - 5 = 11 = 0x0b
</pre>

So the bottom 12 bits of the MOV instruction encoding are:
<pre>
0x0b<<8 | 0x01 = 0xb01
</pre>

Just to prove it works:
<pre>
0xb01 Means:
00000000000000000000000000000001
rotated right by:
0xb = 11
multiplied by 2 = 22

00000000000000000000000000000001 <-- ror 00 (0)
01000000000000000000000000000000 <-- ror 02 (1)
00010000000000000000000000000000 <-- ror 04 (2)
00000100000000000000000000000000 <-- ror 06 (3)
00000001000000000000000000000000 <-- ror 08 (4)
00000000010000000000000000000000 <-- ror 10 (5)
00000000000100000000000000000000 <-- ror 12 (6)
00000000000001000000000000000000 <-- ror 14 (7)
00000000000000010000000000000000 <-- ror 16 (8)
00000000000000000100000000000000 <-- ror 18 (9)
00000000000000000001000000000000 <-- ror 20 (10)
00000000000000000000010000000000 <-- ror 22 (11) = 1024
</pre>
Ah, the 16 - 5! 8O That's how you get the 0xb! That makes a lot more sense now. And yes 1 is the last signed value you are right. I was a little lost with that 64 value I gave you before.

I will come up with a C function and post it here. I would like other users who might find this interesting to see the results.

Thanks again for your enormous patience!
I hope this will be the last question on the subject:

How can I determine what is the last number to stop rotating? Is there an easy formula for this? I have a C function that is working quite well but it doesn't know when to stop looping. If I can figure this one out, the function will be ready.

Oh, I lied, another question... How to make up the smallest number + (16 - shift_count). Like 0x01 + 0xb = 0x01b in C? I'd like my function to return the 12bits value instead of two seperate values.
Ok, I got the last question under control. 0xb * 256 + 0x01 = 0xb01. I was a little rusted with maths.
How can I determine what is the last number to stop rotating?


Well you can only rotate it by 2 sixteen times before it becomes the start value. There's probably a clever way of doing it in less but I can't think of one right now.

I'd just do something like:
Code:


uint min=source;
int rotations;

for(int i=1;i<16;i++)
{
source=ror(source,2);
if(source<min) {
min=source;
rotations=i;
}
}

// You may want to check min<256 here

return ((16 - rotations)<<8)|min;
Thanks. This is what I came up with so far:

Code:
int immediate (unsigned int value)
{
if (value < 256)
return value;

int d = value / 16;
if (value != d * 16)
return -1;

unsigned int startvalue = value;
unsigned int min=value;
int rotations = 0;

int i = 0;

while (value > 0)
{
i++;
value = value >> 2;
if (value > 16)
{
int d = value / 16;
if(value != d * 16)
{
min=value;
rotations = i;
}
}
}

if (rotations == 0)
{
rotations = i - 1;
min = 1;
}

return ((16 - rotations)<<8)|min;
}


I check for the smallest value that cannot be divide by 16 during the shifting loop.

Correct me if I'm wrong but I think that only values dividable by 16 will be allowed to be usable here.
Where did the 16 come from? :S

Try this:
Code:
int immediate (unsigned int value)
{
uint min=value;
int rotations=16;

for(int i=1;i<16;i++)
{
value=(value>>2)|(value<<30); // =ROR(value,2)

if(value<min) {
min=value;
rotations=i;
}
}

if(min>255)
return -1;

return ((16 - rotations)<<8)|min;
}


If not, I think your code rejects immediates such as:
0xf000000f .. which are valid.
Your code seems to work well so far. Thanks.

Btw, how do you do a divide in assembly (ARM)? There are ADD, SUB and MUL operator but I didn't see a DIV.
There isn't one (it's a RISC chip, after all ;)). Start <a href="http://www.pocketmatrix.com/forums/viewtopic.php?t=11940&start;=0">here</a>, maybe (or just look at what C does to perform a divide).
Thanks refactor.

A few other questions, will it ever end I hear you say!

1. Is there an assembler for the PC that supports the THUMB intructions, something like NARM.EXE?

2. Here is a list of all the opcodes I have implemented so far in PASM, can you tell me what other ones are very important to fully program under ARM?

ADD, SUB, MUL, AND, OR, XOR, CMP, JSRxx, JMPxx, PUSH, PULL, MOV, RET, SWP.

3. I am thinking of just using a C function for DIV and the other opcodes not implemented in ARM. What do you think? Like (prototype):

mov r0, value1
mov r1, value2
call _div
mov r0, result_reg
joshbu [MSFT] · 30 Aug 2005, 17:39 UTC
kornalius wrote:Thanks refactor.

A few other questions, will it ever end I hear you say!

1. Is there an assembler for the PC that supports the THUMB intructions, something like NARM.EXE?

2. Here is a list of all the opcodes I have implemented so far in PASM, can you tell me what other ones are very important to fully program under ARM?

ADD, SUB, MUL, AND, OR, XOR, CMP, JSRxx, JMPxx, PUSH, PULL, MOV, RET, SWP.

3. I am thinking of just using a C function for DIV and the other opcodes not implemented in ARM. What do you think? Like (prototype):

mov r0, value1
mov r1, value2
call _div
mov r0, result_reg


1. armasm -16 tells the assembler to generate thumb instructions.

2. Important things to think about: addressing modes. ARM doesn't have the same addressing modes as x86, so certain forms of the 'MOV' instruction won't be valid.

Also, it might be easier to use RISC-style load and store instructions instead of generic MOV. Then for x86 you generate MOVs for both rather than trying to figure out if a MOV is a load or a store.

Also, ARM gets a really nice win out of it's predicated instructions. It's a concept that doesn't map well to x86, so I don't know if you'd want to use it or not.

3. That's probably the right way to go.
I tried armasm -16 but it didn't work for ror neither asl, if I remember. Are these thumb instructions?
The mov instruction I have implemented is quite complex. It supports many varieties of addressing:

mov r0, r1
mov r0, 0x3AF
mov r0, [r1]
mov r0, [r1, 8]
mov [r0], r1
mov [r0, 8], 10
mov r0, [pplvar$]
mov [pplvar$], 10
mov [pplvar$, 8], 10

I made the mov and push quite complete, except the other operators are simpler, like add, sub ... For sure I had to add some opcodes underneath some of the mov'es addressing.

The push works transparently for ARM or Intel. On ARM is uses the r0-r3 before starting to push on the stack.