s502 assembler
A very simple assembler for the 6502 line of processors written in C
Functions
pass_twothree.h File Reference

processing steps 2 and 3 More...

#include "state.h"

Go to the source code of this file.

Functions

int pass_two (State *s)
 compilation pass 2 More...
 
int write_data (State *s)
 compilation pass 3 More...
 

Detailed Description

processing steps 2 and 3

Check out documentation on steps

Definition in file pass_twothree.h.

Function Documentation

◆ pass_two()

int pass_two ( State s)

compilation pass 2

Parameters
scurrent state
Returns
0 on success, -1 on error

Simply subtitutes remaining operand values (forward labels).

Definition at line 19 of file pass_twothree.c.

19  {
20  for (TokensListElement* ptr = s->tokens->head; ptr != NULL; ptr = ptr->next) {
21  int ret = token_get_operand(s, ptr->token);
22  if (ret < 0 || (ptr->token->type == TT_INSTR && (ptr->token->instr.number < 0))) {
23  if (ret == 0) {
24  ERROR("Undefined label reference!\n");
25  }
26  token_print(ptr->token);
27  FAIL("Pass two failed!\n");
28  return -1;
29  }
30  }
31  return 0;
32 }

References ERROR, FAIL, TokensList::head, TokensListElement::next, Token::token_get_operand(), Token::token_print(), State::tokens, and TT_INSTR.

Referenced by main().

◆ write_data()

int write_data ( State s)

compilation pass 3

Parameters
scurrent state
Returns
0 on success, -1 on error

Writes binary data to file

Definition at line 69 of file pass_twothree.c.

69  {
70  // first concat, then write
71  // this way we only write (and destroy possible prev. file) if we can do that
72  int l;
73  char* data = concat_bin(s, &l);
74  if (!data) {
75  FAIL("Could not compile data!\n");
76  return -1;
77  }
78 
79  FILE* f = fopen(s->outfile, "wb");
80  if (!f) {
81  ERROR("An error occured opening the file %s!\n", s->outfile);
82  ERROR("Error opening file: %s\n", strerror(errno));
83  free(data);
84  return -1;
85  }
86  fwrite(data, 1, l, f);
87  free(data);
88  fclose(f);
89  return 0;
90 }

References concat_bin(), ERROR, FAIL, and State::outfile.

Referenced by main().

TT_INSTR
@ TT_INSTR
instruction token
Definition: token_t.h:26
State::tokens
TokensList * tokens
tokens
Definition: state.h:38
TokensListElement::next
struct TokensListElement * next
next element in list or NULL
Definition: tokenslist.h:20
State::outfile
char outfile[STATE_MAX_STRING_LEN]
output file name
Definition: state.h:46
TokensList::head
TokensListElement * head
head (first element) of the list
Definition: tokenslist.h:31
TokensListElement
An element of a TokensList.
Definition: tokenslist.h:16
concat_bin
char * concat_bin(State *s, int *n)
compile tokens and concat binary data
Definition: pass_twothree.c:42
Token::token_print
void token_print(Token *token)
Pretty-print one token, with its source and length.
Definition: tokenFunc.c:20
Token::token_get_operand
int token_get_operand(State *s, Token *t)
parse the operand of the instruction as a number
Definition: tokenFunc.c:287
FAIL
#define FAIL(...)
Fancy-print a fail (failed step). Works like printf.
Definition: logging.h:45
ERROR
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
Definition: logging.h:40