http://calcg.org/newlogo2.png Not Logged in.
Login | Register

Page 3: An Intro to Registers

There are 8 registers that you'll need to know, for starters: B, C, D, E, H, L, (HL), and A, in that order.  Each of thse registers is a byte, and only one of them, (HL), is stored outside of the Z80 itself; That's to say that (HL) is stored in calculator memory.

Indirection

What does "(HL)" mean?  To know that, you'll first need to know what "HL" is.  HL is a combination of register H and register L.  H and L are both bytes, meaning that they're both 8 bits.  When you combine two 8-bit registers you get a 16-bit register.  If H=11010110 (214) and L=01010101 (85), then HL=1101011001010101 (54869).  An easy way to calculate HL by only knowing H and L is to multiply H by 2^8 and add L.

An address is a point in your calculator's memory.  Most of the memory that you'll be dealing with will be RAM (random access memory), so let's say for now that an address is a point in your calc's RAM.  On most computers, things are stored in RAM according to what's available at the time and not necessarily at a specific address, making it virtually randomly accessed.  When programming on a calculator, however, you'll usually know where things are being stored, making "RAM" nothing more than a name for the hardware.  The RAM that you'll be working with has 65535 different slots where bytes can be stored.  What's so special about the number 65535?  It's the highest 16-bit number possible, equal to 1111111111111111 or FFFF.  There isn't a name for every slot.  Rather, they're addressed by their number.  Every single calculator address will be in hexidecimal, so get used to it.

We'll use AD2C, the randomest hex number I could think of off the top of my head, for this next part.  AD2C is just a 16-bit number by itself.  (AD2C) means "whatever number is at AD2C," and is either 8 bit bits or 16 bits, depending on what instruction you're executing.  Luckily for you, it can always be assumed that (HL) is an 8-bit number.

To put 2 and 2 together, let's say that HL=AD2C and that (AD2C)=4F.  You can conclude that (HL)=4F.

The LD Table

ld is, without a doubt, the most useful instruction you'll come across.  It's also the easiest to use and memorize.  It  loads a value to an address or register, depending on which you choose.  There are 64 (minus one) very similar ld possibilities that are in a table together, used for loading the values of the 8 basic 8-bit registers to each other:

 X 40+X 48+X  50+X     58+X
60+X  68+X  70+X  78+X 
 0  
ld b,b
ld c,b
ld d,b ld e,b ld h,b   
ld l,b   
ld (hl),b   
ld a,b
 1  
ld b,c    ld c,c   
ld d,c   
ld e,c   
ld h,c   
ld l,c   
ld (hl),c   
ld a,c   
 2  
ld b,d    ld c,d   
ld d,d   
ld e,d   
ld h,d   
ld l,d   
ld (hl),d   
ld a,d
 3  
ld b,e    ld c,e     ld d,e   
ld e,e   
ld h,e   
ld l,e   
ld (hl),e   
ld a,e
 
 4  
ld b,h    ld c,h    ld d,h   
ld e,h   
ld h,h   
ld l,h   
ld (hl),h   
ld a,h   
 5  
ld b,l ld c,l   
ld d,l   
ld e,l   
ld h,l   
ld l,l   
ld (hl),l   
ld a,l
 6  
ld b,(hl) ld c,(hl)
ld d,(hl)
ld e,(hl)
ld h,(hl)
ld l,(hl)
halt
ld a,(hl)
 7  
ld b,a
ld c,a
ld d,a
ld e,a
ld h,a
ld l,a
ld (hl),a
ld a,a

I see a pattern.  Do you? ;) The numbers in the top row are in hexadecimal.  Add the number above a column with the value of X for a row to get the hex value for that instruction.  For example, ld d,l is 5016+5, or 55ld a,h is 7816+4, or 7C.  It should be pretty easy to have this table in your head if you know how it works, I think.  The register to be loaded from changes by row, and the register to be loaded to changes by columnld h,b loads B to H.

What's that?  You're wondering what 77 is?  It just so happens that the Z80 is unable to load one stored address to another stored address.  Instead, you get halt, an instruction that will be useless to you for quite some time.  Pretend that it doesn't exist for now.

This is great and all, but the LD table doesn't allow you to load any new values to registers.  For that, I present this:

06     
1E   
16   
1E   
26   
2E   
36   
3E
ld b,x   
ld c, x   
ld d, x   
ld e,x   
ld h,x
ld l,x     ld (hl),x   
ld a,x   



I must warn you: Since loading values to (HL) modifies the RAM, only do it when you know the exact value of HL.

Seeing Your Work

It would be pretty difficult to write code without being able to check the values of registers as you go along.  I reccomend that you use the graph screen for this.  At the end of every program that you run, your calculator will copy 756 (63*96/8) bits from an area in the RAM, starting at the address 9340, to the LCD screen.  You can take advantage of this by copying a register to 9340.  Don't ask why yet, but you can update the graph screen in the middle of a program, ASM or BASIC, by running the code EF6A48.  Let's test this now...Here's a program to write F5 to (9340):

ld h,93
 
ld l,40
 
ld (hl),F5
 
ret
 


To run it on your calc, type in

2693   
2E40
 
36F5  
C9  

after AsmPrgm.  Look in the top-left corner of the screen.  And... that's what F5 looks like in binary!  11110101, right?  You'll be banging your head against a wall after I tell you this, but you can only draw onto the graph screen in bytes at a time.  No individual pixels.  When writing to the LCD (liquid crystal display), you can change a setting to go at 6 bits instead of 8, but that's as precise as it gets.  Happy birthday!

You could have all the code on one line if you wanted to, but it's easier to read when you separate it like that.  After memorizing all the instructions, I found it most efficient to put all the code that worked in a specific function on one line and to group all my code similarly; that made it easiest to read and understand.  After you've gotten used to reading code as hex numbers, you should be able to just type in your programs that way instead of writing them out as readable programs.  Isn't that awesome?  You can be like Cypher from The Matrix or something. 


Table of Contents

Forum Discussion 

Previous

Next

Portal | My Account | Register | Lost Password or Username | TOS | Disclaimer | Help | Site Search | File Archives Copyright © 2002-2019 CalcG.org