Machine Code Made Easy - Part 2
Author
Publisher
Description
From Fred 7
Machine Code Made Easy - Part 2
Hello again eveyone! ‘Tis I, the Wizard, here to enlighten all
you beginners as to the joys of machine code. This is part 2 in
the series, and I will be referring back to part 1, so if you
don’t have FRED6 (why not?) go and order a copy immediately!
Right, let us proceed….
A - THE MATHS LESSON
———————————
If you hope to get anywhere in m/c a basic knowledge of
binary and, to a lesser extent, hexadecimal, is neccesary.
BINARY is base 2 - a series of 1’s and 0’s which can
represent any number you care to think of. This is how numbers
are stored inside your coupe, so this is important.
Each binary digit (or BIT) stands for a power of 2:1,2,4,8…
and so 8 bits can hold a number between 0 and 255.
This is how they are arranged:
7 6 5 4 3 2 1 0 -> BIT number ———————————————----- 128 64 32 16 8 4 2 1 -> POWERS OF 2 ———————————————----- 1 0 0 0 1 0 1 1
This is equal to 139 in decimal.
HEXIDECIMAL is less important, but is a very useful way of
keeping binary numbers small and neat.
Because it is base 16, its values run like this:
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16,17,18,19,1A and so on…
The digits 0-9 are easy enough to understand, but because
there is no digit for 10, hex uses the letter A. B=11, C=12 and so on.
OK?
B - BIT MANIPULATIONS (OOER!)
———————————————-
The whole point of learning binary, is that many m/c comands use
it. These can be split into 3 groups - arithmetic, shifting and
single bit control. (I just made these names up. Good, eh?)
Right, what’s first. I know, SINGLE BIT CONTROL:
This is easy. Remember that each of the 8 bits in a number is
located by a number from 0 (the units) to 7 (the 128’s). These commands follow simple patterns:
SET b,r : This sets bit b (0-7) of register r ( A,B,C etc ) ie. makes it equal 1.
RES b,r : As above, except that it resets the bit. ie, makes it 0.
BIT b,r : This takes whatever the value of the bit is and copies it into the zero flag.
That was simple enough, wasn’t it?
SHIFTING COMMANDS
—————————-
Oh, dear. There are so many commands I could cover here that I
could fill FRED by myself, so I’ll just stick to the simple ones
RL r: This rotates the bits in register r, so the bit 7 becomes equal
to bit 6, bit 6 becomes equal to bit 5 and so on. The only complication is that the carry flag becomes equal to bit 7 and bit 0 becomes whatever was in carry.
RR r: As above except that all the bits move right instead of left, and as a result,
bit 0 goes to carry and carry goes to bit 7
RLC r: This is like RL except that bit 7 goes to bit 0 as well as carry.
RRC r: Like RR except that bit 0 goes to both bit 7 and carry.
Phew! Hope you got all that.
ARITHMETICAL BINARY COMMANDS
———————————————
Now these are REALLY complicated, but nevertheless, extremely valuable.
They are all based on the logic functions OR,AND,XOR that we discover in physics, and all follow rules we can lay down in TRUTH TABLES. They all work on the principle of taking a bit of one number (or register) and comparing it will the corresponding Î.bit of another, and producing a resulting bit which is stored. This is then repeated for all the other bits in the numbers to finally produce another 8-bit number.
OR. Here, the two numbers are merged together, so that if either
one bit is 1 OR the other bit is 1, then the resulting bit is 1
AND. Here the resulting bit is only 1 if both one bit AND the other bit are 1.
XOR. Here, the result is 1 if the two bits were different, otherwise the result is zero.
These functions can all be shown in TRUTH TABLES:
OR : BIT A : BIT B : RESULT : The format of this command — :———-:———-:————: is : 0 : 0 : 0 : OR xx : 0 : 1 : 1 : where A is ORed with xx : 1 : 0 : 1 : which can be a number or : 1 : 1 : 1 : a register. The result is ————————————— stored in A. AND : BIT A : BIT B : RESULT : The format of AND & XOR —- :———-:———-:————: is exactly like OR. : 0 : 0 : 0 : ie. : 0 : 1 : 0 : AND xx : 1 : 0 : 0 : XOR xx : 1 : 1 : 1 : Again, A is ANDed/XORed ————————————— with xx, with the result stored in A. XOR : BIT A : BIT B : RESULT : —- :———-:———-:————: : 0 : 0 : 0 : : 0 : 1 : 1 : : 1 : 0 : 1 : : 1 : 1 : 0 : —————————————
C - HINTS & TIPS
—————————
To double the Accumulator, do: RL A
To half it do : RR A
To clear A howsabout : XOR A
Last time we saw how to do a loop using DJNZ.
IF you want to loop round more than 256 times, here’s a 16-bit loop routine:
DEC BC ; Number of times to loop in BC
LD A,B ; OR together B & C
OR C ; If they are both 0, the result
JR NZ,LOOP ; is 0, and the zero flag is set.
; If not, loop back.
That’s about it for this issue.
If you have a query about machine code, a useful routine or
even just a problem, feel free to write to me ( not Colin )
at:
Steve Taylor
29 Murray Crescent
Perth PH2 OHN.
And I’ll try to answer you on screen.
Byeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!