Skip to content

Instantly share code, notes, and snippets.

@tuket
Created January 14, 2026 21:15
Show Gist options
  • Select an option

  • Save tuket/640cc4d9f153df9001e96883a751869a to your computer and use it in GitHub Desktop.

Select an option

Save tuket/640cc4d9f153df9001e96883a751869a to your computer and use it in GitHub Desktop.
Computer Enhance - 1.1 solution
// Simple program to compare two binary files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* read_file(const char* filename, long* out_length)
{
FILE* f = fopen(filename, "rb");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
long length = ftell(f);
fseek(f, 0, SEEK_SET);
char* buffer = (char*)malloc(length);
if (!buffer) {
fclose(f);
return NULL;
}
fread(buffer, 1, length, f);
fclose(f);
if (out_length) {
*out_length = length;
}
return buffer;
}
int main(int argc, char** argv)
{
if (argc != 3) {
printf("Usage: \ncompare file1 file2\n");
return 1;
}
long file1_length = 0;
char* file1_data = read_file(argv[1], &file1_length);
if (!file1_data) {
printf("Failed to read file: %s\n", argv[1]);
return 1;
}
long file2_length = 0;
char* file2_data = read_file(argv[2], &file2_length);
if (!file2_data) {
printf("Failed to read file: %s\n", argv[2]);
free(file1_data);
return 1;
}
if (file1_length != file2_length)
printf("Files differ in length: %ld vs %ld bytes\n", file1_length, file2_length);
else if (memcmp(file1_data, file2_data, file1_length) != 0)
printf("Files differ in content.\n");
else
printf("Files are identical.\n");
free(file1_data);
free(file2_data);
}
#include <stdio.h>
#include <stdint.h>
typedef uint8_t u8;
inline u8 matches_mask(u8 val, u8 mask) { return (val & mask) == mask; }
static void u8_to_binaryString(u8 val, char* out)
{
for (int i = 0b10000000; i > 0; i >>= 1)
*out++ = (val & i) ? '1' : '0';
}
static const char* const reg_names[] = {
"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"
};
int main(int argc, char** argv)
{
if (argc != 2) {
printf("Usage: \ndisasm file.asm\n");
return 1;
}
FILE* f = fopen(argv[1], "rb");
if (!f) {
printf("Failed to open file: %s\n", argv[1]);
return 1;
}
printf("; Disassembly of %s\nbits 16\n\n", argv[1]);
u8 opcode;
while (fread(&opcode, 1, 1, f)) {
if (matches_mask(opcode, 0b10001000)) { // MOV Reg/Reg or Reg/Mem
u8 d = (opcode >> 1) & 0x1;
u8 w = opcode & 0x1;
u8 params;
fread(&params, 1, 1, f); // Read ModRM byte
if (matches_mask(params, 0b11000000)) { // Register to Register
u8 reg1 = (w << 3) | (params >> 3) & 0b111;
u8 reg2 = (w << 3) | (params & 0b111);
if (d == 0) {
u8 aux = reg1;
reg1 = reg2;
reg2 = aux;
}
printf ("mov %s, %s\n", reg_names[reg1], reg_names[reg2]);
}
}
}
}
; ========================================================================
;
; (C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
;
; This software is provided 'as-is', without any express or implied
; warranty. In no event will the authors be held liable for any damages
; arising from the use of this software.
;
; Please see https://computerenhance.com for further information
;
; ========================================================================
; ========================================================================
; LISTING 38
; ========================================================================
bits 16
mov cx, bx
mov ch, ah
mov dx, bx
mov si, bx
mov bx, di
mov al, cl
mov ch, ch
mov bx, ax
mov bx, si
mov sp, di
mov bp, ax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment