0

Are these declarations correct?

#include <stdint.h>
#include <stdbool.h>
#include <reg_mg82f6d64.h>
uint8_t data *R0 = 0x00;
uint8_t data *R1 = 0x01;
uint8_t data *R2 = 0x02;
uint8_t data *R3 = 0x03;
uint8_t data *R6 = 0x06;
uint8_t data *R7 = 0x07;
uint16_t code *DPTRc = 0x0000;
uint16_t xdata *DPTRx = 0x0000; // pdata
uint8_t data *DPTRd = 0x00;
uint8_t idata *STACK[10] = {(uint8_t idata *)0x81};

In here I am trying to use them exact same on 8051 assembler with C features but sometime it goes wrong, for example i can not use SP register for PUSH, POP like on assembly.

void wrb(void) // (write read byte)
{
 *STACK[1] = ACC; // i want to do PUSH(ACC);
 while ((SPSTAT & 0x80) != 0); 
 ACC = STACK[1]; // // i want to do POP(ACC);
 SPDAT = ACC;
 while ((SPSTAT & 0x80) == 0);
 ACC = SPDAT;
}
void PUSH(void)
{
 SP++;
 *(uint8_t idata *)SP = ACC;
}
void POP(void)
{
 ACC = *(uint8_t idata *)SP;
 SP--;
}

So when MCU has Stack Pointer why would i declare another array and use more memory size which is a bad development!

asked Jan 23, 2025 at 13:22
14
  • 3
    Since the C program uses the stack on its own, you (with your level of knowledge) shall not tinker with the stack. Please use the documentation of your compiler to learn how to generate an assembly listing of your compiled program and study it. Commented Jan 23, 2025 at 15:07
  • 1
    I'm not familiar with the environment you use. What exactly is SP? Is it the processor's stack pointer? If so, then your PUSH and POP functions cannot work, because the processor's stack pointer is presumably used for the return address. Commented Jan 23, 2025 at 15:07
  • 3
    Unless you actually need a LIFO data structure you don't need to mess with the stack directly. Use local variables which the compiler will manage for you in registers or the stack as needed. Also trying to reference registers directly will not work reliably since you don't know how the compiler uses them between your accesses. Furthermore, even if you could do the things your PUSH and POP live in different functions so you could not use the same stack the cpu uses for return addresses anyway. Commented Jan 23, 2025 at 15:16
  • 3
    Use the same algorithm, not the same instructions. Suppose you wanted to add 10 to some variable x. In assembly you might do push acc; mov a, x; add a, #10; mov x, a; pop acc or similar. The accumulator is used because the cpu can only do addition there. Note that the push/pop is only because the code might want to preserve the original value, it's not an algorithmic requirement for a stack. You don't want to copy all these steps to C, you would just write x += 10; and be done with it. Commented Jan 23, 2025 at 15:37
  • 2
    If you do actually want to transcribe the instructions you should then probably use simulated registers and stack to avoid conflict with the real things the compiler will manage for you. That will of course not produce good code but could be the starting point for reverse engineering. Commented Jan 23, 2025 at 15:41

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.