Dear all, I am using the Keil 5.23 and running simulation for the STM32F103ZE
I am using optimization Level 0 (-O0)
In C, I have declared two variables i j i = 0x12345678 j = 0x87654321
then I have 2 instructions in C
dummy = i + j;
dummy = i - j;
When I switch to Disassembly and looking to the disassembly code,
the register value
R2 = 0x12345678; R3 = 0x87654321; before executing the ADD r2,r2,r3 the XPSR shows
N = 1, Z = 0, C = 1, V = 0, Q = 0, T = 1; After the ADD instruction, results remains the same.. However, from the addition, there should be NO Carry after the addition, why the status of the xPSR not updated ?
It would be grateful if I can get any help on that.. Thanks, Yours FOX
Dear Nevil,
Thanks for your prompt reply and enlighten me by the following..
So long as the compiler produces code that satisfies the requirements of the original source code and the relevant language standards, then it is free to create that code in any way it wishes.
As a result, I will solve the problem using assembly language.
Many thanks for your time and effort.
Yours, WU, Chi Hang.
> I still cannot understand why the compiler only translate the i + j differently in comparison to other & | ^ and - commands. <
In my testing,
int main(void) { int i; int x = 0x12345678; // volatile int y = 0xDEADBEEF; // volatile int z; // volatile for(;;) { z = x + y; UART2_putchar( z ); z = x - y; UART2_putchar( z ); z = x & y; UART2_putchar( z ); z = x | y; UART2_putchar( z ); z = x ^ y; UART2_putchar( z ); } }
My result is: + <-> ADDS - <-> SUBS & <-> AND | <-> ORR ^ <-> EOR
I would assume that more testing is needed.
Dear John, Thanks for your further help ...
From your setting, AND ORR EOR will not update flags.. and upon my further testing ..
my setting is somehow differ than you and as follows..
int i,j; int dummy; int main(void) { int x, y; // int z; i = 0x12345678; j = 0x87654321; x = i; y = j; dummy = i & j; // ANDS dummy = x & y; // AND // z = i & j; // results NOP // z = x & y; // results NOP dummy = i | j; // ORRS dummy = x | y; // ORR // z = i | j; // results NOP // z = x | y; // results NOP dummy = i ^ j; // EORS dummy = x ^ y; // EOR // z = i ^ j; // results NOP // z = x ^ y; // results NOP dummy = i + j; // ADD dummy = x + y; // ADDS // z = i + j; // results NOP // z = x + y; // results NOP dummy = i - j ; // SUBS dummy = x - y ; // SUBS // z = i - j; // results NOP // z = x - y; // results NOP while(1) { __asm("NOP"); }; }
I tried to declare a int z; in main and repeat all the instructions and replace dummy with z // as above.. all are translated as NOP.
It seems related to global and local variable ?
Thanks for your time, John. I really appreciate your help and testing.
BTW, all the code are in Optimization Level 0.
You're using the word testing as if you're trying to identify a fault.
There is NO fault. Even if you find a way of achieving what you want today, there is absolutely NO guarantee that the next release (or previous release) will create the same code.
As I indicated before, the compiler is free to use whatever instructions it wants. If you want specific instructions, you'll need to do so in assembler.
Rather than trying to persuade the compiler to do what you want, you should really understand what the compiler is permitted to do.
The compiler is likely trying to preserve the state of the flags so it can carry forward a previous test. That's the point of having the ADD and ADDS variants in the first place.
The compiler gets to manage the use of the general purpose registers, including the flags. It also has to work with the subset of instructions a particular ISA provides.
These are more likely to use the ADDS form because the result is of importance. if (++c == 0) puts("foo"); if ((a += b) >= 0) puts("bar");
One might want to review RISC concepts, especially ARM's conditional execution. In other architectures the dependency hazards caused by the availability of the result/status of the prior instruction to the pipeline.
>>For sure I will be trying my best to read the ARM instruction set and the C tutorials to make my teaching better, but I am disappointed with the atmosphere of this forum.
Unfortunately we see the results of poor/ineffective teaching methods here a lot, so it is a point of frustration.
If a student comes here expecting us to do their homework from scratch the teacher has failed. Failed to provide them with the insight to do the work, and failed to teach basic problem solving and logical deduction, rather than just "ask the internet" or cut-n-paste some code together, they don't understand, hoping for a Shakespeare-Monkey event to occur.
I'm an EE with a BEng, on paper you are likely a lot more qualified, so I have higher expectations.
Chi Hang,
I believe that, you already know that your above assumption is incorrect. This is what I have tried to convince you.
And I also believe that, from Westonsupermare Pier's 15-Jun-2017 13:34 GMT post, you (and me) already know that compiler chooses instructions based on the Source Code Context. When the demo source code is so short, compiler just chooses instructions whatever it likes.
Please don't be disappointed with the atmosphere of this forum, you did get the help you need (not from me) from Westonsupermare Pier's valuable post.
Many Thanks for Westonsupermare Pier.
> You're using the word testing as if you're trying to identify a fault. <
I am not a native English speaker. I used the word testing, because A. I believe that OP's original assumption is incorrect. B. When I get a new device, and don't know how to use it, I will do some testing to figure it out. This doesn't mean it is a faulty device.
Sorry for any misunderstanding caused.
I also redo your latest testing, got the same result as yours. ( I am not using Keil 5.23. )
Chiu, John Linq Zhao, John Linq Chao, John Linq
My apology to you if the word testing offended you in any case.
I agreed with you that
and that's why I said before I will solve the problem using assembly language
But at the same time with what the complier output, I can explore if there are any alternatives rather than directly using assembly language that can achieve my goal. May be I can use conducting experiments if you think testing is not a good word.
Thanks for John that he gave me some insights and I can work on it.
As what you said, if it is what compiler permitted to use, I cannot see any fault here and because it is no fault, I will not expect any update on it. But still, the compiler is free to use whatever instructions it wants, what I can do it adapting with the compiler output.
Thanks. Yours WU, Chi Hang
Dear Westonsupermare Pier,
Thanks for your suggestion and that triggered me to have a really deeper understanding on those material I need to teach.
Honestly, paper qualification is not important to me. I would appreciate a person without any diploma or certificates but experienced on their field who can give me insight to solve problems.
Very appreciate your inputs.
Thanks, Yours WU, Chi Hang
Dear John,
Thanks for input and I admit that my original assumption is incorrect. Your code gives direction to me and helps alot.
My sincere thanks to you..
I am sure I did get help from this forum which allows collaborative inputs.
I would appreciate a person without any diploma or certificates but experienced on their field who can give me insight to solve problems.
You say that, but I don't really see you doing so. You've rather consistently been ignoring all the insight offered to you --- often in the same post in which you claim to have understood and accepted it.
So hopefully for the last time, let me put it strongly, but simply: you keep asking the wrong questions, because those questions are based on a colossal misunderstanding of what the job of a compiler is.
The job of the compiler is to generate machine code that does the work described by the actual source code. It's entirely up to the compiler to decide how it does that. Any and all hard expectations regarding the compiler's decisions are wrong. Of course you could run all kind of experiments to see what happens if you do "this", as opposed to "that", but that would be an exercise in futility, because the results will be largely unreliable.
And to top it off, said misunderstanding is quite typical of total greenhorns who have never used a high-level language before. That's what worries us so, as we see it manifest in a person who plans to teach this stuff.
Dear Hans-Bernhard Broeker,
Thanks for your statement of the job of the compiler and that would be informative to me.
Like a compiler, it is free for you to think if I am not appreciating the other's work, however, I truly appreciate all the information I got from here.