Mnemonics

IND

Input and Output Group
Expansion:
INput and Decrement
Usage:
ind

Reads input from the port address found in memory at pointer C register i.e. (C) and saves the result to the address pointer (HL). It then decrements HL and B registers.

INI (Input and Increment) is generally more intuitive because it reads data sequentially, storing it in memory in the natural, forward direction. However, IND (Input and Decrement) has its own use cases and advantages, especially in certain scenarios where the direction of memory storage matters.

Here are some reasons why IND might be needed:

Stack Operations and Buffering:

  • Buffers: Sometimes, data might need to be placed in a buffer or stack where the end of the buffer is filled first, rather than the beginning. This is particularly useful for Last-In-First-Out (LIFO) structures.
  • Stack Operations: In some stack-related operations, you might want to decrement the pointer to store data at the end of the stack.

Reverse Data Reading:

  • Reverse Order: If you need to read data into memory in reverse order, IND is a straightforward way to achieve this. This might be required for certain algorithms or data structures where data needs to be processed from the end towards the beginning.
  • Data Packing: In some communication protocols or data-packing schemes, data may need to be stored in reverse order for efficient retrieval or transmission.

Dual Buffer Management:

  • Dual Buffers: When working with dual buffers, one buffer might be filled in a forward direction while the other is filled in reverse. IND allows you to handle such scenarios efficiently.
  • Ping-Pong Buffers: In systems using ping-pong buffers (double buffering), one buffer can be filled with INI while the other is filled with IND, depending on the specific requirements of the application.

Hardware Constraints

  • Peripheral Behavior: Certain hardware peripherals or devices might require data to be read or written in a specific order. IND provides flexibility to accommodate these constraints.
  • Timing Considerations: In real-time systems, the direction in which data is stored can impact the timing and synchronization of operations, making IND necessary for precise control.
Z80.CPU instructions
Instruction Description Opcode
IND INput and Decrement EDAA

Example

; Example ld bc, $00FE ; load BC with the address of port 254 ($FE) and the counter in B ld hl, $4000 ; load HL with the starting address of the memory block ld b, $10 ; load B with the number of bytes to read (16 bytes in this example) ReadLoop: ind ; input a byte from port C ($FE), store it at (HL), decrement HL, decrement B jp nz, ReadLoop ; repeat the loop if B is not zero ; after the loop, HL will be decremented by the number of bytes read, ; and the memory block at $4000 will contain the data from the I/O port

See also