Question

I prefer to code on my calculator in hex. I know my opcodes and what not, but i'm not sure how to create a string "hello" in register HL. Cany you help? I have googled it, but it's hard to find an answer because most people use compilers that handle that for them. I want to do this the hard and challenging way though. But I want the most efficient way of doing it in hex.

I tried....

[code here] C9 - RETURN [STRING HERE]

...but it doesn't seem LD has a way to get relative data. So I can't even get the address location after C9. Any tips?

Was it helpful?

Solution

You can't store 5 ASCII characters ("hello") in a 2-byte register, which HL is. The only thing you can do is store these 5 characters somewhere in the memory and load HL with the address of this string (effectively, the address of its first character "h").

EDIT:

If you need to find the location of your subroutine while it's executing, you can read the return address from the stack and then minimally disassemble the instruction just before the return address.

If it's CALL (cc,) nn or JP (cc,) nn, the subroutine's address is encoded in the instruction's two last bytes.

If it's JP (HL/IX/IY), the address is in the register (HL, IX or IY) used for the indirect jump. To be able to recover this address, you'll have to save these registers' values in your subroutine (using e.g. PUSH).

If it's RET (cc), the address was on the stack when your subroutine started, but it may since have been overwritten by your routine's activities or that of the interrupt service routines. This is a tough case, but I hope it's not RET.

You can't disassemble the instruction blindly and reliably at the same time because different instructions have different lengths and what you may recognize as JP (HL) can in fact be just a part of a longer CALL nn. But the code that invokes your subroutine is unlikely to change and chances are there's only one place or one method of invocation, which means that once you know the instruction that's used to invoke your subroutine, you don't need to guess anything anymore, just write your code assuming it's always that instruction.

Using the above technique, you can use the layout you suggested in the question:

[code here] C9 - RETURN [STRING HERE]

You just need to recover the subroutine's address and add to it the subroutine's size. That'll be the address of the string.

OTHER TIPS

Does this help?

Strings

Strings are just lots of characters put together in consecutive order. However, it is important to identify the beginning/ends of strings. So, here's how it's done:

Null-Terminating Strings Strings that have a null term, or 0 at the end.

.DB "String Data",0

Pre-determined Length Strings Strings where the first byte is the length of the string.

.DB 11,"String Data"

As you are doing things with strings, this also looks useful for null terminated strings.

TI-83+/84+ I guess? Programs start at a fixed address, $9D95, so you can manually add the offset to that and use the absolute address.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top