We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
dear all, how can I use PUSH and POP instruction in Keil C ? Thanks
The only way to do this just in C is to create a pseudo stack (an array) and a pseudo pointer. Simply write your own pseudo_push() and pseudo_pop() functions. There is a relatively safe way to do this using 8051 assembly language functions that can be called from a C51 function. You will need something like this:
#pragma ASM $REGUSE _push_byte( A, DPH, DPL ) #pragma ENDASM void push_byte( char x ) { // x = x; // Supress UNUSED warning. // #pragma ASM POP DPH ;Get return address from stack POP DPL ;and store in DPTR MOV A,R7 ;Get x into accumulator... PUSH Acc ;...and push it onto the stack. CLR A ; JMP @A+DPTR ;Return to caller. #pragma ENDASM } #pragma ASM $REGUSE _pop_byte( A, DPH, DPL, R7 ) #pragma ENDASM char pop_byte( void ) { // // #pragma ASM POP DPH ;Get return address from stack POP DPL ;and store in DPTR POP Acc ;Get the return value from stach MOV R7,A ;and put it in R7. CLR A ; JMP @A+DPTR ;Return to caller. #pragma ENDASM return(0); // Dummy return. }
void push_byte( char x ); char pop_byte( void ); void main( void ) { unsigned char loop; char a; char b; a = 0; loop = 255; do { push_byte( a ); b = pop_byte(); a++; }while( --loop != 0 ); loop = 255; }