dear all, how can I use PUSH and POP instruction in Keil C ? Thanks
Hi Like this ?
#include <reg669k.h> //void WATCHDOG(void); void WATCHDOG(void) { EA = 0; #pragma ASM PUSH ACC #pragma ENDASM CCAP4L = 0; CCAP4H = CH-1; #pragma ASM POP ACC #pragma ENDASM EA = 1; }
Why on earth would you want to do it in 'C'?? It is highly unlikely to be a safe thing to do...
Just an exampel how to use PUSH & POP
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; }