Assembly language to C

Thread Starter

jey11

Joined Jun 7, 2010
25
I need to start the timer, this assembly code works fine, but need to convert to C. Any assistance very much appreciated,

// #define T_BASE 0xFF202000
// volatile int * T_ptr = (int *)T_BASE;
@ get the addresses
ldr r4 , =0xff202000


Assistance required for below part....

@ start the timer for continuous
@ counting ( 0b0110 )
mov r1 , #6
str r1 , [r4 , #4]
 

Papabravo

Joined Feb 24, 2006
21,225
I need to start the timer, this assembly code works fine, but need to convert to C. Any assistance very much appreciated,

// #define T_BASE 0xFF202000
// volatile int * T_ptr = (int *)T_BASE;
@ get the addresses
ldr r4 , =0xff202000


Assistance required for below part....

@ start the timer for continuous
@ counting ( 0b0110 )
mov r1 , #6
str r1 , [r4 , #4]
It would be nice if there was a simple way to accomplish what you want, but the fact is that doing so requires some knowledge of the environment you are working in and the compiler. It is also the case that the answers are not unique. What kind of assembler are we talking about and which C compiler are we talking about?

In the first example above you have already given your self what you asked for. I don't know what you think you are missing there. You have taken a hexadecimal constant which I presume represents an absolute address in the memory space of some processor, applied a cast to that constant, creating a pointer to an integer and assigning it to a variable called T_ptr. You also indicated that T_ptr is volatile which means the compiler cannot depend on being able to read back what it just wrote to T-ptr.

I'm guessing what the two assembly language instructions are meant to be, but assuming we can rely on T-ptr to have the value that was assigned to it, namely, a pointer to address 0xFF202000, and what you want to do is write the value "6" to that address plus 4, then the following "may be understood" by your compiler:

C:
*(T_ptr + 4) = 6 ;
should do it. This is not for sure, not for certain, but probably. If you know the size of a pointer to an int to be exactly 4, then you might also write

C:
*(++T_ptr) = 6 ;
using the ++ (pre-increment) operator on T_ptr.

Is that all clear?
 

MrChips

Joined Oct 2, 2009
30,802
You have not provided enough information.
Start by telling us which MCU you are running.
Next, what IDE (programming platform) are you using?
 

Thread Starter

jey11

Joined Jun 7, 2010
25
It would be nice if there was a simple way to accomplish what you want, but the fact is that doing so requires some knowledge of the environment you are working in and the compiler. It is also the case that the answers are not unique. What kind of assembler are we talking about and which C compiler are we talking about?

In the first example above you have already given your self what you asked for. I don't know what you think you are missing there. You have taken a hexadecimal constant which I presume represents an absolute address in the memory space of some processor, applied a cast to that constant, creating a pointer to an integer and assigning it to a variable called T_ptr. You also indicated that T_ptr is volatile which means the compiler cannot depend on being able to read back what it just wrote to T-ptr.

I'm guessing what the two assembly language instructions are meant to be, but assuming we can rely on T-ptr to have the value that was assigned to it, namely, a pointer to address 0xFF202000, and what you want to do is write the value "6" to that address plus 4, then the following "may be understood" by your compiler:

C:
*(T_ptr + 4) = 6 ;
should do it. This is not for sure, not for certain, but probably. If you know the size of a pointer to an int to be exactly 4, then you might also write

C:
*(++T_ptr) = 6 ;
using the ++ (pre-increment) operator on T_ptr.

Is that all clear?
Thank you for explanation and trying start the clock in CPUlator ARMv7 System Simulator (01xz.net) simulator when I click push button 0.

Push button part is not implemented yet. When call start function, clock is not functional. What I am not doing correctly.

Thanks for the help.

My function is


int StartClock(void)

{
volatile int Start;
Start = *(T_ptr+4) ;
*(T_ptr+4) = 6 ;

return Start;

}
 

Thread Starter

jey11

Joined Jun 7, 2010
25
Thank you for explanation and trying start the clock in CPUlator ARMv7 System Simulator (01xz.net) simulator when I click push button 0.

Push button part is not implemented yet. When call start function, clock is not functional. What I am not doing correctly.

Thanks for the help.

My function is


int StartClock(void)

{
volatile int Start;
Start = *(T_ptr+4) ;
*(T_ptr+4) = 6 ;

return Start;

}

I attached structure of timer below.
1616245809954.png
 

Thread Starter

jey11

Joined Jun 7, 2010
25
The clock starts but not responding to push button. But I am not doing not right. see attached requirement and using CPUlator ARMv7 System Simulator (01xz.net) simulator. Thanks for your valuable time.

1616251882411.png

#define HEX3_HEX0_BASE 0xFF200020
#define KEY_BASE 0xFF200050
#define T_BASE 0xFF202000


volatile int * KEY_BASE_ptr = (int *)KEY_BASE;
volatile int * HEX_ptr = (int *)HEX3_HEX0_BASE;
volatile int * T_ptr = (int *)T_BASE;

int StartClock(void)
{
volatile int Start;
Start= *(KEY_BASE_ptr+1);
*(T_ptr+1) = 6 ;
return Start;
}

int StopClock(void)
{
volatile int Stop;
Stop = *(KEY_BASE_ptr+1);
*(T_ptr+1) = 8 ;
return Stop;
}
 

vanderghast

Joined Jun 14, 2018
67
Most C compiler can display the Dissambly, the assembly code that they produce. Does your code work in C?

That being said, the button push "probably" raises or drop a voltage. We generally detect it through handling an event, but it can also be done while looping on checking the value for that voltage, generally through a GPIO pin. It "seems" that you are trying to use an interrupt. It can be a raising or a descending edge. An oscilloscope could be use to confirm your assumptions. You have to enable the emitter of the interrupt for the GPIO PORT, which depends on the exact ARM implementation you have, AND also, the NVIC, the arbitror of the various interrupts, which has to be aware that it should interract with which each exceptions/interrupts. Asking only the emitter to raise event won't do any good since the NVIC may be not looking at those non-critical interupts, having them masked out. You may also have to enable the (soft) interrupts on the whole system since by default, they may be flagged as "masked" out as a group (a single flag useful for when a not-interruptible sequence of instructions is required). The MCU, or simulator, that you use is foreign to me, I can't give more specific details.
 

BobaMosfet

Joined Jul 1, 2009
2,113
I need to start the timer, this assembly code works fine, but need to convert to C. Any assistance very much appreciated,

// #define T_BASE 0xFF202000
// volatile int * T_ptr = (int *)T_BASE;
@ get the addresses
ldr r4 , =0xff202000


Assistance required for below part....

@ start the timer for continuous
@ counting ( 0b0110 )
mov r1 , #6
str r1 , [r4 , #4]
You don't need to convert to C if your compiler supports __INLINE__ directive for inline assembly. Learn a little bit more about how to use your compiler.
 
Top