// File: /workspace/github_cpp_makefile_star2_O2/saumya-prakash[P]AssemblerEmulator/Emulator.cpp
// Function: Emulator::loader()
bool Emulator::loader()     // no size checking has been done now -> just simple loading
{    
    ifstream fi(obj_filename, ifstream::binary | ifstream::in);

    if(!fi) // ERROR in opening file
    {
        return false;
    }

    char waste;

    char aux[8] = "\0";
    for(unsigned i=0; i<8; ++i)
    {
        fi.read(aux+i, 1);
    }

    for(unsigned i=0; i<8; ++i)
    {
        if(aux[i]!=format_code[i])
        {
            cout<<i<<" ";
            cout<<"Unknown object file format"<<endl;
            return false;
        }
    }

    fi.read(&waste, 1);
    if(waste!='\n')
    {
        cout<<"Unknown onject file format"<<endl;
        return false; 
    }


    text_size = get_int(fi);

    fi.read(&waste, 1);     // dump newline character

    for(unsigned i=0; i<text_size; ++i)     // load text segment
    {
        mempory_space[i] = get_int(fi);
    }

    fi.read(&waste, 1);     // dump newline character
    fi.read(&waste, 1);     // dump newline character
    
    data_size = get_int(fi);

    fi.read(&waste, 1);     // dump newline character

    for(unsigned i=0; i<data_size; ++i)
    {
        mempory_space[i+data_lower] = get_int(fi);
    }


    PC = 0x00000000;     // set PC to 0x0
    A = 0x00000000;
    B = 0x00000000;
    data_upper = data_lower + data_size - 1;    // denotes upper boundary of data loaded
    SP = 0xff00;    // set SP to ?? NOT CONFIRMED


    fi.close();     // close the object file

    return true;    // loading successful
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rdi, %rbx
leaq 0x10(%rsp), %r14
pushq $0xc
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2280
movq (%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x30(%rsp,%rax)
jne 0x3740
andq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
pushq $0x1
popq %r15
cmpq $0x8, %r12
je 0x366e
leaq (%rsp,%r12), %rsi
addq $0x8, %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x21e0
incq %r12
jmp 0x3650
xorl %eax, %eax
leaq 0x2c85(%rip), %rcx      # 0x62fc
cmpq $0x8, %rax
je 0x36c8
movb 0x8(%rsp,%rax), %dl
leaq 0x1(%rax), %rsi
cmpb (%rax,%rcx), %dl
movq %rsi, %rax
je 0x3677
decq %rsi
movq 0x5939(%rip), %rdi      # 0x8fd0
callq 0x2110
leaq 0x2e7e(%rip), %rsi      # 0x6521
movq %rax, %rdi
callq 0x2180
movq 0x591e(%rip), %rdi      # 0x8fd0
leaq 0x2da1(%rip), %rsi      # 0x645a
callq 0x2180
movq %rax, %rdi
callq 0x20d0
jmp 0x3740
leaq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x21e0
cmpb $0xa, 0x7(%rsp)
jne 0x3725
leaq 0x10(%rsp), %rsi
callq 0x3812
movl %eax, 0x48(%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x21e0
xorl %r15d, %r15d
leaq 0x10(%rsp), %r14
movl 0x48(%rbx), %eax
cmpq %rax, %r15
jae 0x375d
movq %r14, %rsi
callq 0x3812
movq 0x30(%rbx), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
jmp 0x3708
movq 0x58a4(%rip), %rdi      # 0x8fd0
leaq 0x2d42(%rip), %rsi      # 0x6475
callq 0x2180
movq %rax, %rdi
callq 0x20d0
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x2070
movl %ebx, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x21e0
leaq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x21e0
leaq 0x10(%rsp), %rsi
callq 0x3812
movl %eax, 0x4c(%rbx)
leaq 0x10(%rsp), %rdi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x21e0
movl $0x400, %r15d           # imm = 0x400
leaq 0x10(%rsp), %r14
leaq -0x400(%r15), %rcx
movl 0x4c(%rbx), %eax
cmpq %rax, %rcx
jae 0x37d1
movq %r14, %rsi
callq 0x3812
movq 0x30(%rbx), %rcx
movl %r15d, %edx
movl %eax, (%rcx,%rdx,4)
incq %r15
jmp 0x37ab
addl $0x3ff, %eax            # imm = 0x3FF
movl %eax, 0x38(%rbx)
movaps 0x2ac0(%rip), %xmm0     # 0x62a0
movups %xmm0, 0x20(%rbx)
movb $0x1, %bl
leaq 0x10(%rsp), %rdi
callq 0x2050
jmp 0x3742
jmp 0x37fd
jmp 0x37fd
jmp 0x37fd
jmp 0x37fd
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x22b0