mirror of
https://github.com/zoriya/dasm.git
synced 2026-08-15 18:44:40 +00:00
Parse arguments
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
setuptools
|
||||
._setuptools
|
||||
*.o
|
||||
dasm
|
||||
|
||||
@@ -2,7 +2,8 @@ NAME = dasm
|
||||
CFLAGS = -Wall -Wextra -Wshadow
|
||||
LDFLAGS =
|
||||
|
||||
SRC = src/main.c
|
||||
SRC = src/main.c \
|
||||
src/dasm.c
|
||||
|
||||
OBJ = $(SRC:%.c=%.o)
|
||||
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include "dasm.h"
|
||||
|
||||
void print_instruction(unsigned addr)
|
||||
{
|
||||
printf("%X\n", addr);
|
||||
}
|
||||
|
||||
int dasm(char *binary)
|
||||
{
|
||||
int pc = 0;
|
||||
|
||||
while (*binary) {
|
||||
instruction_t inst = *binary;
|
||||
print_instruction(pc);
|
||||
pc += inst.size;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
int dasm(char *binary);
|
||||
|
||||
typedef struct instruction {
|
||||
const char *name;
|
||||
unsigned size;
|
||||
} instruction_t;
|
||||
+48
-2
@@ -1,4 +1,50 @@
|
||||
int main()
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "dasm.h"
|
||||
|
||||
char *open_and_read(char *path)
|
||||
{
|
||||
|
||||
FILE *file = fopen(path, "r");
|
||||
if (!file) {
|
||||
dprintf(2, "Invalid path: %s. File does not exist.\n", path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int rc = fseek(file, 0L, SEEK_END);
|
||||
if (rc != 0) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
long size = ftell(file);
|
||||
if (size == -1) {
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
rewind(file);
|
||||
|
||||
char *ret = malloc((size + 1) * sizeof(char));
|
||||
size = fread(ret, sizeof(char), size, file);
|
||||
ret[size] = '\0';
|
||||
|
||||
fclose(file);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 3 || strcmp(argv[1], "-d")) {
|
||||
printf("Usage: %s -d <binary>\n", argv[0]);
|
||||
return 2;
|
||||
}
|
||||
|
||||
char *binary = open_and_read(argv[2]);
|
||||
if (!binary) {
|
||||
puts("Could not read binary file.");
|
||||
return 2;
|
||||
}
|
||||
|
||||
int ret = dasm(binary);
|
||||
free(binary);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user