Acura's C language error

Source: Internet
Author: User

Today when testing the hardware communication module found a strange problem, send data and receive data to compare replication time frequent data errors.

The test flow is as follows: sends a byte and receives a byte, compares, when the return data and sends the data is unequal, the error counter accumulates.
The data sent and received is abstracted as follows: UInt16 i = 0;     UInt16 j = 0;      UInt32 error_num = 0; Xx_send_data (i++);     j = Xx_rece_data ();     if (I! = (j + 1)) {error_num++;     This code in theory, as long as the data I send and receive data, the error counter should be no error, but in fact, the first suspect is due to the FPGA hardware module data transmission and receive timing problems, but multiple large-volume functional simulation and timing simulation are no problem.     I began to wonder if this short C program is working properly, so in error_num++ this sentence here to break point, to see every time the error data is i=0,j=65535 the time of the error, why all wrong here? Think of the solution, later asked senior engineer Fly elder brother, fly elder brother a look on see where the problem appears, let me ashamed!! ~ ~ The original error occurs: (The program runs on a 32-bit processor) This program from the surface is not any problem, but the detailed analysis will find that when the i=65535 send the data, after the completion of the execution of I++,i 0,j call receive function, the return value is correct is also 65535, but     , it appears that the problem is in the IF (I! = (j+1)) here. Originally, j+1 This arithmetic operation value is the default to the 32 register, so the j+1 operation result is 32 bit data, therefore, when j=65535, (j+1) actually is 65536, (0! = 65536), therefore produces the mismatch.     Error code self-added.     There are many ways to eliminate this error, and the code below will fix the problem. UInt16 i = 0;UInt16 j = 0;UInt32 error_num = 0; Xx_send_data (i++);j = Xx_rece_data (); if (i! = (uint16) (j + 1)) { error_num++;     }Finally found the problem, it seems that after the code will be carefully, learn from the lesson.//Read the reply, found that a lot of people are pure software to understand this code, but embedded C language must be detailed and processor behavior together, the following add detailed explanationThis assembly code for this piece of code, read the assembly code in detail to find out where the problem lies.   The first sentence assembly code Ldhu r3,-10 (FP) means to load a half word from memory or cache and expand it to an unsigned type, which is to load the variable i into the Register R3, R3 is a 32-bit register, At this time, although the variable i is uint16 but the comparison is still a 32-bit second sentence assembly code Ldhu r2,-12 (FP) the same sentence, here is the load variable J to register R2 the third sentence assembly code Addi r2,r2,1/*the problem is * * This is the execution of the j+1, the visible result is stored in the R2, R2 is a 32-bit wide register, so when i=0,j=65535, j+1 = 65536 does not overflow, but directly assigned to R2, when R2 is 65536,r3 is 0The last sentence of the assembly code BEQ r3,r2,0x800258<main+92> This sentence is to compare R3 and R2 values, if equal jump to 0x800258<main+92> this address executes the program. If not equal directly run the next sentence, that is error_num++ from the above analysis, the embedded industry must be very familiar with the processor, there are some strange problems, in order to finally find the problem.

Acura's C language error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.