Example Programs
Programming is at once a skill and an art. Just as anyone may learn to play a musical instrument after sufficient instruction and practice. so may anyone learn to program a computer. Some individuals. however. have a gift for programming that sets them apart from their peers with the same level of experience. just as some musicians are more talented than their contemporaries.
Gifted or not. you will not become adept at programming until you have written and rewritten many programs. The emphasis here is on practice; you can read many books on how to ride a bicycle, but you do not know how to ride until you do it.
If some of the examples and problems seem trivial or without any "real-world" application. remember the playing of scales on a piano by a budding musician. Each example will be done using several methods; the best method depends upon what resource is in short supply. If programming time is valuable. then the best program is the one that uses the fewest lines of code; if either ROM or execution time is limited. then the program that uses the fewest code bytes is best.
¨ EXAMPLE PROBLEM 3.1
Copy the byte in TCON to register R2 using at least four different methods.
· Method1: Use the direct address for TCON (88h) and register R2.
Mnemonic Operation
MOV R2.88h Copy TCON to R2
· Method 2: Use the direct addresses for TCON and R2.
Mnemonic Operation
MOV 02h.88h Copy TCON to direct address 02h (R2)
· Method 3: Use R1 as a pointer to R2 and use the address of TCON.
Mnemonic Operation
MOV R1,#02h Use R1 as a pointer to R2
MOV @R1 .88h Copy TCON byte to address in R1 (02h = R2)
· Method 4: Push the contents of TCON into direct address 02h (R2).
Mnemonic Operation
MOV 81h.#01h Set the SP to address 01 h in RAM
PUSH 88h Push TCON (88h) to address 02h (R2)
¨ EXAMPLE PROBLEM 3.2
Set timer T0 to an initial setting of 1234h.
· Method 1: Use the direct address with an immediate number to set THO and TLO.
Mnemonic Operation
MOV 8Ch,#12h Set TH0 to 12h
MOV 8Ah,#34h Set TL0 to 34h
Totals: 6 bytes, 2 lines
· Method 2: Use indirect addressing with RO for TLO and RI for THO.
Mnemonic Operation
MOV R0,#8Ah Copy 8Ah, the direct address of TL0, to R0
MOV R1,#8Ch Copy 8Ch, the direct address of TH0, to R1
MOV @R0,#34h Copy 34h to TLO
MOV @R1,#12h Copy 12h to THO
Totals: 8 bytes, 4 lines
The first method is also the better method in this example.
¨ EXAMPLE PROBLEM 3.3
Put the number 34h in registers R5, R6, and R7.
· Method 1: Use an immediate number and register addressing.
Mnemonic Operation
MOV R5,#34h Copy 34h to R5
MOV R6,#34h Copy 34h to R6
MOV R7,#34h Copy 34h to R7
Totals: 6 bytes, 3 lines
· Method 2: Since the number is the same for each register, put the number in A and MOV A to each register.
Mnemonic Operation
MOV A,#34h Copy a 34h to A
MOV R5,A Copy A to R5
MOV R6,A Copy A to R6
MOV R7,A Copy A to R7
Totals: 5 bytes, 4 lines
· Method 3: Copy one direct address to another.
Mnemonic Operation
MOV R5,#34h Copy 34h to register R5
MOV 06h,05h Copy R5 (add 05) to R6 (add 06)
MOV 07h,06h Copy R6 to R7
Totals: 8 bytes, 3 lines
¨ EXAMPLE PROBLEM 3.4
Put the number 80h in RAM locations 30h to 34h.
· Method 1: Use the immediate number to a direct address:
Mnemonic Operation
MOV 30h,#8Dh Copy the number 80h to RAM address 30h
MOV 31h,#8Dh Copy the number 80h to RAM address 31 h
MOV 32h,#8Dh Copy the number 8Dh to RAM address 32h
MOV 33h,#8Dh Copy the number 80h to RAM address 33h
MOV 34h,#8Dh Copy the number 80h to RAM address 34h
Totals: 15 bytes,5 lines
· Method 2: Using the immediate number in each instruction uses bytes; use a register to hold the number:
Mnemonic Operation
MOV A,#80h Copy the number 80h to the A register
MOV 30h,A Copy the contents of A to RAM address 30h
MOV 31h,A Copy the contents of A to the remaining addresses
MOV 32h,A
MOV 33h,A
MOV 34h,A Totals: 12 bytes, 6 lines
· Method 3: There must be a way to avoid naming each address; the PUSH opcode can increment to each address:
Mnemonic Operation
MOV 30h,#8Dh Copy the number 8Dh to RAM address 30h
MOV 81h,#3Dh Set the SP to 30h
PUSH 30h Push the contents of 30h (=8Dh) to address 31h
PUSH 30h Continue pushing to address 34h
PUSH 30h
PUSH 30h Totals: 14 bytes, 6 lines
COMMENT
Indirect addressing with the number in A and the indirect address in R 1 could be done; however, R 1 would have to be loaded with each address from 30h to 34h. Loading R 1 would take a total of 17 bytes and 11 lines of code. Indirect addressing is advantageous when we have opcodes that can change the contents of the pointing registers automatically.