Example - 04IncJmp.asm - Counting
Home | Previous | Next
; ===== Counting =================================== MOV BL,40 ; Initial value stored in BL Rep: ; Jump back to this label INC BL ; Add ONE to BL JMP Rep ; Jump back to Rep END ; Program Ends ; ===== Program Ends =============================== TASK ===== Rewrite the program to count backwards using DEC BL. Rewrite the program to count in threes using ADD BL,3. Rewrite the program to count 1 2 4 8 16 using MUL BL,2 Here is a more difficult task. Count 0 1 1 2 3 5 8 13 21 34 55 98 overflow. Here each number is the sum of the previous two. You will need to use registers or RAM locations for temporary storage of the numbers. If you have never programmed before, this is a real brain teaser. Remember that the result will overflow when it goes above 127. This number sequence was first described by Leonardo Fibonacci of Pisa (1170_1230)
The program counts up in steps of one until the total is too big to be stored in a single byte. At this point the calculation overflows. Watch the values in the registers. In particular, watch IP and SR. These are explained below.
Although this program is very simple, some new ideas are introduced.
This line initialises BL to 40.
Rep: is a label. Labels are used with Jump commands. It is possible for programs to jump backwards or forwards. Because of the way numbers are stored, the largest jumps are -128 backwards and + 127 forwards. Labels must begin with a letter or the _ character. Labels may contain letters, digits and the _ character. Destination labels must end with a Colon:
This command adds one to BL. Watch the BL register. It will count up from 40 in hexadecimal so after 49 comes 4A, 4B, 4C, 4D, 4E, 4F, 50, 51 and so on.
When BL reaches 7F hex or 127 in decimal numbers the next number ought to be 128 but because of the way numbers are stored in binary, the next number is minus 128. This effect is called an OVERFLOW.
The status register labelled SR contains four flag bits that give information about the state of the CPU. There are three flags that indicate whether a calculation overflowed, gave a negative result or gave a zero result. Calculations set these flags
These flags are described in more detail later.
This command causes the central processing unit (CPU) to jump back and repeat earlier commands or jump forward and skip some commands.
The instruction pointer labelled IP contains the address of the instruction being executed. This is indicated by a red highlighted RAM position in the simulator. Each CPU command causes the IP to be increased by 1, 2 or 3 depending on the size of the command. In the RAM displays, the instruction pointer is highlighted red with yellow text.
NOP ; Increase IP by 1 INC BL ; Increase IP by 2 ADD AL,BL ; Increase IP by 3 JMP Rep ; Add or subtract a value from IP to ; jump to a new part of the program.
Fetch the instruction. IP points
to it. This is called the operator.
Every machine cycle has one operator or instruction. There could be zero, one or two operands depending on the instruction. OP Codes are the machine codes that correspond to the operators and operands.
Home | Previous | Next
© C Neil Bauers 2003