exg.s 859 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. .define Exg
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. ! This subroutine exchanges two groups of bytes on the top of the
  8. ! stack. The groups may consist of atmost 255 bytes.
  9. ! This number is in register Y.
  10. ! The exchange is from ADDR, ADDR+1 to ADDR+2, ADDR+3
  11. Exg:
  12. lda SP+1
  13. ldx SP+2
  14. stx ADDR ! address of first group (lowbyte)
  15. sta ADDR+1 ! address of first group (highbyte)
  16. sty Ytmp ! save number of bytes to be exchanged
  17. clc
  18. lda SP+2
  19. adc Ytmp
  20. sta ADDR+2 ! address of second group (lowbyte)
  21. lda SP+1
  22. adc #0
  23. sta ADDR+3 ! address of second group (highbyte)
  24. 1: dey
  25. lda (ADDR),y ! get byte from first group
  26. pha ! temporary save
  27. lda (ADDR+2),y ! get byte from second group
  28. sta (ADDR),y ! store in first group
  29. pla ! get temporary saved byte
  30. sta (ADDR+2),y ! store in second group
  31. tya
  32. bne 1b ! perform n times
  33. rts