This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Stack of instructions application_part2

Hi Everyone
Maybe previous a question to application external memory has confused, let me repost sample question about stack of instructions.

The assembler code is push register R0,R1 onto stack and pop register R0,R1from stack. So how to write a C51 program like this function or have any instructions?

PUSH 0 ; register R0
PUSH 1 ; register R1
.
.
.
.
POP 1 ; register R0
POP 0 ; register R1

  • So how to write a C51 program like this function or have any instructions?

    You don't. For starters, a C program has not the slightest idea what might or might not be in R0 and R1, and thus there's absolutely no point in pushing them on the stack (which the C program also has essentially no control over).

    If you want to code like that, stop using C and do it in assembler. Assembler-type thinking doesn't extend that far into the field of C programming.

  • "Maybe previous a question to application external memory has confused"

    Maybe: PUSH and POP act on the processor stack - the thing pointed to by SP. Do you really want to push and pop R0 and R1 to and from that stack, or are you asking how to create and use a software stack to store some other data?

  • I want to know that how to write a progam of C51 to push and pop R0 and R1 to and from that stack.

  • I want to know that how to write a progam of C51 to push and pop R0 and R1 to and from that stack.

    WHY, WHY, WHY

  • There are no instructions in C that let you directly push and pop stuff on the stack.

    There are push and pop library routines (see http://www.keil.com/support/man/docs/c51/c51__push_.htm) that will push and pop the contents of SFRs on the stack.

    Pushing and popping things on the stack are not features or benefits of the C programming language.

    Jon

  • Thank everyone offer information that I more understand the stack of data in C51.
    I will try Jon's one and thanks for let me get the idea to application in my program.

  • I want to know that how to write a progam of C51 to push and pop R0 and R1 to and from that stack.

    WHY, WHY, WHY

    Please, oh please, answer

    Erik

  • Hi Erik

    I am not directly push and pop register, it's contents.
    I want to know that have any instructions of C51 like it or create and use a write to store data at same function?

    For explam:

    MOV R0,#00 ;1 line
    CALL aa
    ;-----------
    aa:
    PUSH 0
    Mov R0,#ff ;2 line
    .
    .
    POP R0
    RET
    ;-----------

    I want to keep 1 line register(R0) of content
    when excuted 2 line progeame.

  • Hi Erik

    I am not directly push and pop register, it's contents.
    I want to know that have any instructions of C51 like it or create and use a write to store data at same function?

    For explam:

    MOV R0,#00 ;1 line
    CALL aa
    ;-----------
    aa:
    PUSH 0
    Mov R0,#ff ;2 line
    .
    .
    POP R0
    RET
    ;-----------

    I want to keep 1 line register(R0) of content
    when excuted 2 line progeame.

  • I want to know that have any instructions of C51 like it or create and use a write to store data at same function?

    The problem with this question which we've all been trying to point out to you is that it's the wrong question to ask yourself. The answer, once you knew it, would not help you in any noticeable way. It'd only make the confusion that's already in your mind worse.

    What you're trying to do here doesn't need to be done by you. The C compiler takes care of all these details for you --- that's exactly its job. Don't mess with it.

  • I this example, the two 'a' are separate variables:

    void Func(void)
    {
    char a;

    a=20;
    }

    main()
    {
    char a;

    a=10;
    Func();
    //Here, a still equals 10

    while(1);
    }

    In this example there is only one 'a':

    char a;

    void Func(void)
    {
    a=20;
    }

    main()
    {
    a=10;
    Func();
    //Here, a equals 20

    while(1);
    }


    In this example we preserve the value of 'a':

    char a;

    void Func(void)
    {
    char b;

    b=a; //b equals 10
    a=20; //a equals 20
    a=b; //a equals 10 again
    }

    main()
    {
    a=10;
    Func();
    //Here, a equals 10

    while(1);
    }

    Does any of this make any sense to you?

  • Hi Hsu,

    I assume you are using the assembler and it is an 'asm' file that you are creating, if so, then this is what your code should be modified to allow you to push/pop R0:-

    MOV R0,#00 ;1 line
    CALL aa
    ;-----------
    aa:
    PUSH AR0   ;*** MODIFIED ***
    Mov R0,#ff ;2 line
    .
    .
    POP AR0    ;*** MODIFIED ***
    RET
    ;-----------
    

    I have just prefixed the register R0 with 'A' so we get AR0 when wanting to push or pop the contents of this register.
    Please note that if you start adding bank switching to your code, I have no idea (because i have not tried it) if this method still works and needs to be replaced by the less obvious pushing and poping of the specific address.

    Hope this helps,
    Mark.