Program error C in the MIPS-based architecture

存储架构 2018-07-23 阅读原文

This program takes the command line argument and fill write_key array

#include 
#include 

 int main(int argc, char **argv)
 {
   int i,j;
   unsigned char write_key[16];

   for(j=0;j<4;j++){
     for(i=0;i>(8*(4-1-i))) & 0xff;
     }
   }

   for(i=0;i<16;i++){
     printf("write_key[%d] :: 0x%x   n", i, writekey[i]);
   }
  printf("n");
}

I launch the program with

./app 0xffffffff 0xffffffff 0xffffffff 0xffffffff

For following input output on my linux pc is

write_key[0] :: 0xff
write_key[1] :: 0xff
write_key[2] :: 0xff
write_key[3] :: 0xff
write_key[4] :: 0xff
write_key[5] :: 0xff
write_key[6] :: 0xff
write_key[7] :: 0xff
write_key[8] :: 0xff
write_key[9] :: 0xff
write_key[10] :: 0xff
write_key[11] :: 0xff
write_key[12] :: 0xff
write_key[13] :: 0xff
write_key[14] :: 0xff
write_key[15] :: 0xff

Which is as expected. but when i cross compiled this program for Mips based embedded board and run it there my output is.

write_key[0] :: 0x7f
write_key[1] :: 0xff
write_key[2] :: 0xff
write_key[3] :: 0xff
write_key[4] :: 0x7f
write_key[5] :: 0xff
write_key[6] :: 0xff
write_key[7] :: 0xff
write_key[8] :: 0x7f
write_key[9] :: 0xff
write_key[10] :: 0xff
write_key[11] :: 0xff
write_key[12] :: 0x7f
write_key[13] :: 0xff
write_key[14] :: 0xff
write_key[15] :: 0xff

Why there is a difference in output for different architecture ?

strtol
returns a long int
. If long int
is 32 bits wide on on your platform, then it can't represent 0xffffffff
(which is about 4 billion).

man strtol
:

The strtol()
function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol()
returns LONG_MIN
. If an overflow occurs, strtol()
returns LONG_MAX
.

So it returns LONG_MAX
, which in 32 bits is 0x7fffffff
(or about 2 billion), because that's the biggest positive 32-bit number.

Presumably your Linux PC uses 64-bit long int
s so this problem doesn't occur there.

To fix this problem, change the code to use strtoul
, because 0xffffffff
fits just fine into an unsigned long int
.

Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!

您可能感兴趣的

谈谈我理解的SA——Systems Architecture 什么是SA? SA即Systems Architecture,是系统体系结构。 系统体系结构是定义系统的结构、行为和系统视频的概念模型。架构师将其...
Android Architecture Components Reach Stability Google has released the 1.0 stable version of its Android Architecture Component...
SUSE Enterprise 15应对多模IT环境 助力企业数字化转型... 越来越多的企业的IT基础设施由两个或更多云(私有,社区或公有)构成。企业将自己内部部署的IT和数据中心来实现IT目标,允许工作负载随着需求和成本的变化在公有和私...
You’ve Created a Monster: the Monolith You might think comparing your bank's monolithic architecture to Frankenstein...
Load Value Approximation(cache层的load近似化) Introduction 近似计算已经成为了一个研究的热门。而且他跨越了多个层面,从电路层到体系结构层,到算法层。 谷歌学术搜索结果: 物...