系统级编程作业_lab1.doc_第1页
系统级编程作业_lab1.doc_第2页
系统级编程作业_lab1.doc_第3页
系统级编程作业_lab1.doc_第4页
系统级编程作业_lab1.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

System Level Programming LabC+ fundamentals and Type conversionStudent ID _1043111051_Student Name 王金科_ _Start Time 2012年09月4日 星期二 16:20 Finish Time 2012年09月9日 星期日 23:551. Objectives:To write simple C+ programs covering: Understand how a character is stored in memory; Understand how an integer is stored in memory; Understand array and pointer; and Perform conversion from eight bits (character) to eight bytes to be displayed on screen.2. Type and Format in memory character, integer, short and float2.1 This exercise is to determine the memory size of different type.lab1_1.cpp/Determine the memory size of declaration and variable type#include #include #include #include #include #include void main() char c;char s128;short i;short n64;printf(%3d %3dn, sizeof(c), sizeof(char);printf(%3d %3dn, sizeof(s), sizeof(char128);printf(%3d %3dn, sizeof(i), sizeof(short);printf(%3d %3dn, sizeof(n), sizeof(short64);The display is:Now execute the above program and fill in the following:TypeSize in byteSize in bitShort216Char18Short3264512Char64645122.2Use the approach of the above, determine the memory size of integer, float, integer128, float16 (lab1_2.cpp)Write a program to verify your answer and fill in the following: hint, use sizeof() TypeSize in byteSize in bitInt432Float432Int1285124096Float1664512Write down your codes below:#include#include#include#include#include#includevoid main()int c; int s128; float I; float n16;printf(“%3d%3dn”,sizeof(c),sizeof(int);printf(“%3d%3dn”,sizeof(s),sizeof(int128);printf(“%3d%3dn”,sizeof(i),sizeof(float);printf(“%3d%3dn”,sizeof(n),sizeof(float16);Write down your expression amongst the difference in size between character, integer, float, short, char8, int8, short8, float8Character = 1 byteInteger = 4bytesFloat = 4bytesShort = 2bytesChar8 = 8bytesInt 8 = 32bytesShort8 = 16bytesFloat8 = 32bytes2.3This exercise is to display the decimal and octal values so that you know how it is stored in memory.lab1_3.cpp/Determine the memory size of declaration and variable type#include #include #include #include #include #include void main()for (char i = 30; i 41; +i)printf(i: dec=%d oct=%o n, i, i);the output is:Now modify the above programme to display decimal, octal, hex and unsigned as well. (Hints: hex, %x, unsigned %u)Write down your codes below:#include #include#include#include#include#includevoid main()for(char i = 30; i 41;+i)printf(i: dec=%d oct=%o hex=%x unsigned=%u n, i, i,i,i);Write down the output of the first 4 lines so that you understand the difference among them.i:dec=30 oct=36 hex=1e unsigned=30 i: dec=31 oct=37 hex=1f unsigned=31 i: dec=32 oct=40 hex=20 unsigned=32 i: dec=33 oct=41 hex=21 unsigned=332.4This exercise is to dump the content of memory of different type so that you know how it is stored in memory. lab1_4.cpp/Determine the memory size of declaration and variable type#include #include #include #include #include #include void main()char a = 0; / in hex 0x30, not 0int i = 0x00000013; /in decimal is (1 x 16 + 3) = 19short j = 18; /occupies two bytes, float f = 1.25; /occupies 4 bytesIn order to display, you have to set a break point by pressing “F9” beside the line, the program will display a red circle as follows:Now, type the address of each variable as shown in the left-bottom frame to locate whether they are. Here, we dump the address of variable a. Since, visual C+ reserves 4 bytes but char c uses one byte, you can see that the rest three bytes are set to CC CC CC (means 10101010 10101010 10101010 in binary, the default setting).1 memory location of a is 0x0065fdf4, you have to type &a2 then in the Address, key in 0x0065fdf4, 3 it displays 30 CC CC CC0x0065FDF4, the hexadecimal is 0x30 (ASCII)Now, type and execute the above program and fill in the following, note that your addresses might be different from what I have below.VariableMemory addressHexadecimal valuea0x0012ff7c0x30i0x0012ff780x13j0x0012ff740x12f0x0012ff700xCC3. Array and Pointer3.1The following is a very simple array definition. It defines an array called char a4 which occupies 4 bytes, the value is 0x30 0x31 0x32 0x00. Note that 0x30 (ASCII) is 0 in decimal. The last character must be terminated by 0x00 or is called null. (ref. lab1_5.cpp)lab1_5.cpp#include #include #include void main()char a4 = 012;Null, 0x000x30 or 0Based on the above, note that the address of array a is 0x0065fdf4. Now fill in the following:AddressTypeValue0x0012ff7ca00x300x0012ff7da10x310x0012ff7ea20x320x0012ff7fa30x003.2The following program will display the content of an array a11.#include #include #include #include void main()char a11 = 0123456789; for (int i = 0; i 10; +i)printf(The value of %d is %c in hex %x n, i, ai, ai);Now modify the above program to display the address of each location in hexadecimal and the value a12, a13, explain why you can display a12 and 13 as you only defined them up to a10 as follows. (Hint: The address of a0 is &a0) (note that your value might be different.)Write down the program#include #include #include #include#include#include void main()char a 11 = “01234567879”;for(int i= 0; i14;+i) printf( The value of %d is %x in hex %x n, i, &ai, ai);Write down your explanation (hint: dump the memory location and look at the location of a11, a12 and a13 etc. you then realise why?) The value of 11 is 12ff7f in hex ffffffcc The value of 12 is 12ff80 in hex ffffffc0 The value of 13 is 12ff81 in hex ffffffffa11,a12,a13 beyond boundary3.3#include #include #include #include #include #include void main()char a11 = 0123456789; char *ptr;ptr = &a0; / it can be &a with same effectfor (int i = 0; i 11; +i, +ptr)printf(The value of %d is %x in hex %x n, i, ptr, *ptr);Now explain the meaning of the following of the above program.%x : 以十六进制的形式输出整数 n: 相当于回车键, i: 整形变量 ptr: 指针变量,表示地址 *ptr: 指针指向地址的值3.4The following is about the integer array. #include #include #include #include #include #include void main()int a7 = 12, 21, 31, 41, 51, 61; for (int i = 0; i 6; +i)printf(Th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论