Expand description
Module instruction
This module contains functions to execute various instructions. These instructions include arithmetic operations, logical operations, branching, jumping, loading, storing, and system calls.
§Usage
The module provides an execute
function that executes the corresponding operation based on the given raw instruction, register file, and memory state.
use lc3_zkvm::memory::Memory;
use lc3_zkvm::register::RegisterFile;
use lc3_zkvm::instruction::execute;
let raw_instruction: u16 = 0x1234;
let mut registers = RegisterFile::new();
let mut memory = Memory::new();
match execute(raw_instruction, &mut registers, &mut memory) {
Ok(_) => println!("Instruction executed successfully"),
Err(e) => println!("Instruction execution failed: {}", e),
}
§Instruction List
OP_ADD
: Addition operationOP_AND
: Bitwise AND operationOP_NOT
: Bitwise NOT operationOP_BR
: Conditional branchOP_JMP
: Unconditional jumpOP_JSR
: Jump to subroutineOP_LD
: Load dataOP_LDI
: Indirect load dataOP_LDR
: Load data from registerOP_LEA
: Load effective addressOP_ST
: Store dataOP_STI
: Indirect store dataOP_STR
: Store data from registerOP_TRAP
: System call
§Error Handling
If the instruction is unrecognized or not implemented, the execute
function will return the corresponding error message.
§Helper Functions
sign_extend
: Sign-extend a valueRegister::from
: Convertu16
toRegister
enum
§Example
use lc3_zkvm::memory::Memory;
use lc3_zkvm::register::RegisterFile;
use lc3_zkvm::instruction::execute;
let raw_instruction: u16 = 0x1234;
let mut registers = RegisterFile::new();
let mut memory = Memory::new();
match execute(raw_instruction, &mut registers, &mut memory) {
Ok(_) => println!("Instruction executed successfully"),
Err(e) => println!("Instruction execution failed: {}", e),
}