Example - 01first.asm - Arithmetic |
Most of these examples include a learning task. Study the example and if you can complete the task/s, it is likely that your understanding is good.
; ===== WORK OUT 2 PLUS 2 ====================================== CLO ; Close unwanted windows. MOV AL,2 ; Copy a 2 into the AL register. MOV BL,2 ; Copy a 2 into the BL register. ADD AL,BL ; Add AL to BL. Answer goes into AL. END ; Program ends ; ===== Program Ends =========================================== YOUR TASK ========= Use SUB, DIV and MUL to subtract, divide and multiply. What happens if you divide by zero? Make use of CL and DL as well as AL and BL. |
Type this code into the simulator editor OR copy and paste the code OR load the example from disk.
Step through the program by pressing Alt+P repeatedly.
While you are stepping, watch the CPU registers. You should see a '2' appear in the AL register followed by a '2' in the BL register. AL should be added to BL and a '4' should appear in AL. The altered registers are highlighted yellow.
Watch the register labelled IP (Instruction Pointer). This register keeps track of where the processor has got to in the program. If you look at the RAM display, one RAM location is labelled with a red blob. This corresponds to the Instruction Pointer. Note how the red blob (IP) moves when you step the program.
When doing the learning exercises, add to and modify your own copy of the example.
Comments |
Any text after a semicolon is not part of the program and is ignored by the simulator. These comments are used for explanations of what the program is doing. Good programmers make extensive use of comments. Good comments should not just repeat the code. Good comments should explain why things are begin done. |
CLO |
The CLO command is unique to this simulator. It closes any window that is not needed while a program is running. This command makes it easier to write nice demonstration programs. It also avoids having to close several windows manually. |
MOV |
The MOV command is short for Move. In this example numbers are being copied into registers where arithmetic can be done. MOV copies data from one location to another. The data in the original location is left intact by the MOV command. Move was shortened to Mov because, in olden times, computer memory was fiendishly expensive. Every command was shortened as much as possible, much like the mobile phone texting language used today. |
ADD |
ArithmeticThe add command comes in two versions. Here are two examples ADD AL,BL - Add BL to AL and store the result into AL ADD AL,5 - Add 5 to AL and store the result into AL y Look at the on-line help to find out about SUB, MUL and DIV. Remeber that you can access on-line help by pressing the F1 key. |
Registers |
Registers are storage locations where 8 bit binary numbers are stored. The central processing unit in this simulator has four general purpose registers called AL, BL, CL and DL. These registers are interchangeable and can, with a few exceptions, be used for any purpose. Newer central processing unit (CPU) chips have 16, 32 or even 64 bit registers. These work in the same way but more data can be moved in one step so there is a speed advantage. Wider registers can store larger integer (whole) numbers. This simplifies many programming tasks. The other three registers SP, IP and SR are described later. |
Hexadecimal Numbers |
In the command MOV AL,2 the 2 is a hexadecimal number. The hexadecimal number system is used in low level programming because there is a very convenient conversion between binary and hex. Study the Hexadecimal and Binary number systems. |
END |
The last command in all programs should be END. Any text after the END keyword is ignored. |
Use all the registers AL, BL, CL and DL and experiment with ADD, SUB, MUL and DIV.
Find out what happens if you try to divide by zero.
© C Neil Bauers 2003