Search code examples
memoryassemblyx86nasmosdev

Implementing queue in nasm assembly


How do I implement queue structure in assembly (x86, 32bit protected mode)? It's simple to implement it like stack, but then I have to move every item one place when taking object from it. Linked list is also possible, but it is not very memory-efficients nor fast.

I'm developing my own operating system in plain assembly, so I can not use OS functions.


Solution

  • If you maintain start and end pointers, you can still use the CPU stack. Yes, you do have to move items from time to time, but not every single time: only when you hit the top of the stack. Because you're maintaining start and end pointers, you don't have to shift by one every time, but can shift by, say, 16, and then you can insert 15 more items before having to shift again.

    For bonus points, make the shift quantity exponential (you'd have to use a third register for keeping the shift quantity), so that the first time you shift 16, the next time you shift 32, etc.