\ > RunCode 1.02 23-04-88 \ .RunCodeLoad \ Load and run machine code \ \ On entry: \ On exit: \ HL= load address \ registers corrupted \ DE= execute address \ \ BC= file length \ \ SP=>[ret_addr] \ \ \ If load address is zero, run as a relocatable utility on the stack \ **WARNING** There are no limits checks to see if there is enough \ space to make room on the stack. \ \ The called code can mess about with the stack. Once it manages to \ return the stack is restored. The machine code is called with \ registers set up in Z80Tube manner. \ LD A,H :\ Is load address zero? OR L :\ Also clears carry PUSH HL :\ Save HL LD HL,0 ADD HL,SP :\ Get SP to HL EX (SP),HL :\ HL=oldHL, SP=>[oldSP],[ret] JR NZ,RunCodeAbsolute SBC HL,BC :\ HL => new stack location POP BC :\ Get old stack location back LD SP,HL :\ Set up stack PUSH BC :\ Save old stack location on new stack \ \ HL=>new stack, address to load at \ BC=>old stack, end address for load \ DE= execution offset \ EX DE,HL ADD HL,DE EX DE,HL \ \ HL=>new stack, address to load at \ BC=>old stack, end address for load \ DE=>execution address \ PUSH BC :\ Swap HL and BC LD B,H LD C,L :\ BC=load address POP HL :\ HL=end address AND A SBC HL,BC :\ HL=end-start=length PUSH BC :\ Swap HL and BC again LD B,H LD C,L :\ BC=length POP HL :\ HL=load address \ .RunCodeAbsolute \ Load code and call it's entry point CALL LoadToHL :\ BC-DE-HL preserved CP A :\ Set Z to signal balanced stack EX DE,HL :\ Put entry address in HL \ \ Code is called with registers containing: \ \ HL=entry address \ DE=load address \ BC=file length \ SP=>[ret to here],oldSP \ oldSP is value to restore SP to after load and run. It may be just \ current SP+2, or may be result of reserving space for transient \ utility to run. \ CALL CallHL POP HL LD SP,HL :\ Restore stack (won't have changed for absolute code) RET \ .CallHL JP (HL) \