⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 label.c

📁 AVR反汇编,对ATMEGA8有效
💻 C
字号:
/*
  Program: revava - Atmel Dis-Assembler
  File: Label.cpp
  Parts of this are Copyright (C) 1997-1999 Uros Platise
  The rest of it is Copyright (C) 2001 Daniel J. Winker
  This file is added by Tomislucky, to replace perl script to generate label in asm file.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Error.h"
#include "Label.h"

class asm_line{
public:
	asm_line();
	char original_text[80];

	bool parsed;
	bool is_sub;

	char label[8];
	char code[80];
	char addr[5];
	char dest[4];
};
asm_line::asm_line(){
	is_sub = false;
	parsed = false;
	dest[0] = '\0';
	label[0]= '\0';
	label[7]= '\0';
}
#define MAX_LINES 1024*20
asm_line asm_line_array[MAX_LINES];

TLabel::TLabel( const char* InputFile,const char* OutFile){
	int i;
	char buff[80];// FIXME - Magic Number
	int line_number = 0;
	char *addr_token = "; ";
	char *token;

	
	FILE *fpInput = fopen(InputFile,"r");
	FILE *fp_out = fopen(OutFile,"w");

	while(!feof(fpInput)){
		memset(buff,'\0',80);
		fgets(buff,80,fpInput);
		//printf("%s",buff);
		asm_line parsed_asm_line;
		strncpy(parsed_asm_line.original_text,buff,80);		
		char *pdest;
		pdest = strstr(buff,"; ");	
		if(pdest!=NULL){
			//found it!
			strncpy(parsed_asm_line.addr,pdest+2,4);
			
		}
		pdest = strstr(buff,"Dest: ");	
		if(pdest!=NULL){
			//found it!
			strncpy(parsed_asm_line.dest,pdest+6,4);			
		}
		asm_line_array[line_number] = parsed_asm_line;
		line_number++;


	}
	

	//label it!
	int j = 0;
	for(i = 0; i<line_number; i++)	{		
		if (asm_line_array[i].dest[0]!='\0'){
			bool is_sub = false;
			char code[5];
			code[4] = '\0';
			if(strstr(asm_line_array[i].original_text,"call")!=NULL){
				is_sub = true;
			}
			//find the dest 
			for(j = 0; j<line_number; j++){
				if(strncmp(	asm_line_array[i].dest,asm_line_array[j].addr,4)==0){//found it!					
					int len = strlen(asm_line_array[i].original_text);
					char label[8];
					label[7] = '\0';
					char *pdest;
					//is there already a label?
					if(asm_line_array[j].label[0]!='\0')
					{
							strncpy(label,asm_line_array[j].label,8);
					}
					else
					{
						if(is_sub){
							strncpy(label,"sub",3);
						}
						else
						{
							strncpy(label,"avr",3);
						}
						asm_line_array[j].is_sub = is_sub;
						strncpy(label+3,asm_line_array[j].addr,4);
						strncpy(asm_line_array[j].label,label,8);
					}

					//replace original offset
					pdest = strrchr(asm_line_array[i].original_text,';');
					int index  = pdest - asm_line_array[i].original_text ;						
					int start = index - 4;
					int count = 0;
					while(asm_line_array[i].original_text[start]!='\t'){
						start--;
						count++;
					}
					char tmp[80];
					strncpy(tmp,asm_line_array[i].original_text,start+1);
					strncpy(tmp+start+1,label,7);
					strncpy(tmp+start+1+7,asm_line_array[i].original_text+index-1,len-index+1);
					tmp[len-count+5] = '\0';
					
					strncpy(asm_line_array[i].original_text,tmp,len-count+7);

					break;

				}

			}
			//did not find
			if(j==line_number){
				int k = 0;
				memcpy(code,strchr(asm_line_array[i].original_text,'[')+1,4);
				char tmp_string[80];
				//special case, an comment line;
				int z = 0;
				bool comment_line = true;
				while(asm_line_array[i].original_text[z]!=';'){
					if(asm_line_array[i].original_text[z]!=' ' &&asm_line_array[i].original_text[z]!='\t') {
						comment_line = false;
						break;
					}
					z++;
				}
				if(comment_line){
					sprintf(tmp_string,"\t; .dw 0x%s %s",code,strrchr(asm_line_array[i].original_text,';'));
				}else{
					sprintf(tmp_string,"\t.dw 0x%s %s",code,strrchr(asm_line_array[i].original_text,';'));
				}

				strcpy(asm_line_array[i].original_text,tmp_string);
			}
		}
	}
	//output it!
	for(i = 0; i<line_number; i++)	{
		if(asm_line_array[i].label[0]!='\0'){
			if(asm_line_array[i].is_sub){
				fprintf(fp_out,"\n;-------------------------------------------------------------------------------------------------------------------");
				fprintf(fp_out,"\n;*****");
				fprintf(fp_out,"\n;");
				fprintf(fp_out,"\n;%s:",asm_line_array[i].label);
				fprintf(fp_out,"\n;	    ");
				fprintf(fp_out,"\n;	    ");
				fprintf(fp_out,"\n;");
				fprintf(fp_out,"\n;*****");
			}
			fprintf(fp_out,"\n%s:\n",asm_line_array[i].label);
		}
		fprintf(fp_out,"%s",asm_line_array[i].original_text);
	}

	fclose(fpInput);
	fclose(fp_out);
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -