[30pts]求助,使用8086无法通过测试点

P1001 A+B Problem

chenjj100419 @ 2023-08-21 14:39:08

// CPUSimulate.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
namespace m8086_type {

    //Base

    template <class T>
    class Node;

    template <class T>
    class List;

    template <class T>
    class Stack;

    template <class T>
    class Heap;

    //Base+
    class Register;
}

template <class T>
class m8086_type::Node {
    friend class List<T>;
private:
    T data;
    Node<T>* next;
    Node<T>* prev;
public:
    Node<T>() {
        next = nullptr;
        prev = nullptr;
    }
};

template <class T>
class m8086_type::List {
private:
    Node<T>* HEAD;
    Node<T>* head;
    Node<T>* head2;
    Node<T>* head3;
    Node<T>* head4;
    Node<T>* head5;
    int len1, len2, lentemp;
public:
    List() {
        HEAD = new Node<T>();
        head = HEAD;
        head2 = HEAD;
        head3 = HEAD;
        head4 = HEAD;
        head5 = HEAD;
        len1 = 0;
        len2 = 0;
        lentemp = 0;
    }
    void addFront(T data) {
        Node<T>* pnew = new Node<T>();
        if (pnew == nullptr) return;
        pnew->data = data;
        pnew->next = head;
        pnew->prev = head->prev;
        head->prev = pnew;
        head = head->prev;
        len1++;
        return;
    }
    void addAfter(T data) {
        Node<T>* pnew = new Node<T>();
        if (pnew == nullptr) return;
        pnew->data = data;
        pnew->prev = head2;
        pnew->next = head2->next;
        head2->next = pnew;
        head2 = head2->next;
        lentemp++;
        len2 = len1 + lentemp;
        return;
    }
    void insert(int x, T data) {
        if (x <= len1) {
            head3 = head3->prev;
            Node<T>* pnew = new Node<T>();
            pnew->data = data;
            for (int i = 1; i <= x; i++) {
                head3 = head3->prev;
            }
            pnew->next = head3;
            pnew->prev = head3->prev;
            head3->prev = pnew;
            len1++;
            return;
        }
        if (x > len2) return;
        head3 = head->next;
        Node<T>* pnew = new Node<T>();
        pnew->data = data;
        for (int i = len2 - len1 - 1; i <= x; i++) {
            head3 = head3->next;
        }
        pnew->prev = head3;
        pnew->next = head3->next;
        head3->next = pnew;
        lentemp++;
        len2 = len1 + lentemp;
        return;
    }
    int length() {
        return len2;
    }
    void erase(int x) {
        if (x <= len1) {
            head3 = head3->prev;
            for (int i = 1; i <= x; i++) {
                head3 = head3->prev;
            }
            Node<T>* ptemp = new Node<T>();
            ptemp = head3;
            head3 = head3->next;
            head3->prev = ptemp->prev;
            head3 = head3->prev;
            head3->next = ptemp->next;
            delete ptemp;
            len1--;
            return;
        }
        if (x > len2) return;
        head3 = head3->next;
        for (int i = len2 - len1 - 1; i <= x; i++) {
            head3 = head3->next;
        }
        Node<T>* ptemp = new Node<T>();
        ptemp = head3;
        head3 = head3->next;
        head3->prev = ptemp->prev;
        head3 = head3->prev;
        head3->neext = ptemp->next;
        delete ptemp;
        lentemp--;
        len2 = len1 + lentemp;
        return;
    }
    void deleteFront() {
        erase(1);
        return;
    }
    void deleteAfter() {
        erase(len2);
        return;
    }
    void remove(T data) {
        head4 = head4->prev;
        for (int i = 1; i <= len1; i++) {
            if (head4->data == data) {
                erase(i);
                return;
            }
            head4 = head4->prev;
        }
        head4 = HEAD;
        head4 = head->next;
        for (int i = len2 - len1 - 1; i <= len2; i++) {
            if (head4->data == data) {
                erase(len1 + i);
                return;
            }
            head4 = head4->next;
        }
        return;
    }
    void modify(int x, T data) {
        erase(x);
        insert(x - 1, data);
        return;
    }
    void modifyFront(T data) {
        modify(1, data);
        return;
    }
    void modifyAfter(T data) {
        modify(len2, data);
        return;
    }
    T query(int x) {
        if (x <= len1) {
            if (head5->prev == nullptr) return NULL;
            head5 = head5->prev;
            for (int i = 1; i <= x; i++) {
                if (head5->prev == nullptr) return NULL;
                head5 = head5->prev;
            }
            return head5->data;
        }
        if (x > len2) return NULL;
        if (head5->next == nullptr) return NULL;
        head5 = head5->next;
        for (int i = len2 - len1 - 1; i <= x; i++) {
            if (head5->next == nullptr) return NULL;
            head5 = head5->next;
        }
        return head5->data;
    }
    T front() {
        return query(1);
    }
    T after() {
        return query(len2);
    }
    bool empty() {
        return len2 == 0;
    }
};

template <class T>
class m8086_type::Stack {
private:
    List<T>* list;
public:
    Stack() {
        list = new List<T>();
    }
    ~Stack() {
        delete list;
    }
    T top() {
        return list->after();
    }
    void push(T data) {
        list->addAfter(data);
        return;
    }
    void pop() {
        list->deleteAfter();
        return;
    }
    int size() {
        return list->length();
    }
    bool empty() {
        return list->length() == 0;
    }

};

class m8086_type::Register {
private:
    unsigned char data;
public:
    Register() {

    }
    ~Register() {

    }

    Register operator+(const Register& r) {
        Register reg;
        reg.data = this->data + r.data;
        return reg;
    }
    Register operator-(const Register& r) {
        Register reg;
        reg.data = this->data - r.data;
        return reg;
    }
    Register operator+(const unsigned char& r) {
        Register reg;
        reg.data = this->data + r;
        return reg;
    }
    Register operator-(const unsigned char& r) {
        Register reg;
        reg.data = this->data - r;
        return reg;
    }
    Register operator-() {
        Register reg;
        reg.data = -this->data;
        return reg;
    }
    Register operator!() {
        Register reg;
        reg.data = !this->data;
        return reg;
    }
    Register operator++() {
        Register reg;
        reg.data = this->data + 1;
        return reg;
    }
    Register operator--() {
        Register reg;
        reg.data = this->data - 1;
        return reg;
    }
    Register operator*(const Register& r) {
        Register reg;
        reg.data = this->data * r.data;
        return reg;
    }
    Register operator%(const Register& r) {
        Register reg;
        reg.data = this->data % r.data;
        return reg;
    }
    Register operator/(const Register& r) {
        Register reg;
        reg.data = this->data / r.data;
        return reg;
    }
    Register operator*(const unsigned char& r) {
        Register reg;
        reg.data = this->data * r;
        return reg;
    }
    Register operator%(const unsigned char& r) {
        Register reg;
        reg.data = this->data % r;
        return reg;
    }
    Register operator/(const unsigned char& r) {
        Register reg;
        reg.data = this->data / r;
        return reg;
    }
    bool operator<(const Register& r) {
        return (this->data < r.data);
    }
    bool operator>(const Register& r) {
        return (this->data > r.data);
    }
    bool operator<=(const Register& r) {
        return (this->data <= r.data);
    }
    bool operator>=(const Register& r) {
        return (this->data >= r.data);
    }
    bool operator==(const Register& r) {
        return (this->data == r.data);
    }
    bool operator<(const unsigned char& r) {
        return (this->data < r);
    }
    bool operator>(const unsigned char& r) {
        return (this->data > r);
    }
    bool operator<=(const unsigned char& r) {
        return (this->data <= r);
    }
    bool operator>=(const unsigned char& r) {
        return (this->data >= r);
    }
    bool operator==(const unsigned char& r) {
        return (this->data == r);
    }
    void operator=(const unsigned char& data) {
        this->data = data;
    }
    void operator=(const Register& r) {
        this->data = r.data;
    }

    unsigned char getData() {
        return this->data;
    }
};

namespace m8086 {
    using SByte = char;
    using Byte = unsigned char;
    using Word = unsigned short;

    using u32 = unsigned int;
    using s32 = signed int;

    struct Mem;
    struct CPU;
    struct StatusFlags;

    m8086_type::Stack<Byte> *Stack;
}

struct m8086::Mem {
    static constexpr u32 MAX_MEM = 1024;
    Byte Data[MAX_MEM];

    void Init()
    {
        for (u32 i = 0; i < MAX_MEM; i++)
        {
            Data[i] = 0;
        }
    }

    Byte operator[](u32 Address) const
    {
        // assert here Address is < MAX_MEM
        return Data[Address];
    }

    Byte& operator[](u32 Address)
    {
        // assert here Address is < MAX_MEM
        return Data[Address];
    }
};

struct m8086::StatusFlags {
    // https://blog.csdn.net/xiong_xin/article/details/101796316
    Byte C = 1; // Carry Flag 
    Byte P = 2; // Parity Flag
    Byte AC = 3; // Auxiliary Carry Flag
    Byte Z = 4; // Zero Flag
    Byte S = 5; // Sign Flag
    Byte I = 6; // Interrupt Flag
    Byte D = 7; // Direction Flag
    Byte O = 8; // Overflow Flag 
    Byte B = 9; // Break
};

struct m8086::CPU {
    Word PC; // program counter
    Mem Memory;
    m8086_type::Register *AX, *BX, *CX, *DX;
    m8086_type::Register *SP, *BP;
    m8086_type::Register *SI, *DI;
    m8086_type::Register *IP, *FLAG;
    m8086_type::Register *CS, *DS, *SS, *ES;

    union {
        StatusFlags Flags;
    };

    void SetRegister() {
        Stack->push(0);
        *SP = Stack->top();
    }

    void SyncRegister() {
        if (!(*SP == Stack->top())) {
            Stack->push(SP->getData());
        }
    }

    void Reset() {
        // PC = 
        delete Stack, AX, BX, CX, DX, SP, BP, SI, DI, IP, FLAG, CS, DS, SS, ES;
        Init();
    }

    void Init() {
        Memory.Init();
        Stack = new m8086_type::Stack<Byte>();
        PC = 0;
        AX = new m8086_type::Register();
        BX = new m8086_type::Register();
        CX = new m8086_type::Register();
        DX = new m8086_type::Register();
        SP = new m8086_type::Register();
        BP = new m8086_type::Register();
        SI = new m8086_type::Register();
        DI = new m8086_type::Register();
        IP = new m8086_type::Register();
        FLAG = new m8086_type::Register();
        CS = new m8086_type::Register();
        DS = new m8086_type::Register();
        SS = new m8086_type::Register();
        ES = new m8086_type::Register();
        SetRegister();
    }

    void Loop() {
        SyncRegister();
    }

    Byte FetchByte(s32& Cycles)
    {
        Byte Data = Memory[PC];
        PC++;
        Cycles--;
        return Data;
    }

    SByte FetchSByte(s32& Cycles)
    {
        return FetchByte(Cycles);
    }

    Word FetchWord(s32& Cycles)
    {
        // 6502 is little endian
        Word Data = Memory[PC];
        PC++;

        Data |= (Memory[PC] << 8);
        PC++;

        Cycles -= 2;
        return Data;
    }

    Byte ReadByte(
        s32& Cycles,
        Word Address)
    {
        Byte Data = Memory[Address];
        Cycles--;
        return Data;
    }

    Word ReadWord(
        s32& Cycles,
        Word Address)
    {
        Byte LoByte = ReadByte(Cycles, Address);
        Byte HiByte = ReadByte(Cycles, Address + 1);
        return LoByte | (HiByte << 8);
    }

    /** write 1 byte to memory */
    void WriteByte(Byte Value, s32& Cycles, Word Address)
    {
        Memory[Address] = Value;
        Cycles--;
    }

    /** write 2 bytes to memory */
    void WriteWord(Word Value, s32& Cycles, Word Address)
    {
        Memory[Address] = Value & 0xFF;
        Memory[Address + 1] = (Value >> 8);
        Cycles -= 2;
    }

    void Exec(Byte Code) {
        if (Code == 0x00000001) {
            *CX = *AX + *BX;
            SyncRegister();
        }
    }

    CPU(){}
};

int main() {
    m8086::CPU simulator;
    simulator.Init();
    int a, b;
    std::cin >> a >> b;
    int c = 100;
    *simulator.AX = a;
    *simulator.BX = b;
    simulator.Exec(0x0000001);
    std::cout << (int)simulator.CX->getData();
    simulator.Reset();
}

by _Haoomff_ @ 2023-08-21 14:40:25

我虽然不会调但我大为震撼……


by juruo_zxt @ 2023-08-21 14:43:49

敌方使用钓鱼岛,必须给予打击


by dxrS @ 2023-08-21 14:53:52

@chenjj100419 你说得对,但是长代码放 1 楼并且不给注释是什么意思?


by zhushixuan @ 2023-08-21 16:52:46

直接这样不行吗?

#include<bits/stdc++.h>
using namespace std;

int main(){
    int a,b;
    cin >> a >> b;
    cout << a+b; 
    return 0;
}

by SZ2528 @ 2023-08-21 23:57:35

你这是一种很新的代码,我看不懂但我大受震撼 看看我的代码,希望能给你亿点帮助

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// Copyright (c) 2023 SZ2528

#include<iostream>
#define ? int
#define ? cin
#define ? if
#define ? return
#define ? cout
#define ? endl
using namespace std;
? add_what(? a, ? b) {
    ? (!a) ? b;
    ? add_what((a & b) << 1, a ^ b);
}
? main() {
    ? a, b;
    ? >> a >> b;
    ? << add_what(a, b) << ?;
    ? 0;
}
// 你的荔枝呢

提交记录


by SZ2528 @ 2023-08-21 23:58:44

不知道为什么乱码了,看推荐记录吧

(编译需要C++20)


by chasp @ 2023-09-04 18:26:39

《用c++模拟汇编》


by Earth_Sky @ 2023-09-09 10:25:02

啊这……


|