INMC 80 News

  

October–December 1981 · Issue 5

Page 46 of 71

When dealing with a subroutine in isolation, we have to make some assumptions, firstly that the data to be printed has already been put in the A register, and for the purposes of demonstration, this is going to be B5H. Next we assume the routine has been CALLed in an orderly fashion, implying that the STACK is already correctly set, and left pointing at the last byte saved (in this instance, because the routine was CALLed, pointing at the low byte of the return address (as the PC will have already been advanced to the correct return address by the CALL instruction before the return data was placed on the STACK, see episodes 3 and 4).

Ok. Oft we jolly well go!!

Line 4470. This is a brief comment as to what we propose to do, viz: OUTPUT A.

Line 4475. This is the start of the routine, and is labelled B2HEX accordingly. The instruction is PUSH AF, which will put the current contents of registers A and F onto the STACK. The STACK will contain B5H as it’s higher byte (because I’ve already said that A contains B5H), and XX as it’s lower byte (because that state of the F register was indeterminate when we PUSHed the AF register, and we don’t care a damn what it contains). The SP has been decremented by two. We PUSHed AF because the calculation process will destroy the current contents of the A register, and we need it back to work out the second half. As a matter of interest, we could have equally well saved the contents of A in some other register, which would have been slightly quicker in terms of execution time, but as the specification implies that all other registers except A remain unchanged, we would have had to save the contents of the other register anyway, so what odds!!

Lines 4480 to 4495. This is a fun one! If you think about it, an 8 bit byte expressed in HEX contains two alpha numeric characters, and each character can be said to represent a nibble of the 8 bit byte. Now the order they appear on the screen says that the highest nibble will be printed first. So because of the way the routine works (wait for it), we’ve got to move the high nibble into the place currently occupied by the low nibble. This is done by moving everything four places to the right (no political cracks please). More precisely, four ‘Rotate Right Arithmetic’ (RRA) instructions are used (shift instructions would have worked just as well, but someone in pre-history dictated that rotate instructions would be used). The rotate right moves the contents of the C flag into the highest bit, everything else right one place, and the bit that drops off the end into the C flag. Lets follow that diagramatically:

B5H equals10110101Xin binary bits, the X is the C flag.
Now rotate it one
X10110101the C flag, X has been moved into the highest bit everything else has moved right one place, and the lowest bit has moved into the C flag.
Rotate again
1X1011010Rotate again
01X101101and for the last time
101X10110

Simple really, and the end result is that the ‘B’ of B5H has been moved into the low nibble.

Line 4500. Now to call the calculation routine using the NAS-SYS RCAL routine. Don’t worry how that works, just pretend it’s a CALL as per the Z80 manual. The CALL instruction PUSHes the return address (037C) onto the STACK, and loads the PC with the address CALLed, 037D, so we skip along to line 4515.

Page 46 of 71