4

If in a computer an instruction is of 16 bits and if memory is organized as 16-bits words, then the address of the next instruction is evaluated by adding one in the address of the current instruction. In case, the memory is organized as bytes, which can be addressed individually, then we need to add two in the current instruction address to get the address of the next instruction to be executed in sequence. Why is it so?? Please explain this concept. I am new to computer organisation and assembly language programming so any help is appreciated. Thanks.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
Kuldeep Sarma
  • 43
  • 1
  • 5
  • its like keeping your shoes in your closet in boxes vs keeping them loose. In one case you need to count over N boxes, the box of shoes being the unit. In another you need to count over N*2 number of shoes to find a specific pair. Or to find a particular left shoe for some reason. You either have to count in units of boxes then within a box take the left shoe or you count over some number of shoes in units of shoes. – old_timer Mar 16 '19 at 16:22

2 Answers2

5

Your question does not say what architecture you are referring to.

Talking about designs that do not allow instructions to be aligned to bytes, the behavior you describe differs from CPU to CPU!

First we look at the meaning of the "address" on 8-bit CPUs. On such CPUs the address is increased by 1 when going from one byte to the next byte in memory:

Address   Meaning
0         1st byte in memory
1         2nd byte in memory
2         3rd byte in memory
3         4th byte in memory
4         5th byte in memory
...

The 68000 uses a similar addressing like 8-bit CPUs. However, the memory is actually organized in 16-bit units and instructions must start at an even address and are a multiple of 16 bits long. Therefore the program counter always contains an even value. It will increase by 2 or a multiple of 2 during each instruction.

(Using odd addresses is only allowed for byte-wise memory access (read/write) which will actually perform a 16-bit memory access in the background.)

For the TMS9900 (a 16-bit CPU) the address is increasing by 1 for 16 bits; the bytes in between can be accessed but the addresses are formed by adding 0x8000:

Address   Meaning
0         1st byte in memory
0x8000    2nd byte in memory
1         3rd byte in memory
0x8001    4th byte in memory
2         5th byte in memory
...

The program counter may contain an odd or an even value here, but not a value above 0x7FFF because this would refer to a byte which is not 16-bit aligned. Of course, the program counter will increase by 1 when the instruction is 16 bits long.

The TMS320 does not allow addressing odd bytes:

Address       Meaning
0             1st + 2nd byte in memory
not possible  2nd + 3rd byte in memory
1             3rd + 4th byte in memory
2             5th + 6th byte in memory
...

In this design the program counter will also increase by 1 when the instruction is 16 bits long.

The LittleMIPS (or similar; I don't remember the correct name) design is a reduced MIPS CPU intended for students to learn microchip design. It allows only 32-bit memory access and only 4-aligned addresses:

Address       Meaning
0             1st to 4th byte in memory
1             address does not exist
2             address does not exist
3             address does not exist
4             5th to 8th byte in memory
5             address does not exist
...

On this design the program counter will always contain a multiple of 4. Because each instruction is exactly 4 bytes long, the program counter will increase by 4 during an instruction.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • 2
    The first couple generations of DEC Alpha were like LittleMIPS: byte addresses, but only aligned 4-byte and 8-byte loads/stores were actually possible. There were no `lbu` / `sb` or `lhu` / `sh` instructions to load/store anything narrower than a 32-bit word, until an ISA extension added them. (MMIO regions could be mapped to a special byte<->word region of physical address space, making it possible to write drivers for PCI devices with adjacent byte registers.) – Peter Cordes Mar 15 '19 at 15:34
  • 1
    I was referring to the 8086 processor. – Kuldeep Sarma Mar 18 '19 at 20:58
  • 2
    @KuldeepSarma Addressing on the 8086 works similar to 68000. However, unlike 68000 the x86 CPUs also know instructions which are only 8 bits long. This also means that instructions may start at any address. The program counter is incremented by 1 if the instruction is 8 bits long and it is incremented by 2 if it is 16 bits long. – Martin Rosenau Mar 18 '19 at 21:10
3

If the smallest addressable unit is 2 bytes, then adjacent 2-byte instruction words are only 1 address apart.

The whole point of word-addressable memory is to avoid wasting an address bit on byte-within-word in a system that can't do byte loads/stores in the first place.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    Sorry, I completely ignored the fact that the instructions are 16 bits which is 2 bytes. So in a byte organised system each instruction will take up 2 bytes. Therefore the next instruction will be 16 bits or 2 bytes away from the current instruction. That is why, to fetch the next instruction we have to add 2 to the Program Counter. – Kuldeep Sarma Mar 14 '19 at 20:54
  • 1
    @KuldeepSarma: yup, that's correct. If this answers your question, please click the "accept" checkmark under the vote arrows. – Peter Cordes Mar 14 '19 at 23:23