Do You Speak Assembler?
Under this title, we begin a series of articles devoted to programming in assembler. They are intended for those who are rather reluctant to venture above RAMTOP, or for people who have only recently acquired that magical box. AMIGA, SPECTRUM, or even ATARI (oops) are no different ... when it comes to the computer’s internal language.
Algorithms are put together in much the same way. The most important thing is how much we can squeeze out of our little machine, not how quickly we can wave a pleasant stick (a nod to the knights of the joystick).
But, down to business. Our Z-80 microprocessor, which is inside the SPECTRUM and so on, can analyse only numbers in the range from 0 to 255. Typing in such numbers (machine code) directly into memory cells tells us very little, so each number has been given a suitable name. We’ll use these names—that is, ASSEMBLER.
Writing in this language can sometimes be tedious, and it can be hard to figure out what is going on and why something doesn’t work. But you can’t do everything with BASIC commands, and it is about 60 times slower than assembler. Sometimes it may seem that our computer is mocking us, and working according to the weather outside. Don’t get discouraged when that happens. I assure you, nothing happens by itself or without reason.
But let’s get to work!
To write using these names for numbers—MNEMONICS—it’s best to use a utility program designed for this purpose, for example GENS.
The Z-80 has many registers and instructions. (What...? Ah.)
Someone asked what registers are. Put simply, they are variables (similar to those in BASIC) to which we assign certain numerical values (that is, if we want, for example, the letter “A” to have the value 10, we write: A,10). Our microprocessor has seven registers: A, B, C, D, E, H, L, and about 700 instructions, which I’ll present to you in portions that are easy to digest.
So, let’s load GENS, for example at address 25000, and write:
ORG 40000 ; place the program at address 40000
PETLA
HALT
OUT (254),A
DEC A
DJNZ PETLA
RET
Assemble it (press A and then Enter twice)—that is, convert the program into machine code, starting at the address specified by the instruction in GENS, ORG nn, which in our case is 40000.
Switch to BASIC (press “B” and Enter) and type:
10 RANDOMIZE USR 40000
RUN
And... the borders change from white to black every 1/50 of a second.
WHY!?
Well, because the screen is refreshed exactly every 1/50 s, and the HALT instruction synchronises the program’s timing precisely for this purpose, so that it is called again after 1/50 s.
OUT (254),A sends the value in register A onto the data bus at address 254. As you know, port 254 is a very important port in which, among other things, the first three bits—that is, values from 0 to 7—are responsible for the border colour.
DEC A means decrementing—that is, subtracting 1 from a given register (in our case, register A). So after this instruction has been executed, its value will be 1 less.
DJNZ PETLA is a conditional GOTO instruction. It automatically decrements register B, that is, DEC B (LET B=B-1). If, after subtracting 1 from B, the value of B is not yet equal to 0, then jump to the labelled line (in our case, PETLA; in assembler, you don’t jump to a specific line number, but to the name of the line, that is, the label...
Do You Speak Assembler?
Fragment 2/2
…label). In other words, jump as many times as the initial value of register B.
RET is an instruction corresponding to BASIC’s RETURN. RANDOMIZE USR 40000 can also be translated as GO SUB 40000: when RETURN is encountered, return to the main program. In our case, that is BASIC.
INC r — increment a register; this means increasing a given register by 1. The r stands for any of the registers, namely:
So let’s add the following lines to our program:
INC
100 PETLA2 HALT
INC
OUT
DJNZ PETLA2
RET
I hope this first lesson was explained simply enough. If not, please get in touch.
To be continued.
SCALMAN
Share your thoughts about the article