Skip to content
Permalink
Browse files

bsf/bsr now working

Signed-off-by: VoR0220 <catalanor0220@gmail.com>
  • Loading branch information
VoR0220 committed Nov 12, 2019
1 parent b13126e commit 2f8edd1986fe5ae982a6f3a36c1da30ca2814ad5
Showing with 50 additions and 62 deletions.
  1. +23 −19 src/ops.rs
  2. +27 −43 tests/simple_tests.rs
@@ -74,49 +74,53 @@ pub fn pop(vm: &mut VM, pipeline: &Pipeline, _hv: &mut dyn Hypervisor) -> Result

pub fn bit_scan_forward(vm: &mut VM, pipeline: &Pipeline, _hv: &mut dyn Hypervisor) -> Result<(), VMError> {
if pipeline.size_override {
let source = vm.get_arg(pipeline.args[1].location)?.u16_exact()?;
let binary_string = format!("{:b}", source);
let index = binary_string.find('1');
if index == None {
let source = vm.get_arg(pipeline.args[0].location)?.u16_exact()?;
let binary_string = format!("{:04b}", source);
let wrapped_index = binary_string.rfind('1');
if wrapped_index == None {
vm.flags.zero = true;
} else {
vm.flags.zero = false;
vm.set_arg(pipeline.args[0].location, SizedValue::Word(index.unwrap() as u16));
let index = (binary_string.len() as u16 - 1) - (wrapped_index.unwrap() as u16);
vm.set_arg(pipeline.args[1].location, SizedValue::Word(index));
}
} else {
let source = vm.get_arg(pipeline.args[1].location)?.u32_exact()?;
let binary_string = format!("{:b}", source);
let index = binary_string.find('1');
if index == None {
let source = vm.get_arg(pipeline.args[0].location)?.u32_exact()?;
let binary_string = format!("{:08b}", source);
let wrapped_index = binary_string.rfind('1');
if wrapped_index == None {
vm.flags.zero = true;
} else {
vm.flags.zero = false;
vm.set_arg(pipeline.args[0].location, SizedValue::Dword(index.unwrap() as u32));
let index = (binary_string.len() as u32 - 1) - (wrapped_index.unwrap() as u32);
vm.set_arg(pipeline.args[1].location, SizedValue::Dword(index));
}
}
Ok(())
}

pub fn bit_scan_reverse(vm: &mut VM, pipeline: &Pipeline, _hv: &mut dyn Hypervisor) -> Result<(), VMError> {
if pipeline.size_override {
let source = vm.get_arg(pipeline.args[1].location)?.u16_exact()?;
let binary_string = format!("{:b}", source);
let index = binary_string.rfind('1');
if index == None {
let source = vm.get_arg(pipeline.args[0].location)?.u16_exact()?;
let binary_string = format!("{:04b}", source);
let wrapped_index = binary_string.find('1');
if wrapped_index == None {
vm.flags.zero = true;
} else {
vm.flags.zero = false;
vm.set_arg(pipeline.args[0].location, SizedValue::Word(index.unwrap() as u16));
let index = (binary_string.len() as u16 - 1) - (wrapped_index.unwrap() as u16);
vm.set_arg(pipeline.args[1].location, SizedValue::Word(index));
}
} else {
let source = vm.get_arg(pipeline.args[1].location)?.u32_exact()?;
let source = vm.get_arg(pipeline.args[0].location)?.u32_exact()?;
let binary_string = format!("{:b}", source);
let index = binary_string.rfind('1');
if index == None {
let wrapped_index = binary_string.find('1');
if wrapped_index == None {
vm.flags.zero = true;
} else {
vm.flags.zero = false;
vm.set_arg(pipeline.args[0].location, SizedValue::Dword(index.unwrap() as u32));
let index = (binary_string.len() as u32 - 1) - (wrapped_index.unwrap() as u32);
vm.set_arg(pipeline.args[1].location, SizedValue::Dword(index));
}
}
Ok(())
@@ -1490,59 +1490,43 @@ fn test_bsf_bsr_1() {
assert_eq!(vm.flags, X86Flags{..Default::default()});
}

/*#[test]
fn test_bsf_bsr_2() {
#[test]
fn test_bsf_zero() {
let vm = execute_vm_with_asm("
mov ebp, 0x8000000A
mov esp, 0x80006650
enter 1, 0
mov eax, [esp + 1]
mov ebx, esp
mov ecx, ebp
leave
mov eax, 0
mov ebx, 1
bsf ebx, eax
hlt");
assert_eq!(vm.reg32(Reg32::EAX), 0x8000000A);
assert_eq!(vm.reg32(Reg32::EBX), 0x8000664b);
assert_eq!(vm.reg32(Reg32::ECX), 0x8000664C);
assert_eq!(vm.reg32(Reg32::ESP), 0x80006650);
assert_eq!(vm.reg32(Reg32::EBP), 0x8000000A);
assert_eq!(vm.flags, X86Flags{..Default::default()});
assert_eq!(vm.reg32(Reg32::EAX), 0);
assert_eq!(vm.reg32(Reg32::EBX), 1);
assert_eq!(vm.flags, X86Flags{zero: true, ..Default::default()});
}

#[test]
fn test_bsr_1() {
fn test_bsr_zero() {
let vm = execute_vm_with_asm("
mov ebp, 0x8000000A
mov esp, 0x80006650
enter 1, 0
mov eax, [esp + 1]
mov ebx, esp
mov ecx, ebp
leave
mov eax, 0
mov ebx, 1
bsr ebx, eax
hlt");
assert_eq!(vm.reg32(Reg32::EAX), 0x8000000A);
assert_eq!(vm.reg32(Reg32::EBX), 0x8000664b);
assert_eq!(vm.reg32(Reg32::ECX), 0x8000664C);
assert_eq!(vm.reg32(Reg32::ESP), 0x80006650);
assert_eq!(vm.reg32(Reg32::EBP), 0x8000000A);
assert_eq!(vm.flags, X86Flags{..Default::default()});
assert_eq!(vm.reg32(Reg32::EAX), 0);
assert_eq!(vm.reg32(Reg32::EBX), 1);
assert_eq!(vm.flags, X86Flags{zero: true, ..Default::default()});
}

#[test]
fn test_bsr_2() {
fn test_bsr_bsf_2() {
let vm = execute_vm_with_asm("
mov ebp, 0x8000000A
mov esp, 0x80006650
enter 1, 0
mov eax, [esp + 1]
mov ebx, esp
mov ecx, ebp
leave
mov edx, 100000000000110b
bsf eax, edx
bsr ecx, edx
mov edx, 111111111100000000000110b
bsf ebx, edx
bsr esi, edx
hlt");
assert_eq!(vm.reg32(Reg32::EAX), 0x8000000A);
assert_eq!(vm.reg32(Reg32::EBX), 0x8000664b);
assert_eq!(vm.reg32(Reg32::ECX), 0x8000664C);
assert_eq!(vm.reg32(Reg32::ESP), 0x80006650);
assert_eq!(vm.reg32(Reg32::EBP), 0x8000000A);
assert_eq!(vm.reg32(Reg32::EAX), 1);
assert_eq!(vm.reg32(Reg32::EBX), 1);
assert_eq!(vm.reg32(Reg32::ECX), 0xE);
assert_eq!(vm.reg32(Reg32::ESI), 0x17);
assert_eq!(vm.flags, X86Flags{..Default::default()});
}*/
}

0 comments on commit 2f8edd1

Please sign in to comment.
You can’t perform that action at this time.