My Project
Data Structures | Typedefs | Functions
util.h File Reference

Defines interface to utility functions for LC3 assembler (do not modify). More...

#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  name_val
 

Typedefs

typedef struct name_val name_val_t
 

Functions

name_val_tutil_bin_search (name_val_t map[], int numNames, const char *name)
 
int util_get_opcode (const char *name)
 
int util_is_valid_label (const char *s)
 
int util_get_reg (const char *regStr)
 
int util_parse_cond (const char *condCodeStr)
 
bool util_get_int (const char *token, int *value)
 

Detailed Description

This defines the interface to some utilities for the LC3 assembler. In completing the corresponding .c file, you will learn how to encode information in data structures, rather than directly in "code". You will also learn how to do some simple character processing.

Author
Fritz Sieker

Typedef Documentation

typedef struct name_val name_val_t

Define a mapping between a name and an integer value

Function Documentation

name_val_t* util_bin_search ( name_val_t  map[],
int  numNames,
const char *  name 
)

Search a sorted list of items for a given name. The names are case insensitive, so the name "foo" and "Foo" match. This function is only used in util.c, but is declared here for documentation purposes.

Parameters
map- a sorted array of values
numNames- number of entries in the array
name- the name to search for (case insensitive)
Returns
NULL if the name is not in the list, or a pointer to the name/value pair.
Todo:
implement this function
bool util_get_int ( const char *  token,
int *  value 
)

Convert a string to an integer using the LC3 syntax for constants. The LC3 format assumes hex unless the string is preceeded by a &# sign to indicate decimal values. The initial x/X for hex values is optional. Hex values may be preceeded by a '-'.

Parameters
token- the string to be converted
value- pointer to where value will be stored
Returns
true on success, false on failure
Todo:
implement this function
int util_get_opcode ( const char *  name)

Get the opcode associated with an LC3 op (e.g. "ADD")

Parameters
name- name of operand
Returns
the opcode for this name, or -1 if the name is not and LC3 operation information about that instruction
Todo:
implement this function
int util_get_reg ( const char *  regStr)

Determine if the string represents a valid register specification A register is specified by the letter 'R' (case insesitive) followed by a digit 0 to 7.

Parameters
regStr- the string to check
Returns
the register number if the string is a valid register, or -1 if it is not.

This is an example of how to use the binary search routine

int util_is_valid_label ( const char *  s)

Determine if a string is a valid label or not. Valid labels beging with a letter or '_' and may contain only letters, digits and ('_'). You may find the C header ctype.h useful.

Parameters
s- ths string to validate @ return 0 if the string is not valid, non-zero if it is
Todo:
implement this function
int util_parse_cond ( const char *  condCodeStr)

Determine the condition code associated with the string. The string consists of of 0 to 3 three characters, with no more that one each character from the set 'nzp' (case insensitive). The values must follow that order. That is, if 'n' is present it MUST proceed 'z' and/or 'p'. If 'z' is present, in must preceed 'p'.

Parameters
condCodeStr- the string to parse (e.g. "zp")
Returns
- the value (1-7) representing the condition code or -1 if the condCodeStr is not valid.
Todo:
implement this function