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:
Code:
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:
Code:
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:
Code:
This gets the contents of 1337 into hl, which is 42, instead of putting the number 1337 into hl. Makes sense?
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:
Code:
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:
Code:
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:
Code:
ld hl,(1337)
This gets the contents of 1337 into hl, which is 42, instead of putting the number 1337 into hl. Makes sense?