Skip to content

10 simple memory instructions ld st lld - #39

Merged
mathish06 merged 3 commits into
mainfrom
10-simple-memory-instructions-ld-st-lld
May 15, 2026
Merged

10 simple memory instructions ld st lld#39
mathish06 merged 3 commits into
mainfrom
10-simple-memory-instructions-ld-st-lld

Conversation

@mathish06

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings May 15, 2026 17:18
@mathish06 mathish06 linked an issue May 15, 2026 that may be closed by this pull request
@mathish06
mathish06 merged commit a64a31c 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

Implements Corewar memory instructions (ld, st, lld) and wires them into the VM execution flow.

Changes:

  • Added memory instruction handlers and included them in the build.
  • Updated instruction dispatch to execute opcodes 2, 3, and 13.
  • Adjusted shared operand-reading logic for lld indirect addressing.

Reviewed changes

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

Show a summary per file
File Description
src/instructions/instructions_mem.c Adds ld, st, and lld implementations.
src/instructions/instructions_calc.c Exposes get_val and changes indirect addressing behavior for lld.
src/instructions/basic_instructions.c Moves/adds op_add implementation.
src/core/execute_processes.c Registers memory instructions in the opcode dispatch table.
src/core/game_loop.c Changes cycle verification from equality to greater-than-or-equal.
Makefile Adds the new memory instruction source file to the build.
include/corewar.h Declares new memory instruction handlers and get_val.
Comments suppressed due to low confidence (5)

src/instructions/instructions_mem.c:53

  • st does not validate that the first operand is a valid source register before computing val. If the coding byte or register number is invalid, get_val can return 0 or read another operand type and the handler may still overwrite the destination register/memory.
    decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
    val = get_val(vm, proc, types[0], &pos);
    if (types[1] == T_REG) {

src/instructions/instructions_mem.c:74

  • lld has the same missing operand validation as ld: it accepts whatever types are decoded instead of enforcing direct/indirect plus register from op_tab. Malformed coding bytes can therefore update registers with values from invalid operand layouts.
    decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
    val = get_val(vm, proc, types[0], &pos);
    reg = vm->arena[pos % MEM_SIZE] - 1;

src/instructions/instructions_mem.c:43

  • This new st handler is not covered by the existing instruction tests. Please add tests for register destinations, indirect memory writes, invalid source/destination registers, and address wraparound to validate the new store behavior.
void op_st(global_t *global, vm_t *vm, process_t *proc)

src/instructions/instructions_mem.c:64

  • This new lld handler is not covered by the instruction test suite. Please add tests that distinguish it from ld, especially indirect addressing without IDX_MOD, carry updates, invalid registers, and memory wraparound.
void op_lld(global_t *global, vm_t *vm, process_t *proc)

src/instructions/instructions_mem.c:57

  • When the second st operand decodes to an unsupported type (for example T_DIR), neither branch consumes that operand before proc->pc is updated. The process will resume in the middle of the malformed instruction instead of advancing past the bytes described by the coding byte.
    if (types[1] == T_REG) {
        reg = vm->arena[pos % MEM_SIZE] - 1;
        if (reg >= 0 && reg < REG_NUMBER)
            proc->registers[reg] = val;
        pos += 1;

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

Comment on lines +76 to +79
if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER)
return;
if (r3 < 0 || r3 >= REG_NUMBER)
return;
Comment on lines +18 to +20
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
val = get_val(vm, proc, types[0], &pos);
reg = vm->arena[pos % MEM_SIZE] - 1;

#include "../../include/corewar.h"

void op_ld(global_t *global, vm_t *vm, process_t *proc)
Comment on lines +39 to +42
if (proc->current_opcode == 13)
target = (proc->pc + addr) % MEM_SIZE;
else
target = (proc->pc + (addr % IDX_MOD)) % MEM_SIZE;
curr->pc = (curr->pc + 3) % MEM_SIZE;
}

void op_add(global_t *global, vm_t *vm, process_t *proc)
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.

Simple memory instructions (ld, st, lld)

2 participants