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:
parent
15b385876c
commit
fd86df806b
139
assembly.c
139
assembly.c
@ -240,32 +240,84 @@ char *disassemble(uint32_t opcode_be){
|
||||
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'){
|
||||
uint32_t addr;
|
||||
if(sscanf(str,"0x%08X",&addr)!=1){
|
||||
return -1;
|
||||
}else
|
||||
return addr;
|
||||
imm_ret->status=1;
|
||||
return imm_ret;
|
||||
}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
|
||||
int len=0;
|
||||
for(int i=0;i<MAX_LABEL_SIZE+1;i++){
|
||||
if(i==MAX_LABEL_SIZE)
|
||||
return -2;
|
||||
if(str[i]==0||str[i]=='\n'||str[i]==' '){
|
||||
if(i==MAX_LABEL_SIZE){
|
||||
imm_ret->status=2;
|
||||
return imm_ret;
|
||||
}
|
||||
if(str[i]==0||str[i]=='\n'||str[i]==' '||str[i]==','||str[i]==')'){
|
||||
break;
|
||||
}else
|
||||
len++;
|
||||
}
|
||||
imm_ret->size+=len;
|
||||
|
||||
struct label_ll_t *search=assembler_context->label_ll;
|
||||
while(search){
|
||||
if(strncmp(str,search->name,len)==0)
|
||||
return search->address;
|
||||
if(strncmp(str,search->name,len)==0){
|
||||
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;
|
||||
}
|
||||
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++;
|
||||
if(line[x]=='$'){
|
||||
x++;
|
||||
int64_t imm=parse_immediate(line+x,assembler_context);
|
||||
if(imm<0){
|
||||
switch(imm){
|
||||
default:
|
||||
struct imm_ret_t *imm=parse_immediate(line+x,assembler_context);
|
||||
uint32_t temp;
|
||||
if(!imm)
|
||||
return -2;
|
||||
case -2:
|
||||
if(imm->status){
|
||||
switch(imm->status){
|
||||
default:
|
||||
free(imm);
|
||||
return -2;
|
||||
case 2:
|
||||
free(imm);
|
||||
return -3;
|
||||
}
|
||||
}else
|
||||
return opcode<<24|(imm&0x00FFFFFF);
|
||||
}else{
|
||||
temp=imm->value;
|
||||
free(imm);
|
||||
return opcode<<24|(temp&0x00FFFFFF);
|
||||
}
|
||||
}else
|
||||
return -2;
|
||||
}else{
|
||||
@ -398,10 +458,25 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
|
||||
while(line[x]==' ')x++;
|
||||
if(line[x]=='$'){
|
||||
x++;
|
||||
if(sscanf(line+x,"%06X",&data)==1){
|
||||
x+=4;
|
||||
struct imm_ret_t *imm=parse_immediate(line+x,assembler_context);
|
||||
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++;
|
||||
if(line[x]!=','){ //24 bit TODO: not the best way to parse this
|
||||
if(line[x]!=','){
|
||||
x+=2;
|
||||
while(line[x]==' ')x++;
|
||||
}
|
||||
@ -435,8 +510,6 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
|
||||
return -2;
|
||||
}else
|
||||
return -2;
|
||||
}else
|
||||
return -2;
|
||||
}else if(line[x]=='%'){
|
||||
x++;
|
||||
if(line[x]=='R'){
|
||||
@ -563,16 +636,24 @@ int64_t assemble_line(char *line, struct assembler_context_t *assembler_context)
|
||||
while(line[x]==' ')x++;
|
||||
if(line[x]=='$'){
|
||||
x++;
|
||||
int64_t imm=parse_immediate(line+x,assembler_context);
|
||||
if(imm<0){
|
||||
switch(imm){
|
||||
default:
|
||||
struct imm_ret_t *imm=parse_immediate(line+x,assembler_context);
|
||||
uint32_t temp;
|
||||
if(!imm)
|
||||
return -2;
|
||||
case -2:
|
||||
if(imm->status){
|
||||
switch(imm->status){
|
||||
default:
|
||||
free(imm);
|
||||
return -2;
|
||||
case 2:
|
||||
free(imm);
|
||||
return -3;
|
||||
}
|
||||
}else
|
||||
return imm;
|
||||
}else{
|
||||
temp=imm->value;
|
||||
free(imm);
|
||||
return temp;
|
||||
}
|
||||
}else
|
||||
return -2;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user