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

📄 source.c

📁 AVR反汇编,对ATMEGA8有效
💻 C
📖 第 1 页 / 共 2 页
字号:
							break;

						default:

							sprintf(
								temp_string,
								"TSourceOut:\n"
								"\t%s instruction has unknown arg0_format: '%c'\n"
								"\targ0 is 0x%X",
								Instruction.name,
								Instruction.arg0_format,
								Instruction.arg0 );

							throw TGenericError( temp_string ); 

							break;

					} // end "switch( Instruction.arg0_format )"

				} // end else part of "if( Arg_longCall == Instruction.arg0_type )"

				if( TAvr::ARG_longCall == Instruction.arg1_type ){

					// These arg types are special because they also use the
					// next word as part of the opcode.

					// A really paranoid programmer would do a bounds check
					// on addr here.
					addr += 2;
					temp_short = pFlash->get_flash_word(
						addr,
						&high_byte_written,
						&low_byte_written );


					if( !high_byte_written || !low_byte_written ){
						sprintf(
							temp_string,
							"TSourceOut: Word Address: %04X: Needed Entire Word Written",
							addr );
						throw TGenericError( temp_string ); 
					}

					temp_long =
						( (unsigned long)Instruction.arg1 << 16 ) |
						( (unsigned long)temp_short );

					strcpy( temp_string2, temp_string );

					sprintf(
						temp_string,
						"%s, 0x%04lX",
						temp_string2,
						temp_long );

				} else if( TAvr::ARG_neg64_63 == Instruction.arg1_type ) {

					// We insist that an ARG_neg64_63 arg type have
					// an arg format of 'k'.
					if( 'k' != Instruction.arg1_format ){

						sprintf(
							temp_string,
							"TSourceOut:\n"
							"\t%s instruciton has arg1_type = ARG_neg64_63\n"
							"\twith 'k' != ( arg1_format = '%c' )",
							Instruction.name,
							Instruction.arg1_format );
				
						throw TGenericError( temp_string ); 
					}

					strcpy( temp_string2, temp_string );

					sprintf(
						temp_string,
						"%s, %d",
						temp_string2,
						(short)Instruction.arg1 );

					have_dest = TRUE;
					//dest = addr + 2 + (short)Instruction.arg1;
					//there is a special case;
					if(Instruction.arg1<0 && Instruction.arg1<-4*1024){
						unsigned int offset = 0;
						offset = addr + 2 + Instruction.arg1;
						offset = offset &0x00000FFF;
						dest = offset;
					}else{
						dest = addr + 2 + (short)Instruction.arg1;
					}

				} else if (TAvr::ARG_0_PAIR==Instruction.arg1_type){
					strcpy( temp_string2, temp_string );
					sprintf(
								temp_string,
								"%s, r%d:r%d",
								temp_string2,
								Instruction.arg1*2+1,Instruction.arg1*2);
				}else{

					switch( Instruction.arg1_format ){

						case '\0':
							// No arg1 to this instruction.  No more to do.
							break;

						case 'b':
						case 'k':

							strcpy( temp_string2, temp_string );

							sprintf(
								temp_string,
								"%s, %d",
								temp_string2,
								(short)Instruction.arg1 );

							break;

						case 'd':
						case 'r':

							strcpy( temp_string2, temp_string );

							/*sprintf(
								temp_string,
								"%s, r%d",
								temp_string2,
								Instruction.arg1 );*/

							//change register name
							char r_name[4];
							sprintf(r_name,"r%d",Instruction.arg1);
							switch(Instruction.arg1){
								case 26:
									strcpy(r_name,"XL");
									break;
								case 27:
									strcpy(r_name,"XH");
									break;
								case 28:
									strcpy(r_name,"YL");
									break;
								case 29:
									strcpy(r_name,"YH");
									break;
								case 30:
									strcpy(r_name,"ZL");
									break;
								case 31:
									strcpy(r_name,"ZH");
									break;

							}
							sprintf(
								temp_string,
								"%s, %s",
								temp_string2,
								r_name );

							break;

						case 'K':
							strcpy( temp_string2, temp_string );

							sprintf(
								temp_string,
								"%s, 0x%X",
								temp_string2,
								Instruction.arg1 );

							break;
						case 'P':

							strcpy( temp_string2, temp_string );

							sprintf(
								temp_string,
								"%s, %s",
								temp_string2,
								m8_IO_register_name[Instruction.arg1] );

							break;

						case 'X':
						case 'Y':
						case 'Z':

							strcpy( temp_string2, temp_string );

							if( 0 == strcmp( "ldd", Instruction.name )){
								sprintf(
									temp_string,
									"%s, %c+%d",
									temp_string2,
									Instruction.arg1_format,
									Instruction.arg1 );

							} else {

								// It's a little convoluted how we get the
								// information back from TAvr::AdjustArg about
								// whether to us "X", "X+", or "-X".  In this
								// arg_format is 'X'.  We differentiate by setting
								// the value of arg (which is an (unsigned short))
								// to one of ' ', '+', or '-'.
								switch( (char)Instruction.arg1 ){

									case ' ':

										sprintf(
											temp_string,
											"%s, %c",
											temp_string2,
											Instruction.arg1_format );

										break;

									case '+':

										sprintf(
											temp_string,
											"%s, %c+",
											temp_string2,
											Instruction.arg1_format );

										break;

									case '-':

										sprintf(
											temp_string,
											"%s, -%c",
											temp_string2,
											Instruction.arg1_format );

										break;

									default:

										sprintf(
											temp_string,
											"TSourceOut:\n"
											"\t%s instruction has special arg1_format: '%c'\n"
											"\twith unknown arg1 of: 0x%X",
											Instruction.name,
											Instruction.arg1_format,
											Instruction.arg1 );
				
										throw TGenericError( temp_string ); 

										break;
								}
							}

							break;

						default:

							sprintf(
								temp_string,
								"TSourceOut:\n"
								"\t%s instruction has unknown arg1_format: '%c'\n"
								"\targ1 is 0x%X",
								Instruction.name,
								Instruction.arg1_format,
								Instruction.arg1 );

							throw TGenericError( temp_string ); 

							break;
					} // end "switch( Instruction.arg1_format )"

				} // end else part of "if( Arg_longCall == Instruction.arg1_type )"

				// We'll make it line up pretty assuming 8 space tabs.  I'm
				// taking a chance here and making the code as a whole a little
				// longer to try to decouple this process from the rest of this
				// function.
				//pLastTab = rindex( temp_string, '\t' );
				pLastTab = strrchr( temp_string, '\t' );

				//if( index( temp_string, '\t' ) == pLastTab ){
				if( strrchr( temp_string, '\t' ) == pLastTab ){

					// There is only one tab.  Therefore this is a simple
					// instruction with no arguments.  Since none of these are
					// 8 or more characters long, we know we haven't advanced
					// the hypothetical cursor to the args column yet.
					// Therefore we know that we need 3 tabs.
					tabs = "\t\t\t";

				} else if( 8 > strlen ( pLastTab + 1 ) ){

					// This is a short set of arguments.  We need 2 tabs to get
					// into the comments column.
					tabs = "\t\t";

				} else {

					// This is a long set of arguments.  We need 1 tab to get
					// into the comments column.
					tabs = "\t";
				}

				// Add the address and possibly the destination.
				// add the op_code
				if( have_dest ){

					sprintf(
						temp_string2,
						"%s%s; %04X:[%4X], Dest: %04X",
						temp_string,
						tabs,
						op_code_addr,
						code_word,
						dest );

				} else {

					sprintf(
						temp_string2,
						"%s%s; %04X:[%04X]",
						temp_string,
						tabs,
						op_code_addr,
						code_word);
				}

				pSourceString_tail =
					new TSourceString( temp_string2, pSourceString_tail );

				if ( NULL == pSourceString_head ){
					pSourceString_head = pSourceString_tail;
				}

			} // end while( 0 != ( Index = Avr.Word2Instruction

			line_count = 0;
			if( pSourceString_head ){

				// We were able to match the opcode to an instruction.

				if( pSourceString_head->next && !last_line_was_blank ){
					// There is more than one equavalent source strings that
					// can make this opcode.  Put in a blank line to seperate
					// these equivalent source strings from the other source.
					push_body( "" );
				}

				while( pSourceString_head ){

					if ( 0 == line_count ){
						strcpy( temp_string, pSourceString_head->string );
					} else {
						sprintf(
							temp_string,
							"\t;%s",
							pSourceString_head->string + 1 );
					}

					push_body( temp_string );
					pSourceString_temp = pSourceString_head->next;
					delete pSourceString_head;
					pSourceString_head = pSourceString_temp;

					line_count++;
				}

				if ( 1 < line_count ){

					// There was more than one line in this block.
					// Put out a blank line.
					last_line_was_blank = TRUE;
					push_body( "" );

				} else {

					last_line_was_blank = FALSE;
				}

			} else {

				// No instruction matched this opcode.  Print it as data.

				sprintf(
					temp_string,
					"\t.dw\t0x%04X\t\t; %04X:[%04X]",
					code_word,
					op_code_addr,
					code_word);

				push_body( temp_string );
				last_line_was_blank = FALSE;
			}

		} else {

			look_for_new_segment = TRUE;

		}
	}

	while( prolog_head ){
		fputs( prolog_head->string, pOutFile );
		fputc( '\n', pOutFile );
		pSourceString_temp = prolog_head->next;
		delete prolog_head;
		prolog_head = pSourceString_temp;
	}

	while( body_head ){
		fputs( body_head->string, pOutFile );
		fputc( '\n', pOutFile );
		pSourceString_temp = body_head->next;
		delete body_head;
		body_head = pSourceString_temp;
	}

	fclose(pOutFile); //!
}

void TSourceOut::push_prolog( const char* s){

	prolog_tail = new TSourceString( s, prolog_tail );

	if( NULL == prolog_head ){
		prolog_head = prolog_tail;
	}
}

void TSourceOut::push_body( const char* s){

	body_tail = new TSourceString( s, body_tail );

	if( NULL == body_head ){
		body_head = body_tail;
	}
}

TSourceOut::TSourceString::TSourceString(
	const char* s,
	TSourceString* previous ){

	strncpy( string, s, 79 );	// FIXME - magic number
	string[ 79 ] = '\0';

	if( NULL != previous )
		previous->next = this;

	next = NULL;
}

⌨️ 快捷键说明

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