Commit 8bfa213a authored by Kai Rese's avatar Kai Rese
Browse files

Fix overwriting and too big offsets

parent 4e770177
......@@ -6,7 +6,7 @@
alias reg0 fnParam0
alias reg0 retVal
# reg1 -> function parameter
alias reg0 fnParam1
alias reg1 fnParam1
alias reg7 testNum
alias reg8 numLeft
......@@ -33,8 +33,8 @@ target listIter :ListStart
:CheckPrimeBegin
sub lenLeft listEnd listIter
bzero lenLeft :CheckPrimeEnd
load fnParam0 listIter
add fnParam1 testNum
load fnParam1 listIter
add fnParam0 testNum zero
target retAddr +3
store retAddr sp
add sp -1
......@@ -45,7 +45,9 @@ add listIter 1
jump :CheckPrimeBegin
:CheckPrimeEnd
store testNum listEnd
add numLeft -1
add listEnd 1
bzero numLeft :FindPrimeEnd
jump :FindPrimeBegin
:FindPrimeEnd
......@@ -58,8 +60,8 @@ stop
# the divisor until it's at the original value again. The remainder is
# our result.
# This does: reg0 = reg0 % reg1
alias reg0 remainder
alias reg1 divisor
alias fnParam0 remainder
alias fnParam1 divisor
alias reg2 dividend
alias reg3 shftCnt
......@@ -70,7 +72,6 @@ add shftCnt zero zero
# loop
:ShiftLeftBegin
shiftl divisor
# use retVal as temp value
sub remainder dividend divisor
bneg remainder :ShiftLeftEnd
add shftCnt 1
......
......@@ -148,7 +148,7 @@ fn assemble_statement(
let destination = get_register(register_symbols, destination)?;
let offset = get_offset(label_symbols, position, offset)?;
assert_in_range(offset, 8, true, true)?;
Ok((OPCODE_LOAD_RELATIVE << 12) | (destination << 8) | offset as u16)
Ok((OPCODE_LOAD_RELATIVE << 12) | (destination << 8) | (offset as u16 & 0x00ff))
}
Statement::LoadBaseOffset {
destination,
......@@ -164,35 +164,38 @@ fn assemble_statement(
let data = get_register(register_symbols, data)?;
let offset = get_offset(label_symbols, position, offset)?;
assert_in_range(offset, 8, true, true)?;
Ok((OPCODE_STORE_RELATIVE << 12) | (data << 8) | offset as u16)
Ok((OPCODE_STORE_RELATIVE << 12) | (data << 8) | (offset as u16 & 0x00ff))
}
Statement::StoreBaseOffset { data, base, offset } => {
let data = get_register(register_symbols, data)?;
let base = get_register(register_symbols, base)?;
assert_in_range(offset, 4, true, true)?;
Ok((OPCODE_STORE_BASE_OFFSET << 12) | (data << 8) | (base << 4) | offset as u16)
Ok((OPCODE_STORE_BASE_OFFSET << 12)
| (data << 8)
| (base << 4)
| (offset as u16 & 0x000f))
}
Statement::JumpRelative(offset) => {
let offset = get_offset(label_symbols, position, offset)?;
assert_in_range(offset, 12, true, true)?;
Ok((OPCODE_JUMP_RELATIVE << 12) | offset as u16)
Ok((OPCODE_JUMP_RELATIVE << 12) | (offset as u16 & 0x0fff))
}
Statement::JumpBaseOffset { base, offset } => {
let base = get_register(register_symbols, base)?;
assert_in_range(offset, 8, true, true)?;
Ok((OPCODE_JUMP_BASE_OFFSET << 12) | (base << 8) | offset as u16)
Ok((OPCODE_JUMP_BASE_OFFSET << 12) | (base << 8) | (offset as u16 & 0x00ff))
}
Statement::BranchEqualZero { test_value, offset } => {
let test_value = get_register(register_symbols, test_value)?;
let offset = get_offset(label_symbols, position, offset)?;
assert_in_range(offset, 8, true, true)?;
Ok((OPCODE_BRANCH_EQUAL_ZERO << 12) | (test_value << 8) | offset as u16)
Ok((OPCODE_BRANCH_EQUAL_ZERO << 12) | (test_value << 8) | (offset as u16 & 0x00ff))
}
Statement::BranchNegative { test_value, offset } => {
let test_value = get_register(register_symbols, test_value)?;
let offset = get_offset(label_symbols, position, offset)?;
assert_in_range(offset, 8, true, true)?;
Ok((OPCODE_BRANCH_NEGATIVE << 12) | (test_value << 8) | offset as u16)
Ok((OPCODE_BRANCH_NEGATIVE << 12) | (test_value << 8) | (offset as u16 & 0x00ff))
}
Statement::AddRegister {
destination,
......@@ -207,7 +210,7 @@ fn assemble_statement(
Statement::AddImmediate { target, addend } => {
let target = get_register(register_symbols, target)?;
assert_in_range(addend, 8, true, false)?;
Ok((OPCODE_ADD_IMMEDIATE << 12) | (target << 8) | addend as u16)
Ok((OPCODE_ADD_IMMEDIATE << 12) | (target << 8) | (addend as u16 & 0x00ff))
}
Statement::Subtract {
destination,
......@@ -226,7 +229,7 @@ fn assemble_statement(
let destination = get_register(register_symbols, destination)?;
let offset = get_offset(label_symbols, position, offset)?;
assert_in_range(offset, 8, true, true)?;
Ok((OPCODE_TARGET << 12) | (destination << 8) | offset as u16)
Ok((OPCODE_TARGET << 12) | (destination << 8) | (offset as u16 & 0x00ff))
}
Statement::ShiftLeft {
destination,
......@@ -302,7 +305,7 @@ fn get_offset(
Err(AssembleErrorKind::UndefinedLabel(target_label))
}
} else {
Ok(position as isize + 1 + offset.offset)
Ok(offset.offset)
}
}
......
......@@ -179,7 +179,7 @@ Currently, the only way to get information out is by reading the register state
alias reg0 fnParam0
alias reg0 retVal
# reg1 -> function parameter
alias reg0 fnParam1
alias reg1 fnParam1
alias reg7 testNum
alias reg8 numLeft
......@@ -206,8 +206,8 @@ target listIter :ListStart
:CheckPrimeBegin
sub lenLeft listEnd listIter
bzero lenLeft :CheckPrimeEnd
load fnParam0 listIter
add fnParam1 testNum
load fnParam1 listIter
add fnParam0 testNum zero
target retAddr +3
store retAddr sp
add sp -1
......@@ -218,7 +218,9 @@ add listIter 1
jump :CheckPrimeBegin
:CheckPrimeEnd
store testNum listEnd
add numLeft -1
add listEnd 1
bzero numLeft :FindPrimeEnd
jump :FindPrimeBegin
:FindPrimeEnd
......@@ -231,8 +233,8 @@ stop
# the divisor until it's at the original value again. The remainder is
# our result.
# This does: reg0 = reg0 % reg1
alias reg0 remainder
alias reg1 divisor
alias fnParam0 remainder
alias fnParam1 divisor
alias reg2 dividend
alias reg3 shftCnt
......@@ -243,7 +245,6 @@ add shftCnt zero zero
# loop
:ShiftLeftBegin
shiftl divisor
# use retVal as temp value
sub remainder dividend divisor
bneg remainder :ShiftLeftEnd
add shftCnt 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment