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

Number module to parse numbers. More...

#include "state.h"

Go to the source code of this file.

Enumerations

enum  {
  OPC_INVALID = 0xff, MAP_MAX_KEY_LEN = 32, NUMBER_ERROR = -1, NUMBER_LABEL_NODEF = -2,
  STATE_MAX_STRING_LEN = 32, TOKEN_BUFFER_SIZE = 100, TOKEN_SOURCE_FILE_SIZE = 32
}
 

Functions

int number_get_number (State *s, char *str)
 interpret a string as a constant, label or number More...
 
int number_char_to_digit (char c)
 Convert a hex char to a digit. More...
 

Detailed Description

Number module to parse numbers.

Handles number in decimal and hex, labels, constants and modifiers

Definition in file number.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
OPC_INVALID 

An invalid opcode to signal invalid / non-existent variations.

MAP_MAX_KEY_LEN 

Key buffer size for Map.

NUMBER_ERROR 

Could not parse a number or constant is undefined.

NUMBER_LABEL_NODEF 

Undefined label.

STATE_MAX_STRING_LEN 

max string length for input/output files

TOKEN_BUFFER_SIZE 

Token max length.

TOKEN_SOURCE_FILE_SIZE 

source filename max length. Longer strings will get truncated.

Definition at line 13 of file number.h.

13  {
15  NUMBER_ERROR = -1,
17  NUMBER_LABEL_NODEF = -2,
18 };

Function Documentation

◆ number_char_to_digit()

int number_char_to_digit ( char  c)

Convert a hex char to a digit.

Parameters
cchar to convert
Returns
value or NUMBER_ERROR on some error

Handles upper or lowercase chars
Returns NUMBER_ERROR on failed conversion

Definition at line 12 of file number.c.

12  {
13  if ('0' <= c && c <= '9')
14  return c - '0';
15  if ('a' <= c && c <= 'f')
16  c += 'A' - 'a';
17  if ('A' <= c && c <= 'F')
18  return c - 'A' + 10;
19  return NUMBER_ERROR;
20 }

References NUMBER_ERROR.

Referenced by instruction_load(), and number_parse_number().

◆ number_get_number()

int number_get_number ( State s,
char *  str 
)

interpret a string as a constant, label or number

Parameters
sState to read constant / label values from
strstring to interpret
  • get the value of a constant or label
  • parse number
  • handles modifier symbols
    Returns NUMBER_ERROR on error, or NUMBER_LABEL_NODEF if number is a valid label with no value

Definition at line 75 of file number.c.

75  {
76 
77  int p = (str[0] == '>' || str[0] == '<') ? 1 : 0;
78  int num = number_get_raw(s, &str[p]);
79 
80  if (num < 0) return num;
81 
82  num = str[0] == '>' ? num >> 8 : num;
83  num &= str[0] == '>' || str[0] == '<' ? 0xff : 0xffff;
84 
85  return num;
86 }

References number_get_raw().

Referenced by compile_data(), compile_pad(), and token_get_operand().

number_get_raw
int number_get_raw(State *s, char *str)
interpret number without modifiers
Definition: number.c:62
NUMBER_ERROR
@ NUMBER_ERROR
Could not parse a number or constant is undefined.
Definition: number.h:15
NUMBER_LABEL_NODEF
@ NUMBER_LABEL_NODEF
Undefined label.
Definition: number.h:17