Skip to content

Commit

Permalink
Start on assembler
Browse files Browse the repository at this point in the history
  • Loading branch information
whampson committed Jun 13, 2019
1 parent 2be6968 commit 2506ef6
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Source files
file(GLOB LIB_SOURCES "src/lib/*.c")
file(GLOB AS_SOURCES "src/as/*.c")
file(GLOB EMU_SOURCES "src/emu/*.c")

# Library for shared code
add_library(lc3tools ${LIB_SOURCES})
target_include_directories(lc3tools PUBLIC include)

# Executables
add_executable(lc3as ${AS_SOURCES})
add_executable(lc3emu ${EMU_SOURCES})
target_include_directories(lc3as PRIVATE include/as)
target_include_directories(lc3emu PRIVATE include/emu)

# Link shared code and executables
target_link_libraries(lc3as lc3tools)
target_link_libraries(lc3emu lc3tools)
6 changes: 6 additions & 0 deletions include/as/lc3as.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __LC3AS_H
#define __LC3AS_H

#define LC3AS_TEST 6969

#endif /* __LC3AS_H */
2 changes: 2 additions & 0 deletions include/lc3tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
#define DEBUG 1

#define TEST_DIR "../test"

/*
* File separator character.
*/
Expand Down
123 changes: 123 additions & 0 deletions src/as/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <lc3as.h>
#include <lc3tools.h>

#define LINE_BUFFER_SIZE 256

struct source_file
{
FILE *file;
char line[LINE_BUFFER_SIZE];
size_t line_len;
size_t line_num;
size_t line_pos;
};

int read_line(struct source_file *src)
{
src->line_num++;
if (feof(src->file) || fgets(src->line, LINE_BUFFER_SIZE, src->file) == 0)
{
return -1;
}

src->line_len = strlen(src->line);
src->line_pos = 0;

return src->line_len;
}

int read_token(struct source_file *src, char **tok, char delim)
{
size_t i;
// char *tok;
char *tok_head;
char *tok_tail;
char c;
int char_seen;

if (src->line_pos >= src->line_len)
{
return 0;
}

tok_head = &src->line[src->line_pos];
tok_tail = tok_head;
i = 0;
char_seen = 0;

while ((c = tok_head[i]) != '\0')
{
src->line_pos++;
if (isspace(c))
{
if (!char_seen)
{
tok_head++;
tok_tail++;
continue;
}
else if (c == '\n')
{
*tok_tail = '\0';
*tok = tok_head;
return 1;
}
else if (!isspace(delim))
{
i++;
continue;
}
}

char_seen = 1;
if (c == delim)
{
*tok_tail = '\0';
*tok = tok_head;
return 1;
}
i++;
tok_tail++;
}

return 0;
}

int main(int argc, char *argv[])
{
struct source_file src;
char *tok;

memset(&src, 0, sizeof(struct source_file));
src.file = fopen(TEST_DIR"/astest2.asm", "rb");
if (src.file == NULL)
{
return 2;
}

while (read_line(&src) != -1)
{
read_token(&src, &tok, ' ');
// read_token(&src, &tok, '\t');

printf("tok: %s\n", tok);
if (strcasecmp(tok, "ADD") == 0)
{
printf("ADD\n");
while ((read_token(&src, &tok, ',')) != 0)
{
printf("'%s'\n", tok);
}
}
}

printf("Hit end of file! (line %zu)\n", src.line_num);
fclose(src.file);

return 0;
}

0 comments on commit 2506ef6

Please sign in to comment.