Pointers<\/a> come in… Pointers allow our application to grab an item at a location in memory based on a value in a register – This value is our pointer and points to the position of our string in memory.<\/p>\nIn our second loop, we begin on line 18 by setting up our pointer by loading the start of our string into a pair of registers; H and L. These two 8 bit wide registers can be combined together into a single 16bit wide register. We need to do this, as the RAM of the SAM is 16bits wide – That is, an 8bit register can hold a value from 0 to 255, whereas a 16bit register can hold 0 to 65535;<\/p>\n
LD<\/span><\/strong> HL,text2<\/code><\/p>\nWe’ve stored our string at a position denoted by the label text2<\/code>, the LD<\/strong><\/span><\/code> instruction takes the address of the text2 label and loads it into the H and L register pair. We’ll see later what this looks like when we get onto Debugging.<\/p>\nOn line 19 we set up the scope for our loop… Our string is 20 characters long, so we need to loop through each of the 20 characters and PRINT it to the screen. Again. we use the B register here for our loop, as the A register is used to hold the value to show on the screen;<\/p>\n
LD<\/span><\/strong> B,20<\/code><\/p>\nOn line 21 we have the start of our loop, and the first time we start utilising the Pointer we set up at Line 18;<\/p>\n
LD<\/span><\/strong> A,(HL)<\/code><\/p>\nThe line above introduces a new style of instruction, where we’ve wrapped an operand in Parentheses, this denotes to the Assembler that this operand is a Pointer. So we’re saying that we want to L<\/strong>oaD<\/strong> a value, pointed to by the HL register pair, into the A register. The first time round this loop, this will the the ASCII character H, or Decimal 72 \/ Hex 48.<\/p>\nNext, on line 22, we have a familiar call to RST<\/span><\/strong> 16<\/code>, which we know by now, Prints the contents of the A Register to the Screen.<\/p>\nOn line 23, we use the INC<\/strong><\/span><\/code> or INC<\/strong>rement instruction, to point the HL register to the next character in our string;<\/p>\nINC<\/span><\/strong> HL<\/code><\/p>\nNext, on line 24, we us the DJNZ or D<\/strong>ecrement and J<\/strong>ump if N<\/strong>ot Z<\/strong>ero instruction to decrement our Loop Counting B register;<\/p>\nDJNZ<\/span><\/strong> print_loop<\/code><\/p>\nThis instruction, decrements the B register, and if the register isn’t Zero, that is, the result of Decrementing the B register doesn’t set the Zero Flag in the processor, then the code should jump to the print_loop label on line 21. Obviously the first 19 times around the B register will be counting down to zero, and only when we’ve printed the final character – An exclamation mark – will we exit the loop.[\/vc_column_text][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n
Understanding the code<\/strong><\/h1>\nThe Final Loop – Reading the Keyboard<\/h2>\n
With the main body of our program complete, we have one final step to take care of… Running our code at the minute, will happily work without any errors… The only problem is, once it’s finished showing our ASCII table and our string,it exits right back to BASIC, and both disappear… Not the best result!<\/p>\n
What we need to do is show our ASCII table and string, then wait for the user to hit a key before returning to BASIC.<\/p>\n
As I mentioned earlier… The SAM Coup\u00e9 ROM has got a set of pre-programmed routines which can be accessed by our code. We have access to a routine called JREADKEY<\/strong><\/span><\/code> which reads the keyboard, and if a key is pressed, stores the key value in the A register. If no key is pressed, the Z or Z<\/strong>ero flag is set.<\/p>\nOn line 28, we CALL<\/strong><\/span><\/code> the address for JREADKEY<\/strong><\/span><\/code> which is\u00a0&0169. You’ll notice that we’re not using RST<\/strong><\/span><\/code> here, which is because this routine doesn’t fall under the small remit of routines covered by the RST<\/strong><\/span><\/code> instruction;<\/p>\nwait_for_key:<\/span>\u00a0<\/span>CALL<\/span><\/strong><\/span> &0169<\/code><\/p>\nThis line forms the start of a loop where we wait for the user to press a key. On line 29 we check the Zero flag, and if it’s set, we simply return to line 28 and check the keyboard again;<\/p>\n
JR<\/strong><\/span> Z,wait_for_key<\/code><\/p>\nIt should be noted, that as well as the JREADYKEY<\/strong><\/span><\/code> routine,we could have also simply used the JWAITKEY<\/strong><\/span><\/code> routine. However, I chose JREADYKEY<\/strong><\/span><\/code> to demonstrate how we loop to wait for a key press.<\/p>\nFinally, on line 30, we have the RET<\/strong><\/span><\/code> or RET<\/strong>urn instruction. This instruction exits the program and returns us back to the BASIC interpreter;<\/p>\nRET<\/strong><\/span><\/code><\/p>\nAnd that’s the end of our code… Once assembled, running this code will produce a result like the following;<\/p>\n