ASM: Implemented the ability to reference only the upper or lower double-word of a label's address to accommodate the limited register loading mechanism in the intruction set

This commit is contained in:
(Tim) Efthimis Kritikos 2024-02-14 19:36:17 +00:00
parent 15b385876c
commit fd86df806b
2 changed files with 132 additions and 51 deletions

View File

@ -240,32 +240,84 @@ char *disassemble(uint32_t opcode_be){
return ret; return ret;
} }
int64_t parse_immediate(char* str,struct assembler_context_t *assembler_context){ struct imm_ret_t{
uint8_t status;
uint32_t value;
uint8_t size;
};
struct imm_ret_t *parse_immediate(char* str,struct assembler_context_t *assembler_context){
struct imm_ret_t *imm_ret=malloc(sizeof(struct imm_ret_t));
if(!imm_ret)
return 0;
imm_ret->size=0;
if(*str=='0'){ if(*str=='0'){
uint32_t addr; uint32_t addr;
if(sscanf(str,"0x%08X",&addr)!=1){ if(sscanf(str,"0x%08X",&addr)!=1){
return -1; imm_ret->status=1;
}else return imm_ret;
return addr;
}else{ }else{
imm_ret->size+=2;
if(str[0]=='0'&&str[1]=='x'){
int i=2;
while(1){
if((str[i]>='0'&&str[i]<='9')||(str[i]>='a'&&str[i]<='f')||(str[i]>='A'&&str[i]<='F'))
imm_ret->size++;
else
break;
i++;
}
}else{
imm_ret->status=1;
return imm_ret;
}
imm_ret->value=addr;
imm_ret->status=0;
return imm_ret;
}
}else{
int select=0;
if(*str=='<'){
select=1;
imm_ret->size++;
str++;
}else if(*str=='>'){
select=2;
imm_ret->size++;
str++;
}
// Get name size // Get name size
int len=0; int len=0;
for(int i=0;i<MAX_LABEL_SIZE+1;i++){ for(int i=0;i<MAX_LABEL_SIZE+1;i++){
if(i==MAX_LABEL_SIZE) if(i==MAX_LABEL_SIZE){
return -2; imm_ret->status=2;
if(str[i]==0||str[i]=='\n'||str[i]==' '){ return imm_ret;
}
if(str[i]==0||str[i]=='\n'||str[i]==' '||str[i]==','||str[i]==')'){
break; break;
}else }else
len++; len++;
} }
imm_ret->size+=len;
struct label_ll_t *search=assembler_context->label_ll; struct label_ll_t *search=assembler_context->label_ll;
while(search){ while(search){
if(strncmp(str,search->name,len)==0) if(strncmp(str,search->name,len)==0){
return search->address; if(select==1)
imm_ret->value=search->address>>16;
else if(select==2)
imm_ret->value=search->address&0x0000FFFF;
else
imm_ret->value=search->address;
imm_ret->status=0;
return imm_ret;
}
search=search->next; search=search->next;
} }
return -2;
imm_ret->status=2;
return imm_ret;
} }
} }
@ -311,16 +363,24 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
while(line[x]==' ')x++; while(line[x]==' ')x++;
if(line[x]=='$'){ if(line[x]=='$'){
x++; x++;
int64_t imm=parse_immediate(line+x,assembler_context); struct imm_ret_t *imm=parse_immediate(line+x,assembler_context);
if(imm<0){ uint32_t temp;
switch(imm){ if(!imm)
default:
return -2; return -2;
case -2: if(imm->status){
switch(imm->status){
default:
free(imm);
return -2;
case 2:
free(imm);
return -3; return -3;
} }
}else }else{
return opcode<<24|(imm&0x00FFFFFF); temp=imm->value;
free(imm);
return opcode<<24|(temp&0x00FFFFFF);
}
}else }else
return -2; return -2;
}else{ }else{
@ -398,10 +458,25 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
while(line[x]==' ')x++; while(line[x]==' ')x++;
if(line[x]=='$'){ if(line[x]=='$'){
x++; x++;
if(sscanf(line+x,"%06X",&data)==1){ struct imm_ret_t *imm=parse_immediate(line+x,assembler_context);
x+=4; if(!imm)
return -2;
if(imm->status){
switch(imm->status){
default:
free(imm);
return -2;
case 2:
free(imm);
return -3;
}
}else{
data=imm->value;
x+=imm->size;
free(imm);
}
while(line[x]==' ')x++; while(line[x]==' ')x++;
if(line[x]!=','){ //24 bit TODO: not the best way to parse this if(line[x]!=','){
x+=2; x+=2;
while(line[x]==' ')x++; while(line[x]==' ')x++;
} }
@ -435,8 +510,6 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
return -2; return -2;
}else }else
return -2; return -2;
}else
return -2;
}else if(line[x]=='%'){ }else if(line[x]=='%'){
x++; x++;
if(line[x]=='R'){ if(line[x]=='R'){
@ -563,16 +636,24 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
while(line[x]==' ')x++; while(line[x]==' ')x++;
if(line[x]=='$'){ if(line[x]=='$'){
x++; x++;
int64_t imm=parse_immediate(line+x,assembler_context); struct imm_ret_t *imm=parse_immediate(line+x,assembler_context);
if(imm<0){ uint32_t temp;
switch(imm){ if(!imm)
default:
return -2; return -2;
case -2: if(imm->status){
switch(imm->status){
default:
free(imm);
return -2;
case 2:
free(imm);
return -3; return -3;
} }
}else }else{
return imm; temp=imm->value;
free(imm);
return temp;
}
}else }else
return -2; return -2;
} }

View File

@ -1,4 +1,4 @@
MOV $FF0000,%SP MOV $0xFF0000,%SP
#Test verious instructions #Test verious instructions
MOV $0x0001,%R0l MOV $0x0001,%R0l
@ -56,8 +56,8 @@ MOV %R1,(%R0)
MOV $0xcccd,%R1l MOV $0xcccd,%R1l
MOV $0x3f4c,%R1h MOV $0x3f4c,%R1h
MOV %R1,(%R0) MOV %R1,(%R0)
MOV $0xF0D0,%R0l MOV $>TR_LOW,%R0l
MOV $0x0000,%R0h MOV $<TR_LOW,%R0h
MOV %R1,(%R0) MOV %R1,(%R0)
MOV (%R0),%R5 MOV (%R0),%R5