FrostyStraw
FrostyStraw

Reputation: 1656

Really confused about bytes and words

I'm having a really difficult time understanding this concept. I'm reading my Computer Architecture book, and it is giving an example of how to write the following C code into MIPS code

g = h + A[8]

and it says:

"Let’s assume that A is an array of 100 words and that the compiler has associated the variables g and h with the registers $s1 and $s2 as before. Let's also assume that the starting address, or base address, of the array is in $s3"

The first thing I'm confused about is what it means when it says A is an array of 100 words. I know from programming in C++ and Java that an array can hold many things, including integers, floating-point numbers, objects, pointers, etc. So when it says "an array of 100 words", what exactly is it referring to? If there is an array of integers, does each integer in the array represent a word?

Continuing from the example from above, it originally gave the answer in MIPS as

lw $t0, 8($s3), and said "The address of this array element is the sum of the base of the array A, found in register $s3, plus the number to select element 8."-->then it goes on to explain in the next page (after explaining byte-addressing) that the answer is actually

lw $t0, 32($s3). This is the part where I get confused again, since I didn't understand byte-addressing. It says:

"Since 8-bit bytes are useful in many programs, most architectures address individual bytes. Therefore, the address of a word matches the address of one of the 4 bytes within the word. Hence, addresses of sequential words differ by 4....Byte addressing also affects the array index. To get the proper byte address in the code above, the offset to be added to the base register $s3 must be 4 x 8, or 32, so that the load address will select A[8] and not A[8/4]. "

And to be completely honest, I have no clue what this even means, and I know it's a lot but can somebody try to help me understand this, because my mind is just not getting this even when I'm looking up like 100 resources

Upvotes: 1

Views: 2879

Answers (3)

RobertB
RobertB

Reputation: 1076

Your problem likely stems from this statement: "I know from programming in C++ and Java..." When you get to the machine code level, you have to forget everything you learned in higher level languages. Even the concept of an "Array" takes on an entirely different meaning.

Where you've thought of an array as a sequence of well-defined, strongly-typed elements, that's no longer the case. There is no "type" (only Zuul) when you're at the bit level. When someone says "an array of X" in a MIPS context, it's just a bunch of memory locations containing 1's and 0's. The smallest unit of memory that all modern processors address is an 8-bit byte, and the "native" larger unit of memory that MIPS (or any 32-bit processor) addresses is a 4-byte (32-bit) word.

So if someone says "A is an array of 100 bytes", it means this: the label "A" points to the address of a chunk of memory 100 bytes long, which we have reserved for our use and will reference as bytes. If you look at that memory, you would see:

A+0:  00000000
A+1:  00000000
A+2:  00000000
...
A+99: 00000000

Now, someone comes along and says "A is an array of 100 words". Now, they mean that the label "A" points to the address of a chunk of memory 100 words long. Since each word is 4 bytes, that chunk of memory needs to be 400 bytes long. And because the byte is still the basic unit of memory, you have to adjust your pointer to account for that fact.

A+0*4:  00000000 00000000 00000000 00000000
A+1*4:  00000000 00000000 00000000 00000000
A+2*4:  00000000 00000000 00000000 00000000
...
A+99*4: 00000000 00000000 00000000 00000000

Does that explain why this statement gets the 8th byte...

lw $t0, 8($s3)

...but this statement is how you get the 8th word?

lw $t0, 32($s3) # 32 = 8*4

Upvotes: 3

Naim FS
Naim FS

Reputation: 61

A byte is 8 bit And a word is 32 Bit A word is 4byte This will solve most of the confussion

Upvotes: 0

Naim FS
Naim FS

Reputation: 61

Array of 100 words means an array occupying 100*32 bit data every data is placed 32 bit apart

for example to access the second element of the array u refer to position no 32 of the array

0-31--1st word 32-63--2nd word and so on

Upvotes: 0

Related Questions