Im attempting to create an assembler in c of a sorts in which i take an input from a file that contains a command, in this case branchifequal
i then take its opcode which is 10
so byte[0] = 0xA0
and then i get the register for the next 4 bits and then the register for the next 4 but then how do i process the offset? assuming that command is branchifequal r1 r2 8
then the hand assembly should be A1 20 00 08
. This is done i know by checking the top 4 bits of the address offset which are 0's and then the bottom 16 bits which are 0000 0000 0000 1000
i believe. which should convert to 00 08
in hex. How can i get this while checking the last 4 bits in the second byte and the 2 bits in the 3rd byte and then the last 2 bits in the 4th byte is just simply the hex value itself which is done in my code below. I have this same issue for my jump
command as well.
PR1.C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *ltrim(char *s) {
while (*s == ' ' || *s == '\t') s++;
return s;
}
char getRegister(char *text) {
if (*text == 'r' || *text=='R') text++;
return atoi(text);
}
char getHex(char *text){
int number = (int)strtol(text, NULL, 16);
char reservednum[5];
if(number <= 0xFFFF){
sprintf(&reservednum[0], "%04x", number);
}
return atoi(reservednum);
}
int assembleLine(char *text, unsigned char* bytes) {
text = ltrim(text);
char *keyWord = strtok(text," ");
if (strcmp("add",keyWord) == 0) {
bytes[0] = 0x10;
bytes[0] |= getRegister(strtok(NULL," "));
bytes[1] = getRegister(strtok(NULL," ")) << 4 | getRegister(strtok(NULL," "));
return 2;
}
else if(strcmp("subtract", keyWord) == 0){
bytes[0] = 0x50;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getRegister(strtok(NULL, " ")) << 4 | getRegister(strtok(NULL, " "));
return 2;
}
else if(strcmp("and", keyWord) == 0){
bytes[0] = 0x20;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getRegister(strtok(NULL, " ")) << 4 | getRegister(strtok(NULL, " "));
return 2;
}
else if(strcmp("divide", keyWord) == 0){
bytes[0] = 0x30;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getRegister(strtok(NULL, " ")) << 4 | getRegister(strtok(NULL, " "));
return 2;
}
else if(strcmp("multiply", keyWord) == 0){
bytes[0] = 0x40;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getRegister(strtok(NULL, " ")) << 4 | getRegister(strtok(NULL, " "));
return 2;
}
else if(strcmp("or", keyWord) == 0){
bytes[0] = 0x60;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getRegister(strtok(NULL, " ")) << 4 | getRegister(strtok(NULL, " "));
return 2;
}
else if(strcmp("halt", keyWord) == 0){
bytes[0] = 0x00;
bytes[1] = 0x00;
return 2;
}
else if(strcmp("return", keyWord) == 0){
bytes[0] = 0x70;
bytes[1] = 0x00;
return 2;
}
else if(strcmp("addimmediate", keyWord) == 0){
bytes[0] = 0x90;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getHex(strtok(NULL, " "));
return 2;
}
else if(strcmp("interrupt", keyWord) == 0){
bytes[0] = 0x80;
bytes[0] |= getHex(strtok(NULL, " "));
bytes[1] = 0x00;
return 2;
}
else if(strcmp("push", keyWord) == 0){
bytes[0] = 0x70;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = 0x40;
return 2;
}
else if(strcmp("pop", keyWord) == 0){
bytes[0] = 0x70;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = 0x80;
return 2;
}
else if(strcmp("jump", keyWord) == 0){
bytes[0] = 0xC0;
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = getHex(strtok(NULL, " "));
return 4;
}
else if(strcmp("branchifequal", keyWord) == 0){
bytes[0] = 0xA0;
bytes[0] |= getRegister(strtok(NULL, " "));
bytes[1] = getRegister(strtok(NULL, " ")) << 4;
bytes[2] = 0x00;
bytes[3] = getHex(strtok(NULL, " "));
return 4;
}
}
int main(int argc, char **argv) {
FILE *src = fopen(argv[1],"r");
FILE *dst = fopen(argv[2],"w");
while (!feof(src)) {
unsigned char bytes[4];
char line[1000];
if (NULL != fgets(line, 1000, src)) {
printf ("read: %s\n",line);
int byteCount = assembleLine(line,bytes);
fwrite(bytes,byteCount,1,dst);
}
}
fclose(src);
fclose(dst);
return 0;
}