I understand what XTHL does, but can someone give an example of a scenario where one would actually use it? Moreover, if possible please share the code for the application as well.
One use includes passing arguments to assembly functions like this:
CALL function_name
DB 1,2,3 ;arguments
...
function_name:
XTHL
MOV A,M ;get argument
INX H ;move to next argument
...
XTHL ;now return modified return address to stack,
;also restoring HL contents as well
Other use is to have shadow copy of HL on top of stack, for example:
DAD B ;hl=hl+bc
XTHL ;'shadow' hl swapped in
JNC no_inc
INX H
no_inc:
DAD D ;hl=hl+de
XTHL
;so we just added 32-bit number in DE:BC to HL:(TOS)
In similar way you juggle words that are pushed on the stack.
Some abuse also includes this:
LXI SP,addr1
XTHL
LXI SP,addr2
XTHL
...
this code will move chain of data words through locations pointed by consecutive LXI SP,addr
commands. Of course, this requires interrupts disabling, but gain is speed.