if&memcmp指令

本文探讨了在C语言中使用memcmp进行数据比较时遇到的一个常见误区:即当比较两个不同内存区域的内容时,如何正确地使用if条件判断。特别关注了在ARM汇编环境下,memcmp函数的行为以及if(flag)条件语句的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int flag;

int cmp_1,cmp_2;

.........

code

.........

/*

当cmp_1>cmp_2时,保存数据

*/

flag = memcmp(cmp_1,cmp_2,1);

if(flag)

{

     //code save data;

}

 

在调试的时候,发现只要cmp_1!=cmp_2,都能跳转到//code save data; 里面,跟原先的流程有很大的出入,在查看汇编的时候发现

 

if(flag) 翻译成汇编为 CBZ指令 

 

CBZ 在arm指令的意思是只要不是0,都执行if括号里面的code。

 

因此在使用if语句判断的时候,千万注意此情况的出现。if(cmp_1>cmp_2)

memcmp 是从首地址开始比较的,如果高为放在高地址,则比较有问题!!!!

auto start_time1 = std::chrono::steady_clock::now(); if (all_serial[index].isconnected() == -1) { log_error("Please open the come port!"); return -1; } int transmission_times = 1; memset(&parsered_message[index], 0, 128); memset(&packed_message[index], 0, 128); uint8_t head = heads[slave]; uint8_t packed_length = 0; package_protocol_message(slave, send, (uint8_t*)(packed_command+index), sendLength, &packed_length, mode); RETRY: all_serial[index].getBufferBytes(); //清除发送缓冲区接收缓冲区 int32_t result = (int32_t)all_serial[index].sendArray(packed_command[index], packed_length);//发送相关指令 if (result != 0) { log_error("%s:Failed to send command to sensor.", send); return result; } //倘若是修改baud率,则不用接受数据 if (sendLength > 6 && (memcmp(send,"baud",4)==0)) { return 0; } //倘若是修改load,则超时 int timeout_multi = 1; if (memcmp(send,"load",4) == 0 || memcmp(send,"frmt",4) == 0) { timeout_multi = 50; } //开始接收协议头数据 _Bool timeout = true;//是否会超时 uint32_t error; COMSTAT comstat;//串口状态 uint32_t actual_length;//串口接收数据的实际字计数 auto start_time = std::chrono::steady_clock::now(); while (ELAPSED_TIME(start_time) <= TIME_OUT_MS*timeout_multi) { result = (int32_t)all_serial[index].clearcom(&error, &comstat); if (comstat.cbInQue < PACKET_EXTRA_HEAD_SIZE) { continue; } result = (int32_t)all_serial[index].getArray(receive, PACKET_EXTRA_HEAD_SIZE, &actual_length);//接收数据 if (actual_length == PACKET_EXTRA_HEAD_SIZE) { timeout = false; break; } } //超时 if (timeout) { log_error(" %s:receive header and length time out!", send); if (transmission_times <= RETRANSMISSION_TIMES) { transmission_times++; goto RETRY; } else { return -1; } } //crc校验错误 if (receive[0] == 1 && receive[1]==0) { log_error(" %s:crc error!", send); if (transmission_times < RETRANSMISSION_TIMES) { transmission_times++; goto RETRY; } else { return -1; } }这一段主要耗时操作在哪
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值