Exemple #1
0
 def test_gt(self):
     vmachine.re_init()
     test_code = self.read_test_code("tests/gt.asm")
     assemble(test_code, vmachine)
     self.assertEqual(vmachine.registers["EAX"], 17)
     self.assertEqual(vmachine.registers["EBX"], 16)
     self.assertEqual(vmachine.registers["ECX"], 16)
     self.assertEqual(vmachine.registers["EDX"], 19)
Exemple #2
0
 def test_arithmetic_shift(self):
     vmachine.re_init()
     test_code = self.read_test_code("tests/arithmetic_shift.asm")
     assemble(test_code, vmachine)
     self.assertEqual(vmachine.registers["EAX"], 4)
     self.assertEqual(vmachine.registers["EBX"], 10)
     self.assertEqual(vmachine.registers["ECX"], 8)
     self.assertEqual(vmachine.registers["EDX"], 8)
     self.assertEqual(vmachine.memory["4"], 4)
Exemple #3
0
 def test_key(self):
     vmachine.re_init()
     test_code = self.read_test_code("tests/key_test.asm")
     assemble(test_code, vmachine)
     self.assertEqual(vmachine.registers["EAX"], 71)
     self.assertEqual(vmachine.registers["EBX"], 71)
     self.assertEqual(vmachine.registers["ECX"], 1)
     self.assertEqual(vmachine.registers["ESP"], 63)
     self.assertEqual(vmachine.memory["9"], 83)
Exemple #4
0
 def test_interrupt(self):
     vmachine.re_init()
     test_code = self.read_test_code("tests/test_interrupt.asm")
     assemble(test_code, vmachine)
     self.assertEqual(vmachine.registers["EAX"], 71)
     self.assertEqual(vmachine.registers["EBX"], 6)
     self.assertEqual(vmachine.registers["ECX"], 1)
     self.assertEqual(vmachine.registers["ESP"], 63)
     self.assertEqual(vmachine.memory["6"], 71)
 def test_jmp(self):
     """
     Jump to a random location from 0 to MAX_INSTRUCTIONS.
     Assert IP is set to that location by jump.
     """
     for i in range(NUM_TESTS):
         vmachine.re_init()
         label_loc = random.randint(FIRST_INST_ADDRESS,MAX_INSTRUCTIONS)
         vmachine.labels["test_label"] = label_loc
         assemble("jmp test_label", vmachine)
         self.assertEqual(vmachine.get_ip(), label_loc)
 def test_jge(self):
     """
     Jump iff sign flag is 0.
     """
     for i in range(NUM_TESTS):
       vmachine.re_init()
       label_loc = random.randint(FIRST_INST_ADDRESS,MAX_INSTRUCTIONS)
       vmachine.labels["test_label"] = label_loc
       sign_flag = random.getrandbits(1)
       vmachine.flags["SF"] = sign_flag
       assemble("jge test_label", vmachine)
       if(not sign_flag):
         self.assertEqual(vmachine.get_ip(), label_loc)
       else:
         self.assertEqual(vmachine.get_ip(), 1)
Exemple #7
0
def main_page(request):
    global vmachine
    last_instr = ""
    error = ""

    site_hdr = get_hdr()

    if request.method == 'GET':
        vmachine.re_init()
        form = MainForm()
    else:
        form = MainForm(request.POST)
        if CLEAR in request.POST:
            vmachine.re_init()
        else:
            step = (STEP in request.POST)
            vmachine.nxt_key = 0
            if step:
                add_debug("Getting next key", vmachine)
                try:
                    vmachine.nxt_key = int(request.POST.get(NXT_KEY, 0))
                except Exception:
                    vmachine.nxt_key = 0

            get_reg_contents(vmachine.registers, request)
            get_mem_contents(vmachine.memory, request)
            get_stack_contents(vmachine.stack, request)
            get_flag_contents(vmachine.flags, request)
            (last_instr, error) = assemble(request.POST[CODE], vmachine, step)

    return render(request, 'main.html',
                  {'form': form,
                   HEADER: site_hdr,
                   'last_instr': last_instr,
                   'error': error,
                   'unwritable': vmachine.unwritable,
                   'debug': vmachine.debug,
                   NXT_KEY: vmachine.nxt_key,
                   'registers': vmachine.registers,
                   'memory': vmachine.memory, 
                   'stack': vmachine.stack, 
                   'flags': vmachine.flags,
                  })
    def test_call(self):
        """
        Tests call by both checking it jumped correctly and pushed correctly. 
        """
        for i in range(NUM_TESTS):
            vmachine.re_init()
            call_instr_addr = random.randint(FIRST_INST_ADDRESS, MAX_INSTRUCTIONS)
            label_loc = random.randint(FIRST_INST_ADDRESS, MAX_INSTRUCTIONS)

            # At the time of writing this test, blank lines are skipped by the tokenizer.
            # In order to have emu jump to the location of label_loc, we have to make
            # no-op lines to assign the correct locations to the lines we test.
            instructions = [NO_OP] * (MAX_INSTRUCTIONS+1)

            instructions[call_instr_addr] = "call " + TEST_LABEL + "\n"
            instructions[label_loc] = TEST_LABEL + ": " + instructions[label_loc]

            vmachine.labels[TEST_LABEL] = label_loc
            vmachine.set_ip(call_instr_addr)

            assemble("".join(instructions), vmachine, True)

            self.assertEqual(vmachine.get_ip(), label_loc)
            self.assertEqual(vmachine.stack[str(STACK_TOP)], call_instr_addr+1)
Exemple #9
0
 def test_power(self):
     vmachine.re_init()
     test_code = self.read_test_code("tests/power.asm")
     assemble(test_code, vmachine)
     self.assertEqual(vmachine.registers["EDX"], 65536)
Exemple #10
0
 def test_loop(self):
     vmachine.re_init()
     test_code = self.read_test_code("tests/loop.asm")
     assemble(test_code, vmachine)
     self.assertEqual(vmachine.registers["ECX"], 16)