Hello guys, Lets see how to generate look up table for odd numbers.
There are three addressing modes in 8051 micro-controller: Immediate addressing mode, Direct addressing mode and indirect addressing mode. We use indirect addressing mode in this program to generate look up table for odd numbers.
Note: When we do division operation in 8051 accumulator register A holds quotient and accumulator register B holds the reminder.
- Initialize a register with base address of memory location where look up table needs to generated
- Initialize counter register with number of elements in look up table
- Initialize natural number register (Rd) with 01h
- Move 02h to accumulator register B
- Move value in Rd to accumulator register A
- Divide A by B and check if value in B is 1
- If value in B is 1 then store the value in Rd to memory location, increment Rd and address register and jump to step 5
- Else increment Rd and jump to step 5
Here is example code for look up table of first 10 odd numbers stored in internal memory location with starting address 0xA0
<br />ORG 0000h <br /> LJMP main ; Long jump main label<br /> ORG 0040h<br /> main: <br /> MOV R0,#0a0h ; Initializing base address of memory location<br /> MOV R1,#0ah ; Initializing counter register <br /> MOV R2,#01h ; Initializing first natural number<br /> loop: MOV B,#02h <br /> MOV A,R2<br /> DIV AB ; A/B here register A holds quotient and B holds reminder after this instruction is executed<br /> MOV R3,B<br /> CJNE R3,#00h,store ; Checking number is whether odd or even <br /> INC R2<br /> SJMP loop<br /> <br /> store: MOV A,R2 ; Store the number if it is odd<br /> MOV @ R0,A ; Moving odd number value to memory location<br /> INC R0 ; Incrementing address register<br /> INC R2 <br /> DJNZ R1,loop<br />end<br />