-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.c
65 lines (54 loc) · 1.19 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include "fifth.h"
#include "stack.h"
/*
* we intentionally do not controlling boundaries.
* invalid program should work incorrectly
*
* zero-th item in vma->data is not used.
*/
void stack_push( struct vma *v, uint8_t b )
{
if ( v->idx <= FIFTH_STACK_SIZE ) {
v->data[ ++v->idx ] = b;
} else {
fprintf( ERR_STREAM, "Stack is full\n" );
}
}
uint8_t stack_pop( struct vma *v)
{
return v->data[ v->idx -- ];
}
/** \param i -- index from top of stack. 0 means top of stack.
*/
uint8_t stack_val( struct vma *v, uint16_t i )
{
return v->data[ v->idx - i ];
}
void stack_clear( struct vma *v )
{
v->idx = 0;
memset( v->data, 0, FIFTH_STACK_SIZE );
}
void stack_print( struct vma *v )
{
int i = 0;
fprintf( ERR_STREAM, "STACK: " );
for ( i = v->idx; i > 0; i -- ) {
fprintf( ERR_STREAM, "%3d ", v->data[ i ] );
}
fprintf( ERR_STREAM, "\n" );
}
/** \brief Function that writes to stack as to random access memory.
* It needs to close from user that data memory is stack.
*
*/
int stack_write( struct vma *v, uint8_t addr, uint8_t data )
{
if( addr > v->idx ) {
return 1;
}
v->data[ addr ] = data;
return 0;
}