Q: What's with the parentheses sometimes used with ld?
A: OK, this is because of something called indirection. For simplicity's sake, let's assume you have a piece of memory that can hold exactly 2^16th bytes (sets of 8 bits). That means that you can 'address' the memory by assigning each of the 2^16 bytes with a number between 0 and 65535. Now, each of these spaces store a byte, so you want to be able to get the contents and store to each (reading and writing). Since registers can store numbers (a, b, c, d, e, h, and l hold one byte, and bc, de, hl, sp, ix, and iy hold 2 bytes each). Two bytes is exactly what we need to store 16-bit numbers, so let's put the number, hmmm, 42 in hl. That means hl will hold the number 42. In order to actually put 42 in hl, we use a regular load statement:

ld hl,42

Easy enough. But say we want to save that for later. In ourt theoretical memory, let's decide that we want the 1,338th location to hold our number. Its address is 1337 (because remember that the first location is 0), so we want to store hl into the _address_ 1337. To do that, we can use:

ld hl,1337
ld (hl),42

Here we have both methods together. First, we are simply loading the number 1337 into hl. Next, we are loading 42 into the address of memory named by hl. This will put the binary equivalent of 42 into address 1337 of memory. If we want it again, we simply do:

ld hl,(1337)

This gets the contents of 1337 into hl, which is 42, instead of putting the number 1337 into hl. Makes sense?
Yes, it somehow does. Smile
It more than "somehow does" make sense. Kerm explained it in an understandable way. I have one question though. You have to load the address to hl and then load the value to that address right? You can't simply do ld (1337) , 42?
Only because of limitations of the z80. That's essentially what I'm doing in two instructions.
Generally you don't have this problem with microcontrollers Razz
By the way, here are the C equivalents:


*hl = 42;

a = 1337;
hl = *a;
Still concerning parenthesis usage, I'd like to mention exceptions (I mean: no indirection) when used with jp :


ld hl,1000
ld ix,2000
ld iy,3000

ld (hl),22
ld (ix+0),22
ld (iy+0),22

jp (hl) ;  won't go to
jp (ix) ;  address 22
jp (iy) ;  but  respectively to address 1000,2000 or 3000

Quite easy to misunderstand, isn't it ? Smile
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum