You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							56 lines
						
					
					
						
							1.3 KiB
						
					
					
				
			
		
		
	
	
							56 lines
						
					
					
						
							1.3 KiB
						
					
					
				#include "../common.h" | 
						|
 | 
						|
static void | 
						|
decode_and_print_reg_reg_movs(u8 *data, u64 size) | 
						|
{ | 
						|
    printf("bits 16\n\n"); | 
						|
     | 
						|
    /* We do not handle corrupt binaries */ | 
						|
     | 
						|
    for (u64 i = 0; i < size; i += 2) { | 
						|
        u8 b1 = data[i + 0]; | 
						|
        u8 b2 = data[i + 1]; | 
						|
         | 
						|
        u8 D_flag = (b1 & 0x2) >> 1; | 
						|
        u8 W_flag = b1 & 0x1; | 
						|
         | 
						|
        u8 mod       = (b2 & 0xC0) >> 6; | 
						|
        u8 reg_field = (b2 & 0x38) >> 3; | 
						|
        u8 rm_field  = b2 & 0x7; | 
						|
         | 
						|
        const char **regs = W_flag ? REGS_W1 : REGS_W0; | 
						|
        const char **rms = W_flag ? RM_W1 : RM_W0; | 
						|
         | 
						|
        if (D_flag) { | 
						|
            /* Instruction destination is specified in REG field */ | 
						|
            printf("mov %s, %s\n", regs[reg_field], rms[rm_field]); | 
						|
        } else { | 
						|
            /* Instruction source is specified in REG field */ | 
						|
            printf("mov %s, %s\n", rms[rm_field], regs[reg_field]); | 
						|
        } | 
						|
    } | 
						|
} | 
						|
 | 
						|
int | 
						|
main(int argc, char **argv) | 
						|
{ | 
						|
    if (argc != 2) { | 
						|
        fprintf(stderr, "Usage: %s binary_file\n", argv[0]); | 
						|
        return(1); | 
						|
    } | 
						|
     | 
						|
    char *filename = argv[1]; | 
						|
    int fd = open(filename, O_RDONLY); | 
						|
     | 
						|
    if (fd == -1) { | 
						|
        perror("open"); | 
						|
        return(1); | 
						|
    } | 
						|
     | 
						|
    u64 size = file_size(fd); | 
						|
    u8 *data = read_file(fd, size); | 
						|
     | 
						|
    decode_and_print_reg_reg_movs(data, size); | 
						|
     | 
						|
    return(0); | 
						|
} |