Purpose: To provide an introduction to structured programming using the C/C++ language. CS 2505 Computer Organization I C04: Pointers in C Version 6.00 This is a purely individual assignment! 1...

1 answer below »
Please make sure the expert read everything, especially the restrictions, before getting started.


Purpose: To provide an introduction to structured programming using the C/C++ language. CS 2505 Computer Organization I C04: Pointers in C Version 6.00 This is a purely individual assignment! 1 Pointer Manipulations Pointer Casts and Data Accesses Viewing Memory The contents of a block of memory may be viewed as a collection of hex nybbles indicating the contents of the byte in the memory region; for example, here's a block of 512 bytes: 7F1022EC2BEAD1F54E9262897A7E39039EF20A22F84AE7F28A40241BCA9ED049 AFF236DADC07CF2B9932DF9DFDA2D19B94DBBD8D26A47FB0E5A4CBAF429BF8F1 8E2ECC6A359B95CECD746BCA163C66AB1823383EC8B7EEAD5BB95C9E0BD886E2 835B4DB8F7E287C457F28F6D2FF51847185085E008738D632CE901803E9163C1 ECB079E39200A8E9F33757222C6F6944C0EE25C861B22B8D9C2D0DDABE709BAA 20148EB315369C086DF32A996393DD238102EBE2B5166F897A7C2B01EDC6AC0D DA3AC0EF705DF7DD502176B3B453D63556C1170BD8865C1B03871DF04DC9FD27 03BE17731B0E506B30C61FE419F51A6FB7317A8FB8D6AABB5DC7ABAA90A8D293 66E90681F756ED271C0C0C360126A5B85720470FF6F2CA54B975FE4A1ED0DD84 897A7E3903F3D857FFE48D000A32B96252007149F23C9DACB19BF6CF6CD35425 B75AD6F24DAF494C93D64C9E0805005B0671A4F8AD41A45FDC9A2E486E826E25 2CE901803E9163C18102EBE2B5166F89DC440BD8F360758736C2253FC7259ACD 963EC6447F6AA35B05D1A4735412983026A47FB0E5A4CBAF39034754682D0DDA 56B05A4A10CFD14791F60BD8862026B15EECF5DD5798385C6ADCCFBEEE67EE45 17488F2818606FA956F50271152922731518506CB088C81A6597D853FFC79816 0F273E2787ADD1DDA2D34EB7FC712A12897A7E39034754682D0DDAB75AD6FDA2 The memory block is a sequence of bytes; we can think of each byte as being at a particular offset from the beginning of the memory block. For example in the first row above, the byte 10 is at offset 110, and the byte 4A is at offset 2110. (Recall that a byte consists of two hex nybbles.) Another way of thinking about this is that we have an array of bytes, indexed just like the cells of any array, relative to the first byte in the memory block. If we called the array Data, then Data[1] would be 0x10 (or 1610) and Data[21] would be 0x4A (or 7410). Here is a C function that will display a selected block of bytes from such a memory block, using an array-based view of the necessary logic: /** Uses array-based logic to access a specified portion of a region of * memory and print the corresponding bytes to a supplied file stream. * * Pre: Out is open on a file * Base[0] is the first byte of the memory region to be examined * Index is the index of the first relevant byte of the memory region * nToPrint is the number of bytes to be printed * Restrictions: * You may not use any pointer syntax in accessing the data. */ void showBytesAtIndex(FILE* Out, const uint8_t Base[], uint16_t Index, uint8_t nToPrint) { for (uint8_t pos = 0; pos < ntoprint; pos++) { fprintf(out, "%02x ", base[index + pos]); } fprintf(out, "\n"); } cs 2505 computer organization i c04: pointers in c version 6.00 this is a purely individual assignment! 2 suppose we executed the call: showbytesatindex(stdout, data, 34, 6) now, data[34] would be the byte 36 near the beginning of the second row of the display, and the function would print 6 bytes starting there, with two spaces separating the bytes: 36 da dc 07 cf 2b (remember, a hex nybble is a 4-bit value, so there are two nybbles, and hence two hex digits, per byte.) let's consider a few details in the implementation of that function:  we used const in specifying the second parameter. that means the function is not allowed to modify the contents of the array that's passed to it.  we refer to the bytes in the memory block using the type unit8_t; that's so we can avoid any issues that might arise if the high bit of a byte happened to be 1 (remember 2's complement representation).  the parameter index is of type uint16_t; that limits the size of the memory block. the maximum value of a uint16_t variable is 216 – 1 or 65535. there's no good reason for that, really. it just gave me an excuse to add this to the discussion. a similar point could be made about the parameter ntoprint.  the format string "%02x " causes the variable to be displayed in two columns, with a leading 0 if necessary, in hexadecimal, and followed by two spaces. but the memory display shown above is not very human-friendly. a more readable version would format the data so the individual bytes were separated, and indicate the offsets of those bytes as offsets relative to the beginning of the block: 0 1 2 3 4 5 6 7 8 9 a b c d e f 0 7f 10 22 ec 2b ea d1 f5 4e 92 62 89 7a 7e 39 03 1 9e f2 0a 22 f8 4a e7 f2 8a 40 24 1b ca 9e d0 49 2 af f2 36 da dc 07 cf 2b 99 32 df 9d fd a2 d1 9b 3 94 db bd 8d 26 a4 7f b0 e5 a4 cb af 42 9b f8 f1 4 8e 2e cc 6a 35 9b 95 ce cd 74 6b ca 16 3c 66 ab 5 18 23 38 3e c8 b7 ee ad 5b b9 5c 9e 0b d8 86 e2 6 83 5b 4d b8 f7 e2 87 c4 57 f2 8f 6d 2f f5 18 47 7 18 50 85 e0 08 73 8d 63 2c e9 01 80 3e 91 63 c1 8 ec b0 79 e3 92 00 a8 e9 f3 37 57 22 2c 6f 69 44 9 c0 ee 25 c8 61 b2 2b 8d 9c 2d 0d da be 70 9b aa a 20 14 8e b3 15 36 9c 08 6d f3 2a 99 63 93 dd 23 b 81 02 eb e2 b5 16 6f 89 7a 7c 2b 01 ed c6 ac 0d c da 3a c0 ef 70 5d f7 dd 50 21 76 b3 b4 53 d6 35 d 56 c1 17 0b d8 86 5c 1b 03 87 1d f0 4d c9 fd 27 e 03 be 17 73 1b 0e 50 6b 30 c6 1f e4 19 f5 1a 6f f b7 31 7a 8f b8 d6 aa bb 5d c7 ab aa 90 a8 d2 93 10 66 e9 06 81 f7 56 ed 27 1c 0c 0c 36 01 26 a5 b8 11 57 20 47 0f f6 f2 ca 54 b9 75 fe 4a 1e d0 dd 84 12 89 7a 7e 39 03 f3 d8 57 ff e4 8d 00 0a 32 b9 62 13 52 00 71 49 f2 3c 9d ac b1 9b f6 cf 6c d3 54 25 14 b7 5a d6 f2 4d af 49 4c 93 d6 4c 9e 08 05 00 5b 15 06 71 a4 f8 ad 41 a4 5f dc 9a 2e 48 6e 82 6e 25 16 2c e9 01 80 3e 91 63 c1 81 02 eb e2 b5 16 6f 89 17 dc 44 0b d8 f3 60 75 87 36 c2 25 3f c7 25 9a cd 18 96 3e c6 44 7f 6a a3 5b 05 d1 a4 73 54 12 98 30 19 26 a4 7f b0 e5 a4 cb af 39 03 47 54 68 2d 0d da 1a 56 b0 5a 4a 10 cf d1 47 91 f6 0b d8 86 20 26 b1 1b 5e ec f5 dd 57 98 38 5c 6a dc cf be ee 67 ee 45 1c 17 48 8f 28 18 60 6f a9 56 f5 02 71 15 29 22 73 1d 15 18 50 6c b0 88 c8 1a 65 97 d8 53 ff c7 98 16 1e 0f 27 3e 27 87 ad d1 dd a2 d3 4e b7 fc 71 2a 12 1f 89 7a 7e 39 03 47 54 68 2d 0d da b7 5a d6 fd a2 this is known as a hexdump view. the value in the first column shows the first two (hex) digits of the offset of the data displayed in that row. the column heading for a byte shows the last digit of the offset of that byte. 0x01 at offset 0x01 or 110 0x4a at offset 0x15 or 2110 0xaa at offset 0xfb or 25110 0x39 at offset 0x1f3 or 49910 cs 2505 computer organization i c04: pointers in c version 6.00 this is a purely individual assignment! 3 the functions for this assignment, you'll implement six c functions to perform different kinds of accesses to a block of memory. be sure to pay attention to the restrictions that are imposed on your implementation. in particular, each function is restricted to using pointer notation to manage all accesses to the data; use of array bracket notation will result in a score of 0. the first function you will implement ntoprint;="" pos++)="" {="" fprintf(out,="" "%02x="" ",="" base[index="" +="" pos]);="" }="" fprintf(out,="" "\n");="" }="" cs="" 2505="" computer="" organization="" i="" c04:="" pointers="" in="" c="" version="" 6.00="" this="" is="" a="" purely="" individual="" assignment!="" 2="" suppose="" we="" executed="" the="" call:="" showbytesatindex(stdout,="" data,="" 34,="" 6)="" now,="" data[34]="" would="" be="" the="" byte="" 36="" near="" the="" beginning="" of="" the="" second="" row="" of="" the="" display,="" and="" the="" function="" would="" print="" 6="" bytes="" starting="" there,="" with="" two="" spaces="" separating="" the="" bytes:="" 36="" da="" dc="" 07="" cf="" 2b="" (remember,="" a="" hex="" nybble="" is="" a="" 4-bit="" value,="" so="" there="" are="" two="" nybbles,="" and="" hence="" two="" hex="" digits,="" per="" byte.)="" let's="" consider="" a="" few="" details="" in="" the="" implementation="" of="" that="" function:="" ="" we="" used="" const="" in="" specifying="" the="" second="" parameter.="" that="" means="" the="" function="" is="" not="" allowed="" to="" modify="" the="" contents="" of="" the="" array="" that's="" passed="" to="" it.="" ="" we="" refer="" to="" the="" bytes="" in="" the="" memory="" block="" using="" the="" type="" unit8_t;="" that's="" so="" we="" can="" avoid="" any="" issues="" that="" might="" arise="" if="" the="" high="" bit="" of="" a="" byte="" happened="" to="" be="" 1="" (remember="" 2's="" complement="" representation).="" ="" the="" parameter="" index="" is="" of="" type="" uint16_t;="" that="" limits="" the="" size="" of="" the="" memory="" block.="" the="" maximum="" value="" of="" a="" uint16_t="" variable="" is="" 216="" –="" 1="" or="" 65535.="" there's="" no="" good="" reason="" for="" that,="" really.="" it="" just="" gave="" me="" an="" excuse="" to="" add="" this="" to="" the="" discussion.="" a="" similar="" point="" could="" be="" made="" about="" the="" parameter="" ntoprint.="" ="" the="" format="" string="" "%02x="" "="" causes="" the="" variable="" to="" be="" displayed="" in="" two="" columns,="" with="" a="" leading="" 0="" if="" necessary,="" in="" hexadecimal,="" and="" followed="" by="" two="" spaces.="" but="" the="" memory="" display="" shown="" above="" is="" not="" very="" human-friendly.="" a="" more="" readable="" version="" would="" format="" the="" data="" so="" the="" individual="" bytes="" were="" separated,="" and="" indicate="" the="" offsets="" of="" those="" bytes="" as="" offsets="" relative="" to="" the="" beginning="" of="" the="" block:="" 0="" 1="" 2="" 3="" 4="" 5="" 6="" 7="" 8="" 9="" a="" b="" c="" d="" e="" f="" 0="" 7f="" 10="" 22="" ec="" 2b="" ea="" d1="" f5="" 4e="" 92="" 62="" 89="" 7a="" 7e="" 39="" 03="" 1="" 9e="" f2="" 0a="" 22="" f8="" 4a="" e7="" f2="" 8a="" 40="" 24="" 1b="" ca="" 9e="" d0="" 49="" 2="" af="" f2="" 36="" da="" dc="" 07="" cf="" 2b="" 99="" 32="" df="" 9d="" fd="" a2="" d1="" 9b="" 3="" 94="" db="" bd="" 8d="" 26="" a4="" 7f="" b0="" e5="" a4="" cb="" af="" 42="" 9b="" f8="" f1="" 4="" 8e="" 2e="" cc="" 6a="" 35="" 9b="" 95="" ce="" cd="" 74="" 6b="" ca="" 16="" 3c="" 66="" ab="" 5="" 18="" 23="" 38="" 3e="" c8="" b7="" ee="" ad="" 5b="" b9="" 5c="" 9e="" 0b="" d8="" 86="" e2="" 6="" 83="" 5b="" 4d="" b8="" f7="" e2="" 87="" c4="" 57="" f2="" 8f="" 6d="" 2f="" f5="" 18="" 47="" 7="" 18="" 50="" 85="" e0="" 08="" 73="" 8d="" 63="" 2c="" e9="" 01="" 80="" 3e="" 91="" 63="" c1="" 8="" ec="" b0="" 79="" e3="" 92="" 00="" a8="" e9="" f3="" 37="" 57="" 22="" 2c="" 6f="" 69="" 44="" 9="" c0="" ee="" 25="" c8="" 61="" b2="" 2b="" 8d="" 9c="" 2d="" 0d="" da="" be="" 70="" 9b="" aa="" a="" 20="" 14="" 8e="" b3="" 15="" 36="" 9c="" 08="" 6d="" f3="" 2a="" 99="" 63="" 93="" dd="" 23="" b="" 81="" 02="" eb="" e2="" b5="" 16="" 6f="" 89="" 7a="" 7c="" 2b="" 01="" ed="" c6="" ac="" 0d="" c="" da="" 3a="" c0="" ef="" 70="" 5d="" f7="" dd="" 50="" 21="" 76="" b3="" b4="" 53="" d6="" 35="" d="" 56="" c1="" 17="" 0b="" d8="" 86="" 5c="" 1b="" 03="" 87="" 1d="" f0="" 4d="" c9="" fd="" 27="" e="" 03="" be="" 17="" 73="" 1b="" 0e="" 50="" 6b="" 30="" c6="" 1f="" e4="" 19="" f5="" 1a="" 6f="" f="" b7="" 31="" 7a="" 8f="" b8="" d6="" aa="" bb="" 5d="" c7="" ab="" aa="" 90="" a8="" d2="" 93="" 10="" 66="" e9="" 06="" 81="" f7="" 56="" ed="" 27="" 1c="" 0c="" 0c="" 36="" 01="" 26="" a5="" b8="" 11="" 57="" 20="" 47="" 0f="" f6="" f2="" ca="" 54="" b9="" 75="" fe="" 4a="" 1e="" d0="" dd="" 84="" 12="" 89="" 7a="" 7e="" 39="" 03="" f3="" d8="" 57="" ff="" e4="" 8d="" 00="" 0a="" 32="" b9="" 62="" 13="" 52="" 00="" 71="" 49="" f2="" 3c="" 9d="" ac="" b1="" 9b="" f6="" cf="" 6c="" d3="" 54="" 25="" 14="" b7="" 5a="" d6="" f2="" 4d="" af="" 49="" 4c="" 93="" d6="" 4c="" 9e="" 08="" 05="" 00="" 5b="" 15="" 06="" 71="" a4="" f8="" ad="" 41="" a4="" 5f="" dc="" 9a="" 2e="" 48="" 6e="" 82="" 6e="" 25="" 16="" 2c="" e9="" 01="" 80="" 3e="" 91="" 63="" c1="" 81="" 02="" eb="" e2="" b5="" 16="" 6f="" 89="" 17="" dc="" 44="" 0b="" d8="" f3="" 60="" 75="" 87="" 36="" c2="" 25="" 3f="" c7="" 25="" 9a="" cd="" 18="" 96="" 3e="" c6="" 44="" 7f="" 6a="" a3="" 5b="" 05="" d1="" a4="" 73="" 54="" 12="" 98="" 30="" 19="" 26="" a4="" 7f="" b0="" e5="" a4="" cb="" af="" 39="" 03="" 47="" 54="" 68="" 2d="" 0d="" da="" 1a="" 56="" b0="" 5a="" 4a="" 10="" cf="" d1="" 47="" 91="" f6="" 0b="" d8="" 86="" 20="" 26="" b1="" 1b="" 5e="" ec="" f5="" dd="" 57="" 98="" 38="" 5c="" 6a="" dc="" cf="" be="" ee="" 67="" ee="" 45="" 1c="" 17="" 48="" 8f="" 28="" 18="" 60="" 6f="" a9="" 56="" f5="" 02="" 71="" 15="" 29="" 22="" 73="" 1d="" 15="" 18="" 50="" 6c="" b0="" 88="" c8="" 1a="" 65="" 97="" d8="" 53="" ff="" c7="" 98="" 16="" 1e="" 0f="" 27="" 3e="" 27="" 87="" ad="" d1="" dd="" a2="" d3="" 4e="" b7="" fc="" 71="" 2a="" 12="" 1f="" 89="" 7a="" 7e="" 39="" 03="" 47="" 54="" 68="" 2d="" 0d="" da="" b7="" 5a="" d6="" fd="" a2="" this="" is="" known="" as="" a="" hexdump="" view.="" the="" value="" in="" the="" first="" column="" shows="" the="" first="" two="" (hex)="" digits="" of="" the="" offset="" of="" the="" data="" displayed="" in="" that="" row.="" the="" column="" heading="" for="" a="" byte="" shows="" the="" last="" digit="" of="" the="" offset="" of="" that="" byte.="" 0x01="" at="" offset="" 0x01="" or="" 110="" 0x4a="" at="" offset="" 0x15="" or="" 2110="" 0xaa="" at="" offset="" 0xfb="" or="" 25110="" 0x39="" at="" offset="" 0x1f3="" or="" 49910="" cs="" 2505="" computer="" organization="" i="" c04:="" pointers="" in="" c="" version="" 6.00="" this="" is="" a="" purely="" individual="" assignment!="" 3="" the="" functions="" for="" this="" assignment,="" you'll="" implement="" six="" c="" functions="" to="" perform="" different="" kinds="" of="" accesses="" to="" a="" block="" of="" memory.="" be="" sure="" to="" pay="" attention="" to="" the="" restrictions="" that="" are="" imposed="" on="" your="" implementation.="" in="" particular,="" each="" function="" is="" restricted="" to="" using="" pointer="" notation="" to="" manage="" all="" accesses="" to="" the="" data;="" use="" of="" array="" bracket="" notation="" will="" result="" in="" a="" score="" of="" 0.="" the="" first="" function="" you="" will="">
Answered 2 days AfterMar 20, 2022

Answer To: Purpose: To provide an introduction to structured programming using the C/C++ language. CS 2505...

Nidhi answered on Mar 22 2022
126 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here