KEIL用C语言加入PUSH,POP

供稿:hz-xin.com     日期:2025-01-14
C语言 push和pop函数可以直接用吗?

#include #include #define MAXSIZE 32typedef struct{int *elem;/* 栈的存储区 */ int max; /* 栈的容量,即找中最多能存放的元素个数 */ int top; /* 栈顶指针 */ }Stack;int InitStack(Stack *S, int n) /*创建容量为n的空栈*/{S->elem = (int *)malloc(n * sizeof(int));if(S->elem==NULL) return -1;S->max=n;S->top =0; //栈顶初值0return 0;}int Push(Stack *S, int item) /*将整数item压入栈顶*/{if(S->top==S->max) {printf("Stack is full!
");return -1;}S->elem[S->top++] = item; //压栈,栈顶加1return 0;}int StackEmpty(Stack S){return (!S.top)?1:0; /*判断栈是否为空*/}int Pop(Stack *S) /*栈顶元素出栈*/{if(!S->top) {printf("Pop an empty stack!
");return -1;}return S->elem[--S->top] ; //弹出栈,栈顶减1}void MultibaseOutput(long n,int B){int m; Stack S;if(InitStack(&S,MAXSIZE)){printf("Failure!
");return;}do {if (Push(&S,B )) //------{printf("Failure!
");return;}n= n-1 ; //--------}while(n!=0);while(!StackEmpty(S)) { /*输出B进制的数*/m=Pop(&S);if(m<10) printf("%d",m); /*小于10,输出数字*/else printf("%c", m+55); /*大于或等于10,输出相应的字符*/}printf("
");}

这个算是数据结构的内容讲解的是一个叫做栈类型的数据结构,这个数据结构的特点就是后进先出--最后放进去的数据最先拿出来。pop函数就是拿出数据的操作,push是放入是数据的操作。
内容拓展:
pop函数呵push函数的使用:
#include #include #include void *clean(void *arg){printf("cleanup: %s
",(char *)arg);return (void *)0;}void * thr_fn1(void * arg){printf("chread 1 start
");pthread_cleanup_push((void *)clean,"thraed 1 first handler");pthread_cleanup_push((void *)clean,"thread 1 second handler");printf("thread 1 push complete
");if(arg){return ((void *)1);}pthread_cleanup_pop(0);pthread_cleanup_pop(0);return (void *)1;}//输出结果: chread 1 start ->thread 1 push complte //push和pop框起来的代码,不管正常退出还是异常退出,都将执行清除函数,但是存在特例:不包括return 退出。

在keil C51中,直接调用库函数:
#include<intrins.h> // 其中包含了对部分汇编指令的调用申明

_nop_(); // 产生一条NOP指令
_push_(acc); // 产生一条push指令

以下是intrins.h的内容
/*--------------------------------------------------------------------------
INTRINS.H

Intrinsic functions for C51.
Copyright (c) 1988-2004 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/

#ifndef __INTRINS_H__
#define __INTRINS_H__

extern void _nop_ (void);
extern bit _testbit_ (bit);
extern unsigned char _cror_ (unsigned char, unsigned char);
extern unsigned int _iror_ (unsigned int, unsigned char);
extern unsigned long _lror_ (unsigned long, unsigned char);
extern unsigned char _crol_ (unsigned char, unsigned char);
extern unsigned int _irol_ (unsigned int, unsigned char);
extern unsigned long _lrol_ (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
extern void _push_ (unsigned char _sfr);
extern void _pop_ (unsigned char _sfr);

#endif

#include<stdio.h>
int r;
extern add (int, int *);
main()
{
int i,j;
int a[6];

printf("six numbers:");
for(i=1;i<=6;i++)
{
scanf ("%d",&a[i]);
}
for(i=1;i<=6;i++)
{
add(a[i],&r);
a[i]=r;
}
for(i=1;i<=6;i++)
{
printf ("result is:%d\n",a[i]);
}

}

public _add:far
.model small
.code
_add proc
push bp
mov bp,sp
mov ax,[bp+4]
add ax,5
mov di,[bp+6]
mov [di],ax
POP bp
ret
_add endp
end