Assembler for Dummies.
(#2)
(C) Mikhail Spitsin 1995
________________________________
Flags
The processor's "F" register is called the flag register. What is it?
A flag is a variable that can have two states - set (equal to one) and cleared (equal to zero). Therefore, the "F" register can be considered as a set of eight flag bits. We can use only four of them: zero flag, carry flag, sign flag, and parity-overflow flag.
Arithmetic operations.
Arithmetic is a very useful science; we constantly calculate something: add, subtract, divide, multiply. We will now discuss how to do this in assembler.
We will start with the simplest, adding one to something, for example to register "A":
********************************
LD A,NUBER
INC A
RET
********************************
As you can see, this is very simple; there is a command "INC" - increment (increase by one), followed by the operand, i.e., some register or register pair:
********************************
INC A INC HL
INC H INC DE
INC E INC IY
INC E INC (HL)
INC (IX+N) INC (IY+N)
********************************
If you need to increase any memory cell by one, you should proceed as follows:
********************************
LD HL,ADDRES LD IX,ADDRES
INC (HL) INC (IX+0)
RET RET
********************************
The first option works faster and is convenient if you are working with a single memory cell, but if you are working in a table, this is not economical and looks ugly. Compare: we need to increase the first, fifth, and tenth bytes in the table by one:
********************************
LD HL,TABL+1 LD IX,TABL
INC (HL) INC (IX+1)
LD HL,TABL+5 INC (IX+5)
INC (HL) INC (IX+10)
LD HL,TABL+10 RET
INC (HL)
RET
********************************
Everything mentioned above about increasing by one is also true for decrementing, i.e., for decreasing by one:
********************************
DEC A DEC HL
DEC L DEC IX
DEC H DEC DE
DEC E DEC BC
DEC D DEC IY
DEC C DEC IX
DEC B DEC (HL)
DEC (IX+N) DEC (IX+N)
********************************
Now let’s assume we need to increase register "A" not by one, but say by ten:
********************************
LD A,NUMBER
ADD A,10
RET
********************************
You can add register "A" with a number and other registers and with a memory cell addressed by register pairs "HL", "IX", and "IY". You can also add register pairs with "HL", "IX", and "IY".
(PureBasic - file system)
********************************
ADD A,N ADD A,(HL)
ADD A,A ADD A,(IX+N)
ADD A,B ADD A,(IY+N)
ADD A,C ADD HL,HL
ADD A,D ADD HL,BC
ADD A,E ADD HL,DE
ADD A,H ADD HL,SP
ADD IX,IX ADD IX,BC
ADD IX,DE ADD IX,SP
********************************
As you can see, the set of commands is quite large. An error may occur when executing this command:
********************************
LD A,45
LD B,230
ADD A,B
RET
********************************
The sum of "A" and "B" exceeded 255, so "A" will not be 275 but 20 (register "A" is not elastic); to let us know that an overflow occurred, the processor sets the carry flag to one. We just need to check it.
Just as "INC" has "DEC", "ADD" also has a "pair," which is "SUB," and it has its own peculiarities. The "SUB" command only works with register "A," so when writing the mnemonic of this command, "A" is omitted:
********************************
SUB N SUB C
SUB A SUB H
SUB B SUB D
SUB E SUB (HL)
SUB (IX+N) SUB (IY+N)
********************************
The command affects the carry flag similarly to "ADD."
In addition to the pair of commands "ADD" and "SUB," there is another pair. The commands "ADC" and "SBC" work with the carry flag, i.e., when adding or subtracting, the value of the carry flag is added (or subtracted) to the result. There are two special commands to set the carry flag - "SCF" and "CCF." "SCF" sets the carry flag to one. "CCF" sets the carry flag to zero.
********************************
ADC A,N SBC A,N
ADC A,A SBC A,A
ADC A,H SBC A,H
ADC A,L SBC A,L
ADC A,D SBC A,D
ADC A,E SBC A,E
ADC A,B SBC A,B
ADC A,C SBC A,C
ADC A,(HL) SBC A,(HL)
ADC A,(IX+N) SBC A,(IX+N)
ADC A,(IY+N) SBC A,(IY+N)
ADC HL,HL SBC HL,HL
ADC HL,BC SBC HL,BC
ADC HL,DE SBC HL,DE
ADC HL,SP SBC HL,SP
********************************
Now examples of the commands "ADC" and "SBC":
********************************
LD A,10 LD A,10
LD B,5 LD B,5
CCF CCF
SBC A,B ADC A,B
RET RET
A=5 B=5 A=15 B=5
********************************
Instead of the two commands "CCF" and "SBC A,B," you can simply use "SUB B," the result will be the same.
********************************
LD A,10 LD A,10
LD B,5 LD B,5
SCF SCF
SBC A,B ADC A,B
RET RET
A=4 B=5 A=16 B=5
********************************
As can be seen from the results, the carry flag significantly affects the result of the operation. When subtracting, it is subtracted from the result, and when adding, it is added to the result.
Almost everything has been said about addition and subtraction; now we will talk about division and multiplication. Unfortunately, SPECCY does not have division and multiplication commands, but these commands can be constructed from several others. For example, we need to multiply the contents of two registers - "A" and "C":
********************************
LD A,10
LD C,5
LD B,A
XOR A
LOOP ADD A,C
DJNZ LOOP
RET
********************************
In the example, there are two new commands - "XOR A" and "DJNZ LOOP." "XOR A" zeros the register "A," and the command "DJNZ LOOP" repeats all commands from the command marked with a label (for example, "LOOP") until the "DJNZ" command (after it should stand the same label as at the beginning of the loop); the number of repetitions is set in register "B." Using the fact that multiplying M by N is adding the number M to itself N times, you can understand the example given above.
This property can also be used for division. Try to do it yourself.
Next time we will talk about comparison commands and working with bits.
________________________________
Contents of the publication: ZX Format #02
- IS-DOS
Announcement of a school-ready hardware-software complex by Iskra Soft and Peters, featuring a networked computer class setup on ZX Spectrum with IS-DOS.
- IS-DOS - Владимир Елисеев
Explanation of command line monitor and text editor restart in IS-DOS using mon.com utility as an example.
- IS-DOS
Introduction to IS-DOS system utilities, covering functions like help, user menu, file viewing, editing, and file operations.
- IS-DOS
Detailed description of the eliminat.com program for freeing memory from resident tasks and drivers, featuring interactive mode and command-line keys. Specific channel numbers allocated for task and driver types. Includes usage options and color customization.
- IS-DOS Window System - Владимир Елисеев
Exploration of IS-DOS window system restarts for printing text in windows and absolute screen coordinates. Examples of restart implementations such as lwt, adrwt, lenwt, prstr, str, and lnstr. Continuation to cover auxiliary restarts in next issue.
- Assembler
Introduction to assembly language basics, focusing on flags, arithmetic operations, and register manipulation. Discusses addition, subtraction, and complex operations like multiplication and division through examples. Highlights specific assembly commands and their functions for ZX Spectrum.
- Hardware
Discussion of hardware modifications for Scorpion ZS-256-Turbo, including the implementation of a Turbo/Normal switch. Pros and cons of software-based switching methods. Advice on soldering and circuit adjustments.
- Hardware
Discussion of a new music add-on for ZX Spectrum by X-TRADE and HACKER STINGER, called 'ZX GENERAL SOUND', offering high-quality audio and minimal processor usage.
- Hardware
Discussion on hardware innovations and marketing strategies, focusing on mouse and keyboard interfaces for ZX Spectrum. Critique of competitor's misleading advertising and analysis of serial versus passive mouse technology. Author questions necessity and cost of advanced features.
- Hardware
Discussion of hardware acceleration methods for ZX Spectrum, focusing on turbo-modes and their effects on performance and compatibility.
- Toys
Fantasy tale about magical creatures battling human intrusion. Main character recruits allies for a quest to restore the land's former glory. Challenges include finding tools, overcoming obstacles, and reviving companions.
- Toys
Review of 'Carrier Command' game, focusing on its strategic and simulation aspects. Includes detailed gameplay mechanics, controls, and objectives. Highlights strategies for success and unique features like managing resources and autonomous systems.
- Interview
Interview with Sergey Zonov and Andrey Larchenko discussing their experience with microprocessors and ZX Spectrum development, including the creation of the Scorpion ZS 256 computer.
- Information
Contact information and staff list of ZX-Format No. 2 (1995) including editor, coders, and designers.
- Information
Editor's address to readers of ZX Format, discussing the positive feedback, past errors, and the quest for a cartoonist, with future plans for the magazine.
- Information
Discussion on the new interpretation of the 'PULLDOWN' window menu system for ZX Spectrum, focusing on interface updates and user interaction enhancements.
- Information
Discussion about companies illegally distributing ZX Format, emphasizing the benefits of purchasing official copies.
- Competition
The article discusses the lack of participation in ZX Format's competition, detailing the rules and prizes, and encourages readers to engage with new ideas.
- Let's Relax
A humorous account of a software vendor's challenges dealing with clueless customers, illustrating the nerve-wracking nature of his job.
- Mailbox
Overview of hardware components and prices for ZX Spectrum enthusiasts with ordering details.
- Mailbox
Reader letters section in ZX Format #02 discusses reader feedback, addresses issues with ZX Format features, and offers future improvements.
- Premiere
Guide to Digital Studio v1.12, a music editor for ZX Spectrum, including features, menu navigation, and the use of Digital Studio Compiler.
- For Programmers
Exploration of tools that extend the standard Basic 48, including Renumber for Basic 128, Trace & Speed, Blast Toolkit, and ZXeditor, highlighting their functionalities and utilities.
- Various
History of Amiga computer models and their evolution from A1000 to A4000/60T with specifications and unique features. Explanation of technical terms and differences between chip and fast memory. Mention of new developments like AGA chipset and models for different needs.
- Miscellaneous
The article presents upcoming ZX Spectrum software releases and reviews game innovations like 'Adventures of Winnie the Pooh' and 'UFO 2: Devils of the Abyss'. It highlights features, creators, and technical requirements. It also includes announcements from SOFTLAND and Cracked Masters Group.
- Systems
Discussion of creating music with Instrument 3.01, focusing on digitized sound. Analysis of program's capabilities and conversion from ASC Sound Master. Instructions for composition and conversion.
- What's New
Review of new ZX Spectrum games entering the St. Petersburg market in late 1995. Detailed game descriptions, memory requirements, controls, and music/graphics evaluations. Highlights include Night Hunter, Extreme, Grell & Falla, and more.