Skip to content

9 calculation instructions add sub and or xor - #36

Merged
mathish06 merged 7 commits into
mainfrom
9-calculation-instructions-add-sub-and-or-xor
May 15, 2026
Merged

9 calculation instructions add sub and or xor#36
mathish06 merged 7 commits into
mainfrom
9-calculation-instructions-add-sub-and-or-xor

Conversation

@mathish06

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings May 15, 2026 11:58
@mathish06 mathish06 linked an issue May 15, 2026 that may be closed by this pull request
@mathish06
mathish06 merged commit ba20296 into main May 15, 2026
10 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for Corewar “calculation” instructions by implementing the missing opcode handlers and wiring them into the VM’s opcode dispatch.

Changes:

  • Added implementations for add, sub, and, or, xor instruction handlers.
  • Updated the process execution dispatcher to call the new handlers and made instruction fetch wrap safely in memory.
  • Registered the new source file in the build and exposed prototypes in the public header.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/instructions/instructions_calc.c Implements op_add, op_sub, op_and, op_or, op_xor plus argument decoding helpers.
src/core/execute_processes.c Adds opcode→function dispatch entries for the new instructions; wraps opcode fetch by MEM_SIZE.
Makefile Builds the newly added instruction source file.
include/corewar.h Declares new instruction handlers for linking/dispatch.
Comments suppressed due to low confidence (3)

src/instructions/instructions_calc.c:85

  • Same issue as op_add: returning early on invalid registers leaves proc->pc unchanged, which can lock the process on the same invalid sub instruction indefinitely. Advance the PC by the full instruction size (5 bytes) even when arguments are invalid.
    if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER)
        return;
    if (r3 < 0 || r3 >= REG_NUMBER)
        return;

src/instructions/instructions_calc.c:124

  • Same as op_and: validate types[] against op_tab for or (especially ensuring the 3rd arg is T_REG) and ensure PC advancement matches the actual encoded argument sizes when the coding byte is invalid.
    decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
    v1 = get_val(vm, proc, types[0], &pos);
    v2 = get_val(vm, proc, types[1], &pos);
    r3 = vm->arena[pos % MEM_SIZE] - 1;
    if (r3 >= 0 && r3 < REG_NUMBER) {

src/instructions/instructions_calc.c:144

  • Same as op_and: validate types[] against op_tab for xor (especially ensuring the 3rd arg is T_REG) and ensure PC advancement matches the actual encoded argument sizes when the coding byte is invalid.
    decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
    v1 = get_val(vm, proc, types[0], &pos);
    v2 = get_val(vm, proc, types[1], &pos);
    r3 = vm->arena[pos % MEM_SIZE] - 1;
    if (r3 >= 0 && r3 < REG_NUMBER) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +66 to +69
if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER)
return;
if (r3 < 0 || r3 >= REG_NUMBER)
return;
Comment on lines +33 to +38
int addr = 0;
int target = 0;
int val = 0;

for (int i = 0; i < 2; i++)
addr = (addr << 8) | vm->arena[(*pos + i) % MEM_SIZE];
Comment on lines +100 to +104
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
v1 = get_val(vm, proc, types[0], &pos);
v2 = get_val(vm, proc, types[1], &pos);
r3 = vm->arena[pos % MEM_SIZE] - 1;
if (r3 >= 0 && r3 < REG_NUMBER) {
Comment on lines +59 to +63
void op_add(global_t *global, vm_t *vm, process_t *proc)
{
int r1 = vm->arena[(proc->pc + 2) % MEM_SIZE] - 1;
int r2 = vm->arena[(proc->pc + 3) % MEM_SIZE] - 1;
int r3 = vm->arena[(proc->pc + 4) % MEM_SIZE] - 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calculation instructions (add, sub, and, or, xor)

2 participants