0

I'm trying to implement the LC-3 architecture in c++. I found a tutorial in C.

I have all the code I need. I built the source code ran the executable give it an obj file who contain the software to run but I'm stuck in the first instruction of the obj file. This is the code of the first instruction execution:

        case Registry::OP_LD:
    {
        uint16_t r0 = (instr >> 9) & 0x7;
        uint16_t pc_offset = m_registry->sign_extend(instr & 0x1FF, 9);

        printf("m_memory: %p\n", m_memory);
        printf("m_registry: %p\n", m_registry);
        printf("\n\n\n\n\n\n");
        printf("m_memory[0]: %d\n", m_memory->read(0));
        printf("m_registry[0]: %d\n", m_registry->get(0));
        printf("\n\n\n\n\n\n");
        printf("R0: %d\n", r0);
        printf("pc_offset: %d\n", pc_offset);
        printf("Reg 0: %d\n", m_registry->get(r0));
        printf("mem read: %d\n", m_memory->read(m_registry->get(static_cast<uint16_t>(Registry::R_PC)) + pc_offset));
        m_registry->set(r0, m_memory->read(m_registry->get(static_cast<uint16_t>(Registry::R_PC)) + pc_offset));
        printf("Reg 0 bis: %d\n", m_registry->get(r0));
        m_registry->update_flags(r0);
    }

I tried to debug this by putting printf to saw what's happened. I save the result in test (check the archive). I extract a result here as an example:

m_memory: 0x7f5c20645010

m_registry: 0x558784821ef0

m_memory[0]: 0

m_registry[0]: 0

R0: 6

pc_offset: 23

Reg 0: 61477

mem read: 61477

Reg 0 bis: 61477

it's seems, for an unknown reason, the reg and/or the memory it's not set correctly. I don't really know how to debug this. If someone can help me to debug this it's would be great.

The source code is here archive

Cheers

  • why do you not know how to debug it? Did you try to use a debugger? – 463035818_is_not_an_ai Feb 09 '23 at 09:39
  • I tried but I'm not good with it. – Rémi boivin Feb 09 '23 at 10:05
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – 463035818_is_not_an_ai Feb 09 '23 at 10:06
  • I found the issue it's was because I forgotten to set the registry. Now some of the instructions works but some other not like the BR. I don't know yet why but I'm stuck in an infinit loop. the value of pc registry was blocked in a endless loop [the result](https://www.swisstransfer.com/d/dbd2ac4d-8895-42d9-82f5-b3cf459e6e11) – Rémi boivin Feb 09 '23 at 16:43

0 Answers0