Microcontroller simple program including loop

Thread Starter

antiantianti

Joined Aug 4, 2016
45
Hi
The problem is that I want to write a program using processor 8051 (which is my first one ) but it doesnt work the point is to store #68h in the adress 40h and to increase to 89h with a loop . I dont want the solution since I already have it . Just asking what did I do wrong . Maybe I will post also the solution if you dont understand the point of my program :
SOLUTION:
Code:
MOV A, #68H ;A=68H
MOV R0, #40H ; R0 is a pointer starting from 40H
MOV R2, #73 ; 89H-40H=49H = 73 decimal (loop iterations)
Again:
MOV @R0, A ; loop
INC R0
DJNZ R2, Again
MY PROGRAM
Code:
cseg at 0
    jmp 100

  cseg at 100
      mov a,#40h
      mov r0,73
what:
      mov @a,#68h
      inc a
      djnz r0, what
main:
       sjmp main
       end
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
Add comments to each line in your code, referring to the 8051 instruction set for the exact details of what each line does. Then flow chart the code or just trace it with your finger, line by line. The problem will reveal itself.
 

Thread Starter

antiantianti

Joined Aug 4, 2016
45
Hi
I tried unfortunately I dont see doing nothing but using the registers for different values I tried using comments .Theres is a syntax error that I am unable to recognize ( It may seem easy for you)
Code:
cseg at 0
    jmp 100
  cseg at 100
      mov r2,#68h  ;r2=#68h
      mov a,#40h   ;a=#40h
      mov r0,#73 ;89H-40H=49H = 73 decimal (loop iterations)
what:
      mov @a,r2  ;40h=#68h
      inc a
      djnz r0, what
main:
       sjmp main
       end
 
Last edited by a moderator:

djsfantasi

Joined Apr 11, 2010
9,163
...And why are you incrementing register a? Do you want to store the sequence:

#40h, #41h, #42h, ...
Or:
#40h, #40h, #40h, ... ?

And store these values repeatedly at #68h?
 

Thread Starter

antiantianti

Joined Aug 4, 2016
45
I changed my code
Code:
mov r1,#40h  ;r1=#40h
      mov 40h,#68h   ;40h=#68h
      mov r0,#73 ;89H-40H=49H = 73 decimal (loop iterations)
what:
      inc r1
      djnz r0, what
I found that for indirect adressing you have to use MOV A,@R1 or MOV A,@R0 .But still didnt see the instruction used in the solution MOV @R0, A ; loop.
For me I didnt use it because I saw that the program is still doing the same thing
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
...And why are you incrementing register a? Do you want to store the sequence:

#40h, #41h, #42h, ...
Or:
#40h, #40h, #40h, ... ?

And store these values repeatedly at #68h?
The '@' means indirect addressing - he is trying to use A as a pointer and incrementing after each write. That said, of the scratchpad registers, only R0 and R1 can be used as pointers and then only to the local RAM. He is trying to fill a range of RAM using indirect addressing in a loop.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
I changed my code
From your new code it looks like you are flailing around here. That is not uncommon for a new programmer.

First, it is important that you know what you are trying to do - the big picture then step by step. From the original working code in post 1, I see that you are trying to fill 73 bytes from 40H in RAM with the value 68h using a indirect addressing in a loop.

One of the best ways to learn programming is to study well written, working code. The working example would be good thing to go through, line by line until you completely understand how it works and why things were done the way that they were. When you get to that understanding, the problems that you are having will reveal themselves. I will add that there is really no shortcut to this process, you can't write code until you fully understand and have solved the problem on your desk.

I mentioned that you should comment your code and you did, but not in a good way. The key to comments is not to reiterate what the instruction does but what it contributes to the solution. For example:
MOV A,#68h ; move 68h into A
is correct but not particularly helpful. A better one would be:
MOV A,#68h ; A contains constant value to be written to RAM

I've added some comments and a more descriptive label. As I did, the problem became apparent. Can you see it?
Code:
;  This code fills a contiguous range of RAM with a constant value.  (Ahh, so that's what we are trying to do here..)
  mov r1,#40h     ;  R1 is fill pointer.  Fill starts at 40h
  mov 40h,#68h ;  ??
  mov r0,#73        ; Loop counter: 89H-40H=49H = 73 decimal (loop iterations) (good job showing how you calculated this)

FillLoop:
    inc r1 ; increment pointer after write
    djnz r0, FillLoop ; fill until done
From this it should be more apparent that one big problem is that you are not actually writing anything in the fill loop and that loading RAM byte 40h with 68h doesn't really do anything helpful. Can you fix it from here?

I like to think of programming as part science, part literature and part art. The science part is what problem you are trying to solve, the literature part is how well you describe your solution - to others but mainly to help yourself as an organizational tool and to facilitate understanding. There are lots of ways to do most programming jobs and the art part is how well you implement the solution. Did you take full advantage of the processor's capabilities or just brute-force it? Can you reuse the code or at least the technique? Does your routine play well with other routines or does it hog the CPU? The art part is the hardest to teach because it requires a willingness and discipline on the part of the student but its essential if you are going to be a good programmer. It is worth noting that a seminal series of texts on programming is Knuth's 'The Art of Computer Programming'.

Don't worry if you don't get this all at once. Nobody does. It is a process and as long as you are dedicated to improvement, you'll be fine.
 

djsfantasi

Joined Apr 11, 2010
9,163
The '@' means indirect addressing - he is trying to use A as a pointer and incrementing after each write. That said, of the scratchpad registers, only R0 and R1 can be used as pointers and then only to the local RAM. He is trying to fill a range of RAM using indirect addressing in a loop.
I get it, but didn't know the restriction on registers. Plus I got the direction of the data move backwards. Thanks for making me look at it again b
 
Top