Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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 »

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 ny
les 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 XXXXXXXXXXRecall
that a byte consists of two hex ny
les.)
Another way of thinking about this is that we have an a
ay of bytes, indexed just like the cells of any a
ay, relative to the
first byte in the memory block. If we called the a
ay 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 a
ay-based view of the
necessary logic:

** Uses a
ay-based logic to access a specified portion of a region of
* memory and print the co
esponding bytes to a supplied file stream.
*
* Pre: Out is open on a file
* XXXXXXXXXXBase[0] is the first byte of the memory region to be examined
* XXXXXXXXXXIndex is the index of the first relevant byte of the memory region
* XXXXXXXXXXnToPrint 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,
XXXXXXXXXXuint8_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
ytes starting there, with two spaces separating the bytes: 36 DA DC 07 CF 2B
(Remember, a hex ny
le is a 4-bit value, so there are two ny
les, 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 a
ay 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 XXXXXXXXXXThere'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:

XXXXXXXXXX XXXXXXXXXX A B C D E F
0 7F 10 22 EC 2B EA D1 F5 4E XXXXXXXXXX7A 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 XXXXXXXXXXDF 9D FD A2 D1 9B
XXXXXXXXXXDB 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
XXXXXXXXXX3E C8 B7 EE AD 5B B9 5C 9E 0B D8 86 E2
XXXXXXXXXX5B 4D B8 F7 E2 87 C XXXXXXXXXXF2 8F 6D 2F F5 18 47
XXXXXXXXXXE XXXXXXXXXX8D XXXXXXXXXX2C E XXXXXXXXXX3E 91 63 C1
8 EC B0 79 E XXXXXXXXXXA8 E9 F XXXXXXXXXX2C 6F 69 44
9 C0 EE 25 C8 61 B2 2B 8D 9C 2D 0D DA BE 70 9B AA
A XXXXXXXXXX8E B XXXXXXXXXX9C XXXXXXXXXX6D F3 2A XXXXXXXXXXDD 23
B XXXXXXXXXXEB E2 B5 16 6F XXXXXXXXXX7A 7C 2B 01 ED C6 AC 0D
C DA 3A C0 EF 70 5D F7 DD XXXXXXXXXXB3 B4 53 D6 35
D 56 C1 17 0B D8 86 5C 1B XXXXXXXXXX1D F0 4D C9 FD 27
E 03 BE 17 73 1B 0E 50 6B XXXXXXXXXXC6 1F E4 19 F5 1A 6F
F B7 31 7A 8F B8 D6 AA BB 5D C7 AB AA 90 A8 D2 93
XXXXXXXXXXE XXXXXXXXXXF7 56 ED XXXXXXXXXX1C 0C 0C XXXXXXXXXXA5 B8
XXXXXXXXXX0F F6 F2 CA XXXXXXXXXXB9 75 FE 4A 1E D0 DD 84
XXXXXXXXXX7A 7E 39 03 F3 D XXXXXXXXXXFF E4 8D 00 0A 32 B9 62
XXXXXXXXXXF2 3C 9D AC B1 9B F6 CF 6C D3 54 25
14 B7 5A D6 F2 4D AF 49 4C XXXXXXXXXXD6 4C 9E XXXXXXXXXX5B
XXXXXXXXXXA4 F8 AD 41 A4 5F DC 9A 2E 48 6E 82 6E 25
16 2C E XXXXXXXXXX3E 91 63 C XXXXXXXXXXEB E2 B5 16 6F 89
17 DC 44 0B D8 F XXXXXXXXXXC2 25 3F C7 25 9A CD
XXXXXXXXXX3E C6 44 7F 6A A3 5B XXXXXXXXXXD1 A XXXXXXXXXX
XXXXXXXXXXA4 7F B0 E5 A4 CB AF XXXXXXXXXX2D 0D DA
1A 56 B0 5A 4A 10 CF D XXXXXXXXXXF6 0B D XXXXXXXXXXB1
1B 5E EC F5 DD XXXXXXXXXX5C 6A DC CF BE EE 67 EE 45
1C XXXXXXXXXX8F XXXXXXXXXX6F A XXXXXXXXXXF XXXXXXXXXX
1D XXXXXXXXXX6C B0 88 C8 1A XXXXXXXXXXD8 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 XXXXXXXXXX2D 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 a
ay
acket notation will result in a score of 0.
The first function you will implement
Answered 2 days After Mar 20, 2022

Solution

Nidhi answered on Mar 22 2022
113 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