Queue/Usage: Difference between revisions

no edit summary
No edit summary
Line 33:
bar
baz
</pre>
=={{header|6502 Assembly}}==
Implementing a queue is very similar to a software stack, except the <code>POP</code> command is a litte more involved. The basic principles are the same: Before the queue can be used, a "queue pointer" must first be loaded into X, which points to the first empty slot in the queue. The queue grows down in memory as new elements join the queue. This software queue uses the zero page as the storage area.
 
 
<lang 6502asm>
queuePointerStart equ #$FD
queuePointerMinus1 equ #$FC ;make this equal whatever "queuePointerStart" is, minus 1.
pushQueue:
STA 0,x
DEX
RTS
 
popQueue:
STX temp
LDX #queuePointerMinus1
loop_popQueue:
LDA 0,X
STA 1,X
DEX
CPX temp
BNE loop_popQueue
LDX temp
INX
RTS
 
isQueueEmpty:
LDA #1
CPX #queuePointerStart
BEQ yes ;return 1
 
SEC
SBC #1 ;return 0
 
yes:
RTS</lang>
 
===PUSH===
This example uses Easy6502 to test the various modes. The first test pushes three values into the queue. For all examples, the subroutines above are defined below the <code>BRK</code>.
 
<lang 6502asm>define temp $00
define queueEmpty $FD
define queueAlmostEmpty $FC
 
LDX #queueEmpty ;set up software queue
 
LDA #$40
jsr pushQueue
 
LDA #$80
jsr pushQueue
 
LDA #$C0
jsr pushQueue
 
brk</lang>
 
Output of Example 1:
<pre>
Queue Pointer = $FA
 
Hexdump of $00fa: 00 c0 80 40
Address of each: (FA FB FC FD)
</pre>
===POP===
<lang 6502asm>define temp $00
define queueEmpty $FD
define queueAlmostEmpty $FC
 
LDX #queueEmpty ;set up software queue
 
LDA #$40
jsr pushQueue
 
LDA #$80
jsr pushQueue
 
LDA #$C0
jsr pushQueue
 
jsr popQueue
 
brk</lang>
 
Output of Example 2:
<pre>
Queue Pointer = $FB
Hexdump of $00FB: c0 c0 80
Address of each: (FB FC FD)
 
Note that c0 still exists in FB, but its slot is "empty" so it will get overwritten in the 3rd example.
</pre>
 
===PUSH,POP,PUSH===
<lang 6502asm>define temp $00
define queueEmpty $FD
define queueAlmostEmpty $FC
 
 
LDX #queueEmpty ;set up software queue
 
LDA #$40
jsr pushQueue
 
LDA #$80
jsr pushQueue
 
LDA #$C0
jsr pushQueue
 
jsr popQueue
 
lda #$ff
jsr pushQueue
 
brk</lang>
 
Output of Example 3:
<pre>
Queue Pointer = $FA
Hexdump of $00FA: 00 ff c0 80
Address of each: (FA FB FC FD)
</pre>
 
1,489

edits