10 simple memory instructions ld st lld - #39
Merged
Conversation
…le verification logic
Contributor
There was a problem hiding this comment.
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
lldindirect 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
stdoes not validate that the first operand is a valid source register before computingval. If the coding byte or register number is invalid,get_valcan 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
lldhas the same missing operand validation asld: it accepts whatever types are decoded instead of enforcing direct/indirect plus register fromop_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
sthandler 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
lldhandler is not covered by the instruction test suite. Please add tests that distinguish it fromld, especially indirect addressing withoutIDX_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
stoperand decodes to an unsupported type (for exampleT_DIR), neither branch consumes that operand beforeproc->pcis 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.